Knowledge Guide
HomeConcurrencyConcurrency Problems

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).

Two hydrogen threads and one oxygen thread wait at a barrier; once 2H and 1O are present they are released together to bond
Two hydrogen threads and one oxygen thread wait at a barrier; once 2H and 1O are present they are released together to bond

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

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


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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes