C++ 将 std::future 形式 std::async 存储在向量中并等待所有人

问题描述

我想用 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)

我想知道为什么。我不应该能够存储未来的对象吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)