TRT
Sign in
easyAdvancedconcept

Why must condition variable waits be in a predicate loop?

Explain spurious wakeups and that waking does not guarantee the condition holds, so re-check in a while loop.

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

The Runtime Theory Team2 min read
Learn it firstCondition Variables: pthread_cond_wait, Predicate Loops, and Spurious WakeupsPro — members onlyCoordinate thread state transitions without busy-waiting using pthread_cond_wait, eliminate lost wakeups, defend against spurious wakeups with predicate loops, and evaluate futex requeueing.

A strong answer

A strong candidate frames Why must condition variable waits be in a predicate loop? around the exact memory layout, compiler semantics, and hardware execution model covered in Condition Variables: pthread_cond_wait, Predicate Loops, and Spurious Wakeups. Explain spurious wakeups and that waking does not guarantee the condition holds, so re-check in a while loop.

This busy-wait loop has severe consequences:

  1. CPU Saturation: The worker thread spins in a tight loop across millions of iterations per second, driving CPU core utilization to 100% and generating heat while waiting for work.
  2. Lock Contention: The spinning worker repeatedly locks and unlocks queue_lock, starving producer threads that need to acquire the lock to enqueue new jobs.
  3. Adding sleep() or usleep() is Fragile: Adding usleep(1000) reduces CPU burn, but introduces arbitrary latency (jobs sit idle for up to 1 ms) and degrades throughput.

If the producer acquires the lock and posts work in the microsecond between pthread_mutex_unlock and sleep_until_notified, the notification arrives when nobody is listening. The worker then falls asleep and never wakes up.

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: Coordinate thread execution across state transitions using pthread_cond_wait, pthread_cond_signal, and pthread_cond_broadcast.
  • Mastery of core invariant: Explain the atomic release-and-sleep contract of pthread_cond_wait that eliminates lost-wakeup race windows.
  • Mastery of core invariant: Construct robust while-loop predicate guards to defend against spurious wakeups and stolen signals.
  • 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. 01How does the compiler, OS kernel, or hardware memory model enforce the invariants of Condition Variables: pthread_cond_wait, Predicate Loops, and Spurious Wakeups under optimization (`-O2`/`-O3`)?
  2. 02What production bug or undefined behavior occurs when Condition Variables: pthread_cond_wait, Predicate Loops, and Spurious Wakeups is misused across module or thread boundaries, and which sanitizer (`ASan`, `UBSan`, `TSan`) catches it?

Not started

Sign in to save your learning progress.

Sign in to save