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.
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.
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.
To be more clear, this is what i have so far:
- Split the buffer into N sub-buffers of the same size
- For each sub-buffer, create a thread and run it on the sub-buffer
- Wait for all threads to complete (WaitForMultipleObjects), add together the results and destory the threads
- Repeat
What i would like to achieve is this:
- Split the buffer into N sub-buffers of the same size
- Assign each sub-buffer to a thread from the threadpool (which has exactly N threads)
- Once a thread finishes, let it sleep until another task is ready
- When all threads are done (and sleeping) add together the results they produced
- Repeat by waking up the threads and assign them new tasks
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&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.
My main problem is with the management of threads:
- how do i make my threads "sleep" and wake them up once new task is ready?
- how do i wait for all the threads to finish?
I will be grateful for any help. Also feel free to ask questions if i was not clear enough. Thanks!