Skip to content

Shared memory

Deadlock

Two locks, two threads, two orders, and a wait-for graph with a cycle in it.

12 minutes · deadlock · wait-for graph · lock ordering

Nobody is doing anything wrong

Each thread here is written correctly on its own: take both locks, do the work, release both. The deadlock is not in either thread. It is in the relationship between them — one takes the locks in the order it was handed the accounts, and so does the other, and the two orders disagree.

That is what makes deadlock so hard to review for. There is no bad line to point at. The bug is a property of the pair, and you can only see it by looking at both at once, which is what the wait-for graph does.

Everything you do here stays in this browser.