Skip to content
Optimal Lab

Operating systems · interactive

Deadlocks

Two threads, two locks. Each grabs one and then waits for the other's. Nothing crashes; nothing happens, forever. The fix is a rule, not more code.

Thread T1

holds: nothing

waits for: —

Thread T2

holds: nothing

waits for: —

lock A

free

lock B

free

T1 needs A then B. T2 needs B then A.

Lock order

step 1 of 5
  1. 1

    A thread keeps the lock it has while it waits for the next one.

    Fine on its own.

  2. 2

  3. 3

  4. 4