I am new to threading in C ++ 11, and I am wondering how to manage workflows (using the standard library) to perform some kind of task, and then die. I have a thread pool vector<thread *> thread_poolthat maintains a list of active threads.
Let's say I start a new thread and add it to the pool with thread_pool.push_back(new thread(worker_task)), where it worker_taskis defined as follows:
void worker_task()
{
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "Hello, world!\n"
}
Once the workflow is complete, what is the best way to reliably remove the thread from the pool? The main thread must work continuously and cannot be blocked during a call join. The general structure of the code confuses me more than the subtleties of synchronization.
Edit: Looks like I misused the concept of a pool in my code. All I had in mind was that I have a list of threads that currently work.
source
share