I have the following code in C++. The code is from C++ Concurrency In Action: Practical Multithreading
void do_work(unsigned id);
void f() {
std::vector<std::thread> threads;
for(unsigned i = 0; i < 20; ++i) {
threads.push_back(std::thread(do_work, i));
}
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
}
Suppose that threads[0] has completed processing and returns a value. I still have more files to process and would now like to assign this new file to a thread that is complete. How can I achieve this behavior in C++ ? Or must I destroy the thread and now create a new one upon thread completion ? But then how do I check if any of these threads have completed ?