Implementing thread pools using pthreads in C ++


I'm having trouble in designing a program which has pool of threads.

The main issue i'm stuck at is when a thread is done with work the parent has to wait on the threadId (this is how a parent waits on thread using pthread_join). Because we do not know which of the threads is going to finish first, i'm not able figure out a programatic way to solve the problem.

Any explanation with a small snippet of code is appreciated.


Well, I don't know your setup or requirements, but you have run into the problem that pthread_join() waits for precisely one thread, and you actually want to wait for any thread.

Thus, the most obvious conclusion is that pthread_join doesn't help you. Sorry for stating the obvious, but I need to build up my case :-)

Instead, you'll probably have to come up with another idea. For example, you could wait on a condition variable; before a thread exits it would set the condition to "exited". The main thread could wait for that condition, iterate through the threads to find out which ones have terminated (it can be more than one), and eventually reset the condition. The condition mutex is usually sufficient to prevent races.

Threads could also add some ID into a list of exited threads in addition to setting the condition (you can use the condition mutex to protect that list), so the main thread only has to go through that list instead of checking every thread.


initialize condition variable with status "not exited"
launch your threads
while (some threads are still running) do
   lock condition variable on "exited"
   iterate through threads, remove the ones that have exited
   unlock condition variable with new condition "not exited"

Inside your threads:

 do whatever it needs to do
 lock condition variable
 unlock condition variable with new condition "exited"
 /* end of thread */