Sign in to follow this  
scratt

Thread Pool implementation..

Recommended Posts

To save myself having to re-invent the wheel I hunted around for a couple of thread-pool libraries with appropriate licenses online.. I found a few, and the most simplistic that I could build on appeared to be this one.. http://shobhitgupta12.blogspot.com/2008/03/thread-pool-implementation-using-c.html However, on OS X, try as I might the worker threads don't appear to idle properly. They try to execute and as they have no specific task they then cause a crash. Looking through the information on Posix threads, and deciding to mod it / fix it / or write my own implementation I can't find a problem with the above library.. Has anyone else tried it? I am just trying to get some feedback to see if the problem is with this library or something else I am doing.. Or if someone can recommend something similar they know works on OS X. Cheers. For the record I have no problem getting threads to work, but wanted to avoid the setup overhead so was planning to implement this threadpool in place of thread code I already have in situ. The above library works fine if you immediately give the threads work, but then borks once they go back into the pool. So I think the problem has a specific cause, but I am damned if I can see it.

Share this post


Link to post
Share on other sites
I just recently implemented my own version of a thread pool (coincidentally using POSIX threads on OS X).

Basically, I have the thread pool which accepts tasks (function objects, think boost::function) and then distributes them to a certain number of threads, set at construction and dynamically modifiable. Each worker thread has a thread-safe queue of tasks. When a thread finishes it's task, it starts the next task in it's queue. If there is none, then it sleeps until a new task is added. The thread pool sends tasks to the thread with the least number of tasks in it's queue so that the work load is balanced.

Share this post


Link to post
Share on other sites
Thanks for the feedback..

That's basically how the above library does it, and how I would implement it. However the sleeping threads created at start up, don't!

I have a list of jobs as long as the condo I live in is tall, so was just trying to save myself re-inventing the wheel.
But working through these things yourself is usually the best way to learn, so I guess I am just going to code this myself over the weekend.. I am trying to avoid Boost though. Nothing against it, but I do try to reduce dependancy on external libraries when they tools you need are already there.

What is irritating is that I have infected my mind with the above library, and can't see why it does not work. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this