Real-World Deadlock Examples
Pattern 1: Lock Ordering Violation
Function A acquires lock X then Y. Function B acquires lock Y then X. Both are correct individually. Called concurrently? Deadlock. This is the most common cause.
Pattern 2: Callback Holding Lock
Thread holds lock, calls a callback or listener. Callback tries to acquire the same lock (reentrant) or a different lock held by another thread waiting for this one. Hidden dependency causes deadlock.
Pattern 3: Thread Pool Exhaustion
Task A runs on thread pool, submits Task B to same pool, waits for B. If pool is full, B cannot start. A waits for B, B waits for a free thread, all threads wait for A. Circular dependency on threads.
Pattern 4: Database Deadlock
Transaction 1 updates rows in order A, B. Transaction 2 updates rows in order B, A. Classic lock ordering problem at the database level. Most databases detect this and abort one transaction.