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
- Reading shared state without the lock (the original bug): even a single
int/array read can see a stale value without proper synchronization;volatilefixes visibility but not the check-then-act atomicity you need here — use the lock. - Guard the wait in a
whileloop, not anif: aftersignalAll, only the threads whose direction is now green should proceed; the rest must re-check and wait again.
Takeaways
- Read and write shared state under the same lock — a one-sided guard is no guard.
- Vehicles wait on a condition for their green;
signalAllon every switch wakes the right ones. volatile/atomics give visibility but not check-then-act atomicity; here the lock is required.
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.
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.
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.
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.
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.