Knowledge Guide
HomeConcurrencyConcurrency Problems

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.

Threads A, B, C each gated by a semaphore; A releases the gate for B, B for C, producing output order 1, 2, 3
Threads A, B, C each gated by a semaphore; A releases the gate for B, B for C, producing output order 1, 2, 3

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

Pitfalls

Takeaways


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.

🎨 Explain it visually

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

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

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

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.

📝 My notes