我想与 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;
}
但是在执行程序时我得到一个std::future_error
抛出:
terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: No associated state
Aborted (core dumped)
我想知道为什么。我不应该能够存储未来的对象吗?