Condition variables and monitor protocols

Watch a thread sleep until a job arrives, lose a signal that comes at the wrong moment, and learn why a woken thread tests again in a loop. Threads and concurrency contracts · Operating systems.

A worker finds the queue empty and must sleep, but a job may arrive in the gap before it is asleep. How does a thread sleep without missing the news?

What you will learn

  • Write the wait protocol with a mutex and a predicate
  • Explain why a lost signal strands a sleeper
  • Use a while loop and say when broadcast is needed

Lesson outline

  1. A worker with nothing to do
  2. Sleep and wake with a mutex
  3. The signal nobody hears
  4. Why the test sits in a loop
  5. One change, several waiters

18 minutes · 5 scenes · 0 predict checks · 9 quiz questions

The full lesson is available with Premium.