KatInterview

Build a Reusable Thread Rendezvous Point from First Principles

System DesignModel answer
HardModel answerSystem Designconcurrencysynchronizationthreading

Last updated

Implement a cyclic barrier from a mutex and condition variables where N threads block until all arrive, then release together and reset for the next round.

Thread Rendezvous: Build a Cyclic Barrier

You need to coordinate a fixed pool of worker threads so that no thread moves past a checkpoint until every thread in the pool has reached it. Once the last thread arrives, all of them are unblocked simultaneously, and the barrier resets so the same pool can synchronize again on the next iteration — indefinitely, without being recreated.

Design and implement this primitive from scratch.

CyclicBarrier(parties: int)
    # parties >= 1, fixed at construction

wait() -> int
    # blocks until all `parties` threads have called wait() in this round
    # returns the calling thread's arrival index (0 = last to arrive)
    # resets automatically so the same object works for round 2, round 3, …

What you may use

Build from a mutex and condition variables (or your language's monitor primitives). Do not reach for a library barrier, latch, phaser, semaphore helper, or anything that already solves the problem for you.

Constraints and rules

  • parties >= 1, set once at construction.
  • The barrier must support an unlimited number of consecutive rounds without being recreated or manually reset under normal operation.
  • No busy-waiting in the main implementation.
  • A thread that arrived in round k must never be counted toward round k+1, and no thread may leave round k before every party in that round has arrived.
  • Optional action: support an optional callable passed at construction; it runs exactly once per round, executed by the last thread to arrive, before any waiters are released.
  • Broken-barrier semantics: if a waiting thread times out, is interrupted, or the per-round action raises an exception, the barrier enters a broken state; all current and future callers of wait() in that generation raise BrokenBarrierError until the barrier is explicitly reset.

What the interviewer is looking for

You are expected to reason through the design out loud before writing code. Cover:

  1. The data you need to track per round and why.
  2. How you prevent a fast thread from lapping a slow one (the reuse race).
  3. How you handle spurious wakeups correctly.
  4. The failure modes above and how each affects the barrier state.
  5. An atomics-only (lock-free / spin-based) variant — when would you prefer it, and what does it give up?
  6. How you would test this: what races does a naive test miss, and how do you expose them?

Hints

Hint 1

The trickiest part is safe reuse. If the last thread of round 1 resets the count to 0 immediately, a fast thread could call wait() again and be counted in round 1's wake-up. Track a 'generation' or 'round ID' alongside the count — each waiter spins on its own generation, not on the global count.

Hint 2

Condition variable waits must always be in a loop: `while not condition: cv.wait()`. Your loop condition should check whether *this round* is done or the barrier is broken, not whether the count reached parties. That way spurious wakeups are harmless and broken-barrier propagation works correctly.

Hint 3

For the atomics-only variant, think about what CAS (compare-and-swap) buys you on the arrival counter and where it falls short. A single atomic decrement tells you who is last, but you still need all other threads to observe the release — consider a flag or a generation counter spun on with a read loop, and be honest about the cost on high-core-count machines.