I need to build a system of workers (represented as threads) and (multiple) queues. Individual jobs are waiting in one of the queues and waits for a worker thread to process them. Each worker can process jobs only from some of the queues. No spin-waiting. C/C++, pthreads, standard POSIX.
The problem for me is the “multiple queues” thing. I know how to implement this with a single queue. The workers need to wait on all the queues that they can process (wait for ANY of them).
On Windows I would use WaitForMultipleObjects, but this needs to be multi-platform.
I don’t want any particular code for this, only a hint or a description of the model which I should use. Thanks in advance.
What you can do is use a condition variable. Have your worker threads wait on a condition variable. When a job gets added to any of the job queues, signal the condition variable. Then, when the worker thread wakes up, it checks the queues it’s waiting on. If any of them have a job, it takes that job off the queue. Otherwise, it goes back to waiting on the condition variable. Waiting on a condition variable puts the thread to sleep, so it does not consume CPU time.
Of course, it goes without saying that you should protect all accesses to the job queues with a mutex (e.g.
pthread_mutex_t).