Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Clean Code70 XP7 min

Deadlock: The 4 Conditions

A deadlock requires all four Coffman conditions simultaneously — break any one and deadlock becomes impossible.

Anatomy of Deadlock

A deadlock occurs when all four Coffman conditions hold simultaneously: (1) Mutual Exclusion — a resource can only be held by one thread at a time; (2) Lock & Wait — a thread holds at least one resource and waits to acquire more; (3) No Preemption — a held resource cannot be forcibly taken from a thread; (4) Circular Wait — thread A waits for a resource held by thread B, which waits for a resource held by thread A. Remove any single condition and deadlock becomes structurally impossible.

Breaking Strategies

Attack one condition: (1) Break Mutual Exclusion — use lock-free data structures where sharing is safe. (2) Break Lock & Wait — require threads to request all resources at once (all-or-nothing). (3) Break No Preemption — implement timeout-based lock acquisition; release everything and retry on timeout. (4) Break Circular Wait — impose a global total ordering on all resources and always acquire them in that order (always lock mutex A before mutex B). Lock ordering is the simplest and most common strategy in practice.

Code Challenge

Fix the deadlock by imposing a consistent global lock ordering.

💡Key takeaway

A deadlock needs all four Coffman conditions. Imposing a consistent global lock ordering costs almost nothing and breaks Circular Wait — making deadlock structurally impossible.

🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.

Hint: Lock ordering (always acquire A before B, everywhere in the codebase) eliminates Circular Wait — the 4th Coffman condition. One condition gone = deadlock impossible.

✗ Your version

Deadlock: The 4 Conditions — CleanKata — CleanKata