Master the fundamental concepts of threads & concurrency through this focused micro-challenge.
Condition variables let threads sleep until a predicate becomes true. A mutex protects the predicate; wait() atomically releases the mutex and sleeps, reacquiring on wakeup. Spurious wakeups mean you always re-check the predicate in a loop.
Pattern:
while (!ready) cond_wait(&cv, &mutex);For example, a queue depth predicate count > 0 blocks consumers until a producer post() increments count and signals the CV.
This exact wait/signal pattern is what powers pthread_cond_wait in glibc, Java's Object.wait()/notify(), and every bounded producer-consumer queue in real message brokers like RabbitMQ's internal worker pools. The 'always recheck in a while loop, never an if' rule you'll apply here guards against spurious wakeups, a documented POSIX behavior that has caused real intermittent bugs in code that assumed a single wakeup always means the condition holds.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will implement cond_wait and cond_signal using futex-backed queues or pthread equivalents from scratch. The task asks for a bounded buffer demo proving that waiters sleep instead of spinning.
Implement condition variable with mutex.
Requirements:
Success Criteria:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github