I have a multithreaded application, with an loop waiting for user input as main thread. On the correct input, it is supposed to stop the loop and wait for all other threads, to end propperly.
For this purpose I created an std::list in which I put the std::future objects created for thread creation
std::list<std::future<int>> threads;
threads.emplace_front(std::async(std::launch::async, ...));
I was under the impression, that letting the list run out of scope, should block, until all threads return their main function, because the lists destructor will destrurct all std::future elements and the destructor of those will wait, for the thread to finish.
EDIT: Since it is relevant I will add it here: This is on Win7 with the MSVC version in Visual Studio 2013 Professional /EDIT
When I tried this, it didn't block, I had to add
for (auto it = threads.begin(); it != threads.end(); ++it) {
it->get();
}
to the end of the function, to block correctly.
Did I missunderstand something, or do I have to create the thread in a different way, to do what I want to do here?