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:
- 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.
- Lock Contention: The spinning worker repeatedly locks and unlocks
queue_lock, starving producer threads that need to acquire the lock to enqueue new jobs. - Adding
sleep()orusleep()is Fragile: Addingusleep(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
-O0debug build. - Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.