I want to do several tasks in parallel with std::async and then wait until all the futures have completed.
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;
}
But when executing the program i get an std::future_error
thrown:
terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: No associated state
Aborted (core dumped)
And i wonder why. Shouldnt I be able to store the future objects?