← Lessons

quiz vs the machine

Silver1050

Concurrency

Deadlock: The Four Conditions

Why threads freeze forever, and the four boxes that must all be checked.

4 min read · intro · beat Silver to climb

What a deadlock is

A deadlock is a state where two or more threads each wait for a resource that another holds, so none of them ever proceeds. The program does not crash; it simply stops making progress.

The Coffman conditions

A deadlock can only happen when all four of these hold at once:

  • Mutual exclusion — a resource is held in a non sharable way, so only one thread can use it at a time.
  • Hold and wait — a thread holds at least one resource while requesting another.
  • No preemption — a resource cannot be forcibly taken; it is released only voluntarily.
  • Circular wait — there is a cycle of threads, each waiting on the next.

Breaking the cycle

Because all four are required, breaking any one prevents deadlock. The most practical fix is to attack circular wait by imposing a global lock ordering: always acquire locks in the same agreed sequence. Another option is to attack hold and wait by grabbing all needed locks at once, or releasing what you hold before blocking.

Key idea

Deadlock needs four conditions together; remove circular wait with a consistent lock order and the cycle cannot close.

Check yourself

Answer to earn rating on the learn ladder.

1. Which condition does a global lock ordering directly eliminate?

2. How many of the four Coffman conditions must hold for deadlock?