TRT
Sign in

Advanced · Module 11 · Lesson 4 of 6

Condition Variables: Waiting for Work

Park consumer threads until a producer enqueues work, using a mutex, a condition variable and a predicate loop that survives spurious wakeups.

Pro — members onlyAbout 12 minutes

By the end you can

  • Write the wait-in-a-predicate-loop pattern correctly
  • Explain why notify must happen with the queue already updated
  • Shut a queue down so no consumer waits forever

Builds on Deadlock: What It Is and How std::lock Avoids It.

01 · A question first

Three producers file books while two consumers index them. Consumers must sleep when the shelf is empty and wake the instant work arrives, then all stop cleanly when the last producer finishes. How do threads sleep without spinning, and who wakes whom?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • A kitchen pass
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem