Skip to content
BENIBIX Learn

Deadlock

Development⚙️ Compilers & RuntimesConcurrency

Also known as Deadlocks, Deadly Embrace

Two tasks each wait for the other. So neither can ever go on.

Think of it like

Think of it like two cars meeting on a narrow bridge. Neither will back up, so neither can move.

Example

Emma's bank app moves money between two accounts. One thread locks Lucy then Jack, the other locks Jack then Lucy. Both wait forever.

Why it matters

A deadlock gives no error message. The app just stops, which makes it one of the hardest bugs to spot.

Definition

A deadlock is when two tasks each wait for something the other holds. Then none of them can go on. Thread A holds lock 1 and waits for lock 2. Thread B holds lock 2 and waits for lock 1. Neither will ever let go, so the program freezes with no error. The usual fix is to always take locks in the same order everywhere.

Common mistake

Assuming a frozen app is just slow. Check whether its threads are all waiting on each other.

Key takeaways

  1. 1
    Wait in a circle. Each holds what the other needs.
  2. 2
    Silent freeze. No error shown.
  3. 3
    Same order. Take locks the same way.

Compare