CSOS & ConcurrencyDiagramIntermediate
Deadlock
KO: 데드락 (교착 상태)·EN: Deadlock·JA: デッドロック
A concurrency failure where two or more threads or transactions wait indefinitely for resources held by one another, halting progress completely
Table of Contents
Structure Diagram
Deadlock Circular Wait Standoff
1/4Thread 1 holds Lock A— Non-preemptive hold
Non-preemptive holdQuick Answer
What is Deadlock?
A concurrency failure where two or more threads or transactions wait indefinitely for resources held by one another, halting progress completely. It is a core concept in OS & Concurrency within CS.
Core Principles
A Deadlock occurs when all four Coffman Conditions hold simultaneously:
- Mutual Exclusion: Resources cannot be shared simultaneously.
- Hold and Wait: A thread holds at least one resource while waiting for another.
- No Preemption: Resources cannot be forcibly taken away.
- Circular Wait: A closed cycle of threads waiting on each other exists (
T1 → T2 → T1).
Breaking any single condition makes deadlocks impossible.
Practical Takeaways
- Enforce strict Lock Ordering: Always acquire locks in a canonical global order (e.g., sorted by account ID) to eliminate circular waits completely.
- Use timeouts (
tryLock) & keep critical sections tiny: Never call external network I/O while holding a mutex. - Handle database deadlocks gracefully: Engines like PostgreSQL and MySQL detect cycles via a wait-for graph and abort one victim transaction—application code should catch deadlock errors and retry idempotently.
Notable Examples
- SQL Row-Lock Deadlocks: Concurrent transactions updating the same two rows in opposite order.
- Thread-Pool Starvation: Parent tasks blocking pool threads while awaiting child tasks queued on the same full pool.
- Distributed Locks without TTL: A crashed worker holding a lock forever without lease expiration.