I am wokring on pthread pool and there will be five separate thread and one queue. All the five threads are competing to get a job from the queue and I know the basic idea that I need to do lock/unlock and wait/signal.
But I am not sure how many mutex and cond variable I should have. Right now I have only one mutex and cond variable and all five thread will use it.
To elaborate on @Ivan’s solution…
Instead of a mutex + condition variable, you can use a counting semaphore + atomic operations to create a very efficient queue.
Enqueue operation is just:
Dequeue operation is:
The “atomic_add_to_queue” and “atomic_remove_from_queue” are typicaly implemented using atomic compare&exchange in a tight loop.
In addition to its symmetry, this formulation bounds the maximum size of the queue; if a thread calls enqueue() on a full queue, it will block. This is almost certainly what you want for any queue in a multi-threaded environment. (Your computer has finite memory; consuming it without bound should be avoided when possible.)
If you do stick with a mutex and condition variables, you want two conditions, one for enqueue to wait on (and deque to signal) and one for the other way around. The conditions mean “queue not full” and “queue not empty”, respectively, and the enqueue/dequeue code is similarly symmetric.