---
title: "Deadlock and the Four Conditions"
description: "Deadlock needs mutual exclusion, holding while waiting, no preemption, and a cycle of waiting. Removing any one of the four prevents it, which is why a fixed lock ordering is the usual answer in real "
canonical: https://lightmysky.com/learn/computing/deadlock-and-the-four-conditions-mt_CsMOA4sBb1
source: https://lightmysky.com/learn/computing/deadlock-and-the-four-conditions-mt_CsMOA4sBb1.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Deadlock and the Four Conditions

Deadlock needs mutual exclusion, holding while waiting, no preemption, and a cycle of waiting. Removing any one of the four prevents it, which is why a fixed lock ordering is the usual answer in real code.

Subject: Computing · Area: Computer Systems · Ages 20 to 22
Page: https://lightmysky.com/learn/computing/deadlock-and-the-four-conditions-mt_CsMOA4sBb1

## Ready when they can

- Draw the wait-for graph of a deadlocked pair of threads and find the cycle
- Break a given deadlock by removing one of the four conditions
- Explain how a global lock ordering prevents the cycle from forming

## Lesson: When two locks freeze each other

You already know two threads can share locks. A deadlock needs four things at once: each lock has one holder, each thread holds one lock while asking for another, nobody can take a held lock away, and the waiting forms a cycle.

**Example.** Thread A holds lock 1 and waits for lock 2. Thread B holds lock 2 and waits for lock 1. Draw an arrow from each waiter to the holder of the lock it wants. The arrows make a cycle, and that cycle is the deadlock.

Remove any one of the four and the deadlock breaks. The usual fix removes the cycle: agree on one order for all locks, such as lock 1 before lock 2, and make every thread follow it. With one order everywhere, a waiting cycle cannot form.

**Tip.** When your program freezes with no crash, sketch who holds what and who waits for what. If you find a cycle, pick the lock order that one thread breaks and change it to match the rest.

**Recap.** Deadlock needs all four conditions at once, so one shared lock order everywhere keeps the cycle from forming.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Race Conditions and Mutual Exclusion](https://lightmysky.com/learn/computing/race-conditions-and-mutual-exclusion-mt_hygT74jJDE)
- [Graphs and How to Store Them](https://lightmysky.com/learn/computing/graphs-and-how-to-store-them-mt_tSO-F1sTuu)

## Opens up

- [Model Checking and the State-Space Explosion](https://lightmysky.com/learn/computing/model-checking-and-the-state-space-explosion-mt__pgZ0PzEZo)
- [Concurrency Control: Locks and Snapshots](https://lightmysky.com/learn/computing/concurrency-control-locks-and-snapshots-mt_gOd17LRKel)
- [Message Passing and Asynchronous Work](https://lightmysky.com/learn/computing/message-passing-and-asynchronous-work-mt_twcpO-945r)
