Card 08/ 08

RecapDifficulty: Advanced1 min

Four Conditions, and the One Worth Removing

Five things this topic established, and where each one was.

What the topic established, and which card it was on
The thingWhere
A deadlock is a cycle of threads each holding what another wantsTwo Threads, Two Locks
Two correct methods taking the same locks in opposite orders is enoughA Transfer Between Two Accounts
Four conditions must hold at once; removing any one prevents itThe Four Conditions a Deadlock Needs
A global lock ordering is the condition you can remove for freeFixing the Transfer by Giving the Two Locks an Order
Idle threads stuck is deadlock; busy threads stuck is livelockDeadlock, Livelock and Starvation

No single method can deadlock on its own, so no amount of reading one method will find it. What causes a deadlock is two pieces of code disagreeing about an order, and the fix is a decision the whole program keeps.