Now let us present an implementation of a producer-consumer system using condition variables. This implementation works.
dequeue()
lock(A)
while (queue empty) {
wait(A, C) // atomically releases lock A and sleeps, waiting for
// condition variable C.
// When the thread wakes up, it re-acquires the lock.
// C is the condition variable that means ''queue not empty''.
}
remove_item()
unlock(A)
enqueue()
lock(A)
insert_item()
signal(C)
unlock(A)
In dequeue() above, if the thread wakes up and by chance the queue is empty, there is no problem: that's why we need the "while" loop.