this is a example from boost library.
int calculate_the_answer_to_life_the_universe_and_everything()
{
return 42;
}
boost::packaged_task<int> pt(calculate_the_answer_to_life_the_universe_and_everything);
boost:: future<int> fi=pt.get_future();
instead of boost::thread task(boost::move(pt));
to launch a task on the thread,
now I want to put the thread into shared_ptr vector and launch a task on the thread.
First i creat a vector.
std::vector<std::shared_ptr<boost::thread>> vecThreads;
And is this the right way to put a thread into vector?
vecThreads.push_back(std::make_shared<boost::thread>(boost::packaged_task<int> &pt));
thank you all for the attention!
Packaged tasks are just that. They don't "have" threads. They just run on a thread. Any thread.
In fact, it's an anti-pattern to start a thread for each task. But, of course, you can. I'd suggest using a
So you can depend on
to await all pending threads completion.
UPDATE
With shared pointers, here's an example:
Live On Coliru