Implementing a simple thread pool <p>I'm currently in the need of a simple and efficient thread pool implementation. I have searched here and also on Google and found numerous interesting links, but nothing i've found so far seems to be suitable. Most of the implementations i have found on the web are either too complicated or lack some of the key features i need.</p> <p>Also i don't want to use code that i do not understand, so i decided to code it myself (sometimes reinventing the wheel helps me push myself forward in terms of knowledge and experience). I of course understand the basic idea behind thread pool, but some implementation details are still somewhat unclear to me. This is probably because the kind of thread pool i need is a bit special. Let me describe it. I have a task that is done hundreds of thousands of times on a particular (large) buffer. I have measured that the performance is much better if I use threads for this task - the buffer is split into sub-buffers and each thread performs its task on the sub-buffer and returns the result. All the results from all threads are then added together, giving me the final solution.</p> <p>However since this is done very often i'm losing precious time because of so many threads being created (because of the overhead that comes with thread creation). So i would like to have a pool of threads that would perform this task, instead of creating a new set of threads every time.</p> <p>To be more clear, this is what i have so far:</p> <ul> <li>Split the buffer into N sub-buffers of the same size</li> <li>For each sub-buffer, create a thread and run it on the sub-buffer</li> <li>Wait for all threads to complete (WaitForMultipleObjects), add together the results and destory the threads</li> <li>Repeat</li> </ul> <p>What i would like to achieve is this:</p> <ul> <li>Split the buffer into N sub-buffers of the same size</li> <li>Assign each sub-buffer to a thread from the threadpool (which has exactly N threads)</li> <li>Once a thread finishes, let it sleep until another task is ready</li> <li>When all threads are done (and sleeping) add together the results they produced</li> <li>Repeat by waking up the threads and assign them new tasks</li> </ul> <p>As you can see, this is a bit of a special thread pool, since i need to wait for the threads to finish. Basically i want to get rid of the overhead of creating threads all the time, since the program goes through hundreds of thousands of iterations so it can create&amp;destroy milions of threads over its lifetime. Good news is that i do not need any synchronization at all between threads, they all get their own data and storage place fot the results. However i must wait until all threads are finished and i have the final solution, because the next task depends on the results of the previous task.</p> <p>My main problem is with the management of threads:</p> <ul> <li>how do i make my threads "sleep" and wake them up once new task is ready?</li> <li>how do i wait for all the threads to finish?</li> </ul> <p>I will be grateful for any help. Also feel free to ask questions if i was not clear enough. Thanks!</p>