principle-concurrency
Concurrency
Concurrency is a correctness hazard first, a performance tool second. Prove correctness before optimizing.
Failure Modes
Race condition — two goroutines/threads read and write shared state without synchronization; result is non-deterministic. Fix: mutex, channel, or atomic.
Deadlock — two or more tasks each hold a lock the other needs; all block forever. Fix: consistent lock-acquisition ordering; prefer higher-level primitives.
Livelock — tasks keep changing state in response to each other but make no progress; common in retry loops that back off identically. Fix: add jitter; use backpressure signals.
Starvation — a low-priority task never gets scheduled because high-priority tasks monopolize a resource. Fix: fair queues; priority inheritance; rate limits.
Lost wakeup — a condition-variable signal fires before the waiter starts waiting; waiter sleeps forever. Fix: always check the predicate in a loop; use atomic flags.
ABA problem — a CAS succeeds because a value returned to A, masking an intervening change. Fix: use version tags or hazard pointers; prefer higher-level data structures.