What is Deadlock?

A state in which threads each hold a resource while waiting for one held by another, so none can proceed. Fixing a global lock order prevents it.

Field: Multicore & Synchronization

Japanese name: デッドロック

The four necessary conditions

Deadlock can occur only when four conditions, often called the Coffman conditions, hold together. Mutual exclusion: a resource can be held by only one thread at a time. Hold and wait: a thread keeps what it has while waiting for more. No preemption: resources cannot be taken away forcibly. Circular wait: a cycle of threads exists in which each waits for a resource held by the next. Because all four are required, every prevention technique works by making at least one of them impossible.

What it looks like

The classic case involves two locks. Thread 1 holds lock A and waits for lock B, while thread 2 holds B and waits for A; neither releases what it holds, so both wait forever. With locks that put waiting threads to sleep, the CPU goes idle and the program simply appears frozen. Livelock differs: threads stay busy, for example repeatedly backing off and retrying in step, yet make no progress. Starvation is when one thread never gets a resource while others proceed, and thrashing is a memory-paging problem, not a lock problem.

Preventing and handling it

The most common practical rule is a global lock order: if every thread always acquires A before B, no thread can hold B while waiting for A, so no cycle can form. Other approaches acquire all needed locks at once, or use a try-lock and release everything held when a lock is unavailable, then retry after a delay. Some systems instead detect deadlock by finding cycles in a wait-for graph and break it by aborting one participant, as databases do with transactions. Lock-order checking tools can flag violations during testing.

Deadlock in hardware

Hardware interconnects can deadlock too. In an on-chip network, packets occupy buffers while waiting for buffers ahead of them; if those dependencies form a cycle, traffic stops permanently. Designers prevent this with routing rules that rule out cycles, such as dimension-order routing on a mesh, or with virtual channels that break the dependency chain. Protocol deadlock, where responses cannot proceed because requests fill the same buffers, is avoided by giving request and response messages separate buffers or channels.

Try it: questions about Deadlock

Which is an example of deadlock?

  1. AThread 1 holds lock A and waits for lock B, while thread 2 holds lock B and waits for lock A
  2. BTwo threads update the same variable without synchronization and corrupt it
  3. COne thread acquires a lock and releases it immediately
Show answer and explanation

Answer: A. Thread 1 holds lock A and waits for lock B, while thread 2 holds lock B and waits for lock A

Deadlock is a state in which threads each hold a resource while waiting for one held by another, so none can proceed. In the example, thread 1 waits for B to be released and thread 2 waits for A, but neither gives up its own lock, so both stop forever. With locks that sleep while waiting, they stop without using the CPU, so the program simply appears frozen.

In a program using several locks, what is the most common way to prevent deadlock?

  1. AFix a single global order in which every thread acquires locks
  2. BNever release a lock once acquired
  3. CLimit the program to two threads or fewer
Show answer and explanation

Answer: A. Fix a single global order in which every thread acquires locks

Deadlock arises when threads form a cycle, each waiting for a lock held by another. If everyone follows "always take A before B", no thread can hold B while waiting for A, so no cycle forms. Other methods include acquiring all needed locks at once, or releasing held locks and retrying when a lock cannot be obtained.

FAQ

What are the four conditions for deadlock?

Mutual exclusion, hold and wait, no preemption and circular wait; all four must hold at once, so breaking any one prevents deadlock.

How do you prevent deadlock with multiple locks?

Define one global order for acquiring locks and make every thread follow it, which makes a circular wait impossible; acquiring all locks at once or backing off with try-lock are alternatives.

What is the difference between deadlock and livelock?

In deadlock the threads are blocked and do nothing; in livelock they keep running and changing state, for example retrying in response to each other, but still never make progress.

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary