Pattern: Deterministic Ordering & Signaling (Problems 7–11)
Making concurrent threads produce a fixed order
Problems 7–11 (FizzBuzz, ZeroEvenOdd, Print in Order, Leap-Year, Palindrome) share one demand: several threads run concurrently, but their output must appear in a specific sequence. Left alone they race. You impose order with a signaling primitive — a semaphore (or condition variable, or in Go a channel) where each step unlocks the gate for the next.
Worked: Print in Order (Problem 9)
Three threads call first(), second(), third() in any order; output must be
"first second third". Start two gates closed; each method opens the next:
Semaphore g2 = new Semaphore(0), g3 = new Semaphore(0);
void first (Runnable p){ p.run(); g2.release(); } // open gate 2
void second(Runnable p){ g2.acquire(); p.run(); g3.release(); } // wait gate 2, open gate 3
void third (Runnable p){ g3.acquire(); p.run(); } // wait gate 3
No matter which thread is scheduled first, second blocks until first released
g2, and third until second released g3. The release is a
happens-before edge, so it also fixes visibility, not just order.
Same idea in Go (channels as gates)
g2, g3 := make(chan struct{}), make(chan struct{})
go func(){ first(); close(g2) }()
go func(){ <-g2; second(); close(g3) }()
go func(){ <-g3; third() }()
How each problem maps to it
- P9 Print in Order: the two-gate example above.
- P7 FizzBuzz (4 threads): four roles (number / fizz / buzz / fizzbuzz); a controller releases the
permit for whichever role matches each
i, then waits for it to finish — a per-step gate. - P8 ZeroEvenOdd: three threads (zero / even / odd) ping-pong via three semaphores so output is 0 1 0 2 0 3… — each prints then hands the permit to the next required role.
- P10 Leap-Year / P11 Palindrome: a producer thread computes, a printer thread waits on a
"ready" signal — the same gate pattern with one hand-off (a
CountDownLatchworks too).
Pitfalls
- Wrong initial permits: start gates at 0 (closed). Starting at 1 lets a later thread run first.
- Lost signal: with condition variables you must signal after setting state and re-check
in a
while(see Condition Variables) — semaphores avoid this since a release is remembered. - Deadlock: if every thread waits before anyone releases, all block. One gate must start open or one thread must be ungated.
Takeaways
- Order = signaling: each step
release()s the gate the next stepacquire()s. - Semaphores remember releases (no lost-signal); condition vars need the
whilere-check. - Go expresses the same gates as channels (send/close to signal, receive to wait).
Re-authored for this guide; signaling diagram hand-authored as SVG. Covers concurrency Problems 7–11. See also: Condition Variables, Semaphores, Go Concurrency.
🤖 Don't fully get this? Learn it with Claude
Stuck on Pattern: Deterministic Ordering & Signaling (Problems 7–11)? 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: Deterministic Ordering & Signaling (Problems 7–11)** (Concurrency) and want to truly understand it. Explain Pattern: Deterministic Ordering & Signaling (Problems 7–11) 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: Deterministic Ordering & Signaling (Problems 7–11)** 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: Deterministic Ordering & Signaling (Problems 7–11)** 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: Deterministic Ordering & Signaling (Problems 7–11)** 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.