What if two accounts transfer to one another concurrently?
Deadlock
Each thread locks the first mutex and waits for the second
They now have a circular dependency and will never progress or
unlock their mutex
Deadlock conditions
Threads hold resources exclusively
Threads hold some resources while waiting for others
Resources cannot be removed from threads by force
Threads wait in a circular chain
Dining Philosophers
Addressing Deadlocks
Detection and mitigation
Prevention
Detection
OS stores additional information about mutexes
Track which thread holds a mutex
Record which mutex a thread is waiting for
Use this graph to periodically check for cycles (deadlocks)
Mutex Cycle indicating
Deadlock
Breaking the Deadlock
A thread can be rolled back to before it attempted the offending
lock
Most systems don’t support rolling back threads
Killing a thread is the typical solution
Immediate detection
If we detect deadlock conditions when the last lock in the cycle is
attempted, we can notify applications and they can choose to take
appropriate action