0 day streak
6. ConcurrencyDeadlock

Deadlock

A deadlock is when two or more threads are stuck forever, each waiting for something another one holds. The simplest version: thread A holds lock 1 and wants lock 2, while thread B holds lock 2 and wants lock 1, so neither can move, like two cars nose to nose on a one-lane bridge. It needs four conditions at once: resources only one thread can hold, holding one while waiting for another, no way to take a resource away by force, and a circular chain of waiting. Breaking any one prevents it. The usual fix is to always take locks in the same global order; timeouts help too, and databases detect cycles and abort one transaction.

In this lesson: Dining philosophers with a live wait-for graph that detects cycles. A lock-ordering toggle fixes it.