Deadlock, Livelock, Starvation and Priority Inversion
Deadlock needs all four Coffman conditions at once, so every prevention technique breaks one of them. Liveness also covers livelock, starvation and priority inversion, and real systems deadlock through pools, channels and event loops, not only mutexes.
Key points
- 1
The four conditions are mutual exclusion, hold and wait, no preemption and circular wait. Lock ordering breaks circular wait; all-or-nothing acquisition breaks hold and wait; try-lock with release approximates preemption.
- 2
Order locks by a unique, stable key such as an id. Handle ties (equal hashes) with a tie-breaker lock and same-object calls (transfer to self) explicitly.
- 3
Thread-pool starvation deadlock: tasks that block on subtasks in the same bounded pool hang once every worker is waiting. Use a separate pool or avoid blocking inside workers.
- 4
Livelock is busy failure: threads keep reacting to each other without progress. Randomised exponential back-off breaks the symmetry.
- 5
Starvation is when the system progresses but one thread does not (reader-preferring locks, strict priorities). Fair hand-off and aging fix it.
- 6
Priority inversion lets medium-priority work delay a high-priority task through a low-priority lock holder; priority inheritance or ceiling protocols bound it.
- 7
Databases detect deadlocks with wait-for graphs and abort a victim; applications must retry and should lock rows in a consistent order.
Common traps
A timeout or retry loop hides deadlocks but does not remove them; the lock order is the real fix.
Fair locks prevent starvation on one lock but do nothing about deadlock across several locks.
An unsafe state in the Banker's algorithm means deadlock is possible, not that it has already happened.
Read the source
Test yourself on Deadlock, Livelock, Starvation and Priority Inversion
Ten questions, with the answer and explanation after each one.