DEADLOCK

Both holding, both waiting

L215 MINRACE CONDITIONS
PREDICT

Thread A locks L1 then L2. Thread B locks L2 then L1. Which single change makes deadlock impossible?

Thread A takes lock L1 and then asks for L2. Thread B takes L2 and then asks for L1. Each request blocks because the other thread holds the lock. Each thread can only release what it holds after it gets what it is waiting for. That is a cycle in the wait-for graph: A waits for B, B waits for A, and nothing in the system will ever break it. The code is correct in every run where one thread gets both locks before the other asks for either, which is almost every run.

▶ SIMULATION
THREAD A
holds: —
waits for: —
pc: start
LOCKS
L1: free
L2: free
WAIT-FOR GRAPH
(no edges)
THREAD B
holds: —
waits for: —
pc: start
✓ no cycle in the wait-for graph
SCHEDULE
01[A]A: lock(L1)
02[B]B: lock(L2)
03[A]A: lock(L2) → held by B, WAIT
04[B]B: lock(L1) → held by A, WAIT
05[A]A: work — never runs
06[B]B: work — never runs
Start of deadlock scenario. 6 steps.
// Thread A:                 // Thread B:
lock(L1);                  lock(L2);
lock(L2);  // waits for B  lock(L1);  // waits for A
// work                    // work
unlock(L2); unlock(L1);    unlock(L1); unlock(L2);

Watch the WAIT-FOR GRAPH in the middle panel. In DEADLOCK it gains the edge A → B, then B → A, and the invariant turns red the instant the cycle closes. In FIX: LOCK ORDER thread B blocks on its first lock instead, the graph has one edge, and A finishes and hands L1 over.

BREAK IT NOT YET

Each thread takes two locks in its own order. Find the interleaving where both threads block forever.

GOAL: a cycle appears in the wait-for graph

INJECT A FAULT NOT YET

Every thread takes L1 before L2, so this schedule cannot deadlock. Inject one out-of-order lock and find the position where it does.

GOAL: a cycle appears in the wait-for graph

FIX IT NOT YET

Two threads transfer money in opposite directions at the same time. Rewrite transfer so it can never deadlock, and holds both accounts' locks while it moves the money so no one sees it in neither account. The grader tries every interleaving of the two threads' lock and balance operations.

Mental model:

A deadlock is a cycle in the graph of who waits for whom. You do not fix it by changing timing. You make the cycle impossible: give every lock a rank and only ever acquire upward, or never hold one lock while blocking on another (try-lock and back off). If you cannot say the order in which your code takes its locks, you do not know whether it deadlocks.

IN PRODUCTION

Two database transactions that update the same rows in opposite orders (the database detects it and kills one). A logger that takes its own lock while a callback inside it logs. A connection pool whose checkout blocks while holding a cache lock that the cache refresh needs. Symptom: a hang with idle CPU, cleared by restart.

IN INTERVIEWS

“Here are two functions that each take two locks. Can they deadlock?” Draw the wait-for graph, name the four conditions, then say which one you break: a global lock order (the usual answer), try-lock with backoff, or a single coarser lock. Mention that databases detect the cycle and abort a victim, and that your application code gets no such help.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: ABA →