Original Post
Hello everyone
I'm going to need some sort worker threadpool in my project so i can queue independent jobs and let the pool takes care of finding a thread to execute the job. The pool would run a fix number of threads though it could have a minimum and a maximum number of threads (i have no idea how to manage growing the pool).
What i'm thinking so far is this :
- A FIFO queue that will receive the jobs to be executed (from external threads).
- A mutex (critical section on windows or a pthread_mutext_t on POSIX systems) to synchronize access to the queue.
- An event that would be managed by the Pop/Push operations of the queue (event api on windows or pthread_cond_wait on POSIX systems).
The algorithm would be somthing like this :
All threads are sitting on the event. When a job is added to the queue, the event is fired. All the threads (that aren't busy doing executing some job) will try to dequeue the item (only one will because of the mutex) then the event is reset.
That's the basic idea, but alot of things seem wrong with this design. All the threads firing at the same time doesn't sound good. I'm also worried about contention.
I would be interested in having your ideas.
Thanks alot in advance.
P.S.
I am working with C++ and the code must compile on non C++11 compilers. Furthermore, it would be nice to avoid using Boost (not that it's bad quite on the contrary).
I'm going to need some sort worker threadpool in my project so i can queue independent jobs and let the pool takes care of finding a thread to execute the job. The pool would run a fix number of threads though it could have a minimum and a maximum number of threads (i have no idea how to manage growing the pool).
What i'm thinking so far is this :
- A FIFO queue that will receive the jobs to be executed (from external threads).
- A mutex (critical section on windows or a pthread_mutext_t on POSIX systems) to synchronize access to the queue.
- An event that would be managed by the Pop/Push operations of the queue (event api on windows or pthread_cond_wait on POSIX systems).
The algorithm would be somthing like this :
All threads are sitting on the event. When a job is added to the queue, the event is fired. All the threads (that aren't busy doing executing some job) will try to dequeue the item (only one will because of the mutex) then the event is reset.
That's the basic idea, but alot of things seem wrong with this design. All the threads firing at the same time doesn't sound good. I'm also worried about contention.
I would be interested in having your ideas.
Thanks alot in advance.
P.S.
I am working with C++ and the code must compile on non C++11 compilers. Furthermore, it would be nice to avoid using Boost (not that it's bad quite on the contrary).