DeadlocksWhat is Deadlock?Medium⏱️ ~2 min

Understanding Deadlock

Definition
Deadlock occurs when two or more threads are blocked forever, each waiting for a resource held by another. No thread can proceed because each needs something the other holds.

The Traffic Intersection

Four cars arrive at a four-way intersection simultaneously. Each waits for the car on their right to go first. No one moves. No one can move. Everyone waits forever. This is deadlock - circular waiting with no way out.

Deadlock vs Other Problems

Deadlock: All threads blocked. No progress at all. System is frozen.

Livelock: Threads running but not making progress. Like two people in a hallway stepping aside the same way repeatedly.

Starvation: Some threads progress, others wait indefinitely. System works, but unfairly.

Classic Deadlock: Circular Wait
Thread Aholds Lock 1Thread Bholds Lock 2wants Lock 2wants Lock 1

Why Deadlock Is Serious

Deadlock does not crash your program. It silently freezes. CPU usage drops to zero. Requests pile up. Users see timeouts. Worst part: it might happen once a week under specific load, making it hard to reproduce and diagnose.

Key Characteristic: In deadlock, no thread can make progress. Every blocked thread is waiting for a resource held by another blocked thread. The cycle has no exit.
💡 Key Takeaways
Deadlock: threads blocked forever, each waiting for resources held by others. Circular dependency.
Differs from livelock (threads run but no progress) and starvation (some progress, unfair to some).
Deadlock is silent. No crash, no error. System just freezes. Hard to detect and diagnose.
Circular wait is the key pattern: A waits for B, B waits for A. Neither can proceed.
Often appears under specific load conditions. Works in testing, fails in production at 3 AM.
📌 Examples
1Thread A holds Lock 1, wants Lock 2. Thread B holds Lock 2, wants Lock 1. Both wait forever.
2Database: Transaction 1 locks row A, wants row B. Transaction 2 locks row B, wants row A. Deadlock.
3Dining philosophers: All pick up left fork, wait for right fork. Everyone holding one, wanting another.
← Back to What is Deadlock? Overview
Understanding Deadlock | What is Deadlock? - System Overflow