Rechecking sample availability after waiting

Two analysis threads take samples from a shared buffer. The integer ready_count records how many samples are available, and all accesses to the buffer and count use the same mutex. A producer adds one sample at a time, increases the count and then notifies waiting threads.


A condition variable lets a thread wait for a possible change in shared state. Its wait operation atomically releases the mutex and blocks the calling thread; before returning, it reacquires the mutex.


Each consumer must never remove a sample from an empty buffer. While holding the mutex, begin the consumer's guard with the keyword , followed by the condition ready_count == 0 and the wait operation as its body. After the guard, the consumer removes one sample and decreases the count before releasing the mutex.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Threads and Concurrency Questions: