TRT
Sign in
mediumAdvancedcoding

Why must std::condition_variable::wait always check its condition in a predicate loop, and how do you shut down a blocking queue cleanly?

Explain spurious wakeups and stolen wakeups, trace how cv.wait atomically unlocks and sleeps, and show the done-flag shutdown pattern.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstCondition Variables: Waiting for WorkPro — members onlyPark consumer threads until a producer enqueues work, using a mutex, a condition variable and a predicate loop that survives spurious wakeups.

A strong answer

A strong candidate frames Why must std::condition_variable::wait always check its condition in a predicate loop, and how do you shut down a blocking queue cleanly? around the exact memory layout, compiler semantics, and hardware execution model covered in Condition Variables: Waiting for Work. Explain spurious wakeups and stolen wakeups, trace how cv.wait atomically unlocks and sleeps, and show the done-flag shutdown pattern.

Producers never wait for consumers; consumers never spin. The mutex protects the queue, the condition variable parks the idle.

A condition variable is a sleep queue plus a protocol. wait atomically releases the mutex and parks the thread; a notify_one or notify_all wakes sleepers, which reacquire the mutex and recheck. The trace shows the ordering invariant: mutate, then notify. The predicate lambda (done || !empty) is the loop condition, evaluated under the lock on every wakeup, which absorbs spurious wakeups and racing notifications.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Write the wait-in-a-predicate-loop pattern correctly.
  • Mastery of core invariant: Explain why notify must happen with the queue already updated.
  • Mastery of core invariant: Shut a queue down so no consumer waits forever.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Should a producer call notify_one() while holding the mutex or after unlocking it?
  2. 02What goes wrong if the done flag is a std::atomic<bool> modified without locking the mutex that guards cv.wait?

Not started

Sign in to save your learning progress.

Sign in to save