Knowledge Guide
HomeConcurrencyConcurrency Problems

Problem 17 Traffic-Light-Controlled Intersection Synchronization

Mutual exclusion across directions — read the state under the lock

An intersection must never let conflicting directions move at once. The shared "which direction is green" state is read by every vehicle thread and written by the controller, so every access must be synchronized. The earlier version read a plain (non-volatile) array without the lock, so a vehicle could act on a stale or torn value and two directions could cross together — the exact accident the lock exists to prevent. The fix: guard the state with a lock and let vehicles wait on a condition until their direction is green.

Correct Java

import java.util.concurrent.locks.*;

class Intersection {
    private final Lock lock = new ReentrantLock();
    private final Condition changed = lock.newCondition();
    private int green = 0;                 // 0 = North-South, 1 = East-West

    // controller flips the light atomically
    public void switchLight() {
        lock.lock();
        try { green = 1 - green; changed.signalAll(); }
        finally { lock.unlock(); }
    }
    // a vehicle crosses only when its direction is green; state read UNDER the lock
    public void cross(int direction) throws InterruptedException {
        lock.lock();
        try {
            while (green != direction) changed.await();   // wait for my green
            drive(direction);                             // safe: exclusive + correct light
        } finally { lock.unlock(); }
    }
}

Both the read (green != direction) and the write (green = 1 - green) happen while holding the same lock, so there is a single, consistent view and only the green direction ever drives.

Correct Go (mutex + cond)

type Intersection struct { mu sync.Mutex; cv *sync.Cond; green int }
func New() *Intersection { x := &Intersection{}; x.cv = sync.NewCond(&x.mu); return x }
func (x *Intersection) Switch() { x.mu.Lock(); x.green = 1 - x.green; x.cv.Broadcast(); x.mu.Unlock() }
func (x *Intersection) Cross(dir int) {
    x.mu.Lock()
    for x.green != dir { x.cv.Wait() }   // Wait releases the mutex while parked
    drive(dir)
    x.mu.Unlock()
}

Pitfalls

Takeaways


Re-authored for correctness for this guide (the prior version read unsynchronized state and lacked cross- direction exclusion). See also: Mutex Lock, Condition Variables, Critical Section & Race Condition.

🤖 Don't fully get this? Learn it with Claude

Stuck on Problem 17 Traffic-Light-Controlled Intersection Synchronization? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Problem 17 Traffic-Light-Controlled Intersection Synchronization** (Concurrency). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Problem 17 Traffic-Light-Controlled Intersection Synchronization** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Problem 17 Traffic-Light-Controlled Intersection Synchronization**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Problem 17 Traffic-Light-Controlled Intersection Synchronization**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes