Master the fundamental concepts of threads & concurrency through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksBarriers synchronize phases: no thread enters phase two until all threads finish phase one. Scientific simulations use them each timestep; thread pools use variants when joining forked work.
Operations:
For example, 4 threads computing a matrix row each call barrier_wait(); printing results before all 4 arrive would read incomplete rows.
POSIX's pthread_barrier_wait and MPI's MPI_Barrier implement exactly this pattern to synchronize compute phases in HPC codes like Jacobi iteration and parallel matrix multiplication, where correctness depends on every rank finishing phase N before any starts phase N+1. The generation-counter trick you implement here is what makes the barrier cyclic and safe to reuse across phases without a lost-wakeup race, the same issue naive reusable barriers in early parallel libraries got wrong.
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 a sense-reversing or mutex/cond barrier for a fixed thread count. The task asks you to run a phased computation where phase B output depends on all phase A completions.
A barrier makes N threads wait until all of them have arrived, then releases them together. A barrier is reusable, so the same threads meet again in the next phase. Getting that right needs a generation counter. With only an arrival count, the last thread resets the count, and a woken thread that re-checks "count < N" (as every correct condition-variable wait must) sees 0 and goes back to sleep for ever. Simulate both versions with an explicit schedule.
cLoading…
count < N. The count was reset, so the thread waits again: the release was missed. It still counts as waiting, but it adds nothing to the count.T: already waiting at the barrier, T: still inside barrier_wait (needs run), T: not released, T: unknown operation X, and barrier: threads are waiting, cannot resize.cLoading…
With nobody waiting, the release says releases nobody. Lists follow the order in which threads first appeared. The final line is always printed.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover several phases with a fast thread arriving early for the next generation, a one-thread barrier, resizing while threads wait, the naive barrier's stale waiters being swept into a later release, and misuse errors.