我正在尝试编写一个相当简单的方法来返回未来。lambda 决定了未来。这是一个最小的例子。实际上,lambda 可能会在不同的线程中调用,等等。
#include <future>
std::future<std::error_code> do_something() {
std::promise<std::error_code> p;
auto fut = p.get_future();
auto lambda = [p = std::move(p)] {
std::error_code error;
p.set_value(error);
};
lambda();
return std::move(fut);
}
int main() { return do_something().get().value(); }
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我收到类型错误。VSCode 智能感知 说:
没有重载函数“
std::promise<_Ty>::set_value [with _Ty=std::error_code]”的实例与参数列表和对象匹配(该对象具有阻止匹配的类型限定符) -- 参数类型为: (std::remove_reference_t<std::error_code &>) -- 对象类型为:const std::remove_reference_t<std::promise<std::error_code> &>
MSVC 编译器说:
错误 C2663: '
std::promise<std::error_code>::set_value': 2 个重载没有对 'this' 指针进行合法转换
我真的不明白 VS Code 错误。它是说它认为error是 aconst promise<error_code>吗?如何正确调用lambda 捕获中的set_value承诺? …
std::future我在C++编写的程序中看到了 的用法。所以,我很快去查找什么是:std::future,并得到了一个相当复杂的答案。
谁能用简单的话来形容一个6岁的孩子?我的理解力只有6岁孩子的水平……
只需一个小定义和最小的用例就可以为我工作。如果问得太多,请告诉我,我会收回我的问题。
我使用的组合std::async和std::future从C++ 11.我正在使用对我在代码中执行的某项活动强制执行time_out,这可能需要一些时间,因为我尝试连接到服务器.
以下是代码:
#include <future>
#include <chrono>
std::size_t PotentiallyLongRunningActivity() {
using namespace std::chrono_literals;
std::this_thread::sleep_for(10000s);
return 10;
}
bool DoActivity() {
bool activity_done = false;
auto my_future_result(std::async(std::launch::async, []() {
return PotentiallyLongRunningActivity(); //returns size_t
}));
std::future_status my_future_status = my_future_result.wait_for(std::chrono::milliseconds(800));
if (my_future_status == std::future_status::timeout) {
activity_done = false;
}
else if (my_future_status == std::future_status::ready) {
if (my_future_result.valid() && my_future_result.get() > 0) {
activity_done = true;
}
}
return activity_done;
//my_future_result hangs while exiting this method …Run Code Online (Sandbox Code Playgroud) I have a program that calculates some values in different threads with std::packaged_task<int()>. I store the std::future I get from the packaged tasks via get_future() in a vector (defined as std::vector<std::future<int>>).
When I calculate the sum of all the tasks I use a for loop and it's working:
// set up of the tasks
std::vector<std::future<int>> results;
// store the futures in results
// each task execute in its own thread
int sum{ 0 };
for (auto i = …Run Code Online (Sandbox Code Playgroud) 我想与 std::async 并行执行多项任务,然后等到所有期货都完成。
void update() {
// some code here
}
int main() {
std::vector<std::future<void>> handles(5);
for (int i = 0; i < 5; ++i) {
auto handle = std::async(std::launch::async, &update);
handles.emplace_back(std::move(handle));
}
for (auto& handle : handles) {
handle.wait();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是在执行程序时,我得到了一个std::future_error抛出:
terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: No associated state
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
我想知道为什么。我不应该能够存储未来的对象吗?
为什么不在这个抽象基类中创建这样的线程呢?我试图抽象出从这个基类派生的用户的所有多线程细节.当我清楚地写出callbackSquare返回类型时,我不明白它为什么说"没有类型命名为'type'" int.
#include <iostream>
#include <future>
#include <vector>
class ABC{
public:
std::vector<std::future<int> > m_results;
ABC(){};
~ABC(){};
virtual int callbackSquare(int& a) = 0;
void doStuffWithCallBack();
};
void ABC::doStuffWithCallBack(){
for(int i = 0; i < 10; ++i)
m_results.push_back(std::async(&ABC::callbackSquare, this, i));
for(int j = 0; j < 10; ++j)
std::cout << m_results[j].get() << "\n";
}
class Derived : public ABC {
Derived() : ABC() {};
~Derived(){};
int callbackSquare(int& a) {return a * a;};
};
int main(int argc, char **argv)
{ …Run Code Online (Sandbox Code Playgroud)