How can I use a vector of thread futures to get the return values from a group of threads? Should this work? If not, how do I adapt it?
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());
Well this code doesn’t compile because packaged tasks are not copyable.
Based on the problem statement, all you need is a vector of futures, though, why even try to store packaged_tasks?