classical producer consumer threading
In the Classical producer consumer problem . producer sleeps when itemCount == BUFFER_SIZE amd wakes up again when it goes down. But once itemCount grows up, producer thread is put to sleep. how can it know that itemCount has gone down and it needs to wakeup ? In pseudo-code the producer is something like: void producer_thread() { while(true) queue.push( produce() ); } so consider the queue push method (I've used pthreads here, but the same logic applies with other libraries) void SynchronizedQueue::push(Item const &i) { pthread_mutex_lock(&mutex); // queue is full, so wait for consumer while