如何使用线程期货向量从一组线程中获取返回值?这应该工作吗?如果不是,我该如何适应它?
int calculate_the_answer_to_life_the_universe_and_everything()
{
return 42;
}
vector<packaged_task<int> > tasks;
vector<unique_future<int> > futures;
for (int i=0; i < 4; ++i)
{
tasks.push_back(packaged_task<int>(calculate_the_answer_to_life_the_universe_and_everything));
futures.push_back(tasks.back().get_future());
thread task(tasks.back());
}
boost::wait_for_all(futures.begin(), futures.end());