Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)
When threads must wait for a group, not just one signal
Problems 12 (Building H₂O), 13 (dual-thread sync), 14 (multi-buffered master/worker) and 17 (traffic-light
intersection) all need threads to rendezvous: nobody proceeds until a required set has arrived.
That's a barrier, built from semaphores (or CyclicBarrier/Phaser in Java).
Worked: Building H₂O (Problem 12)
Hydrogen and oxygen threads must bond in groups of exactly 2 H + 1 O. Gate each element type with a semaphore, and use a barrier so a molecule only forms when all three are present:
Semaphore hGate = new Semaphore(2), oGate = new Semaphore(1);
CyclicBarrier bond = new CyclicBarrier(3); // 2 H + 1 O
void hydrogen(Runnable bondH) {
hGate.acquire(); // at most 2 H proceed
bond.await(); // wait for the full molecule
bondH.run(); hGate.release();
}
void oxygen(Runnable bondO) {
oGate.acquire();
bond.await();
bondO.run(); oGate.release();
}
How each problem maps to it
- P12 H₂O: per-type semaphores + a 3-party barrier (above).
- P13 Dual-thread synchronization: a 2-party barrier (or one semaphore each way) so two threads alternate in lock-step.
- P14 Multi-buffered master/worker: a bounded set of buffers guarded by two counting semaphores
(
empty,full) — the producer-consumer pattern with more than one slot. - P17 Traffic light: a mutex over the intersection plus per-direction gates; only one direction "goes" at a time — mutual exclusion over a shared critical region with fair turn-taking.
In Go
var bond sync.WaitGroup; bond.Add(3) // simplest 3-party barrier
// each atom goroutine: bond.Done(); bond.Wait() then bond
// counting semaphore = make(chan struct{}, n); turn-taking = a token passed on a channel
Takeaways
- Barrier = "wait until the whole group arrives, then release together";
CyclicBarrierresets for reuse,CountDownLatchis one-shot. - Counting semaphores gate "how many of X may proceed"; combine with a barrier for group formation.
- Go:
WaitGroupfor a barrier, buffered channel for a counting semaphore, a token for turn-taking.
Re-authored for this guide; barrier diagram hand-authored as SVG. Covers Problems 12, 13, 14, 17. See also: Semaphores, Condition Variables, Deterministic Ordering & Signaling.
🤖 Don't fully get this? Learn it with Claude
Stuck on Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)** (Concurrency) and want to truly understand it. Explain Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17) from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Pattern: Barriers & Resource Coordination (Problems 12, 13, 14, 17)** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.