Getting stuck
Two threads run at the same time and share two locks, A and B. A lock lets only one thread at a time use something shared. T1 takes A, then B. T2 takes the same locks in the other order: B, then A.
T1 runs line 1, lock(A). Lock A is free, so T1 gets it at once: A's key moves onto T1's card. While T1 holds A, any other thread that asks for A must wait.
Before T1 reaches line 2, the scheduler switches to T2. T2 runs lock(B). B is free too, so T2 gets it. Each thread holds one lock and nobody waits yet. But each one's next line asks for the lock the other holds.
T1 runs line 2, lock(B). But B's key is on T2's card. T1 cannot go on, so the operating system puts it to sleep until B is released. T1 keeps holding A while it sleeps.
T2 runs lock(A). A is held by T1, which is asleep waiting for B. So T2 sleeps too. Each waits for a lock the other holds, and neither can ever release one. This is deadlock: both wait forever.
The four conditions
Why did it happen? Four conditions were all true at once. First, mutual exclusion: a lock has only one holder at a time. T2 could not share A with T1, so it had to wait.
Second, hold and wait: each thread kept the lock it already had while it waited for another one. T1 holds A and waits for B. T2 holds B and waits for A.
Third, no preemption: nothing can take a lock away from its holder. Only T1 will release A, and T1 is asleep. The operating system will not pull A off T1 and hand it to T2. The same goes for B.
Fourth, circular wait: the waiting closes a loop. T1 waits for B, which T2 holds. T2 waits for A, which T1 holds. Follow the arrows from T1 and you come back to T1.
Breaking one
Break any one condition and this deadlock cannot happen. Here T2 takes A first, like T1. T2 waits for A while holding nothing, T1 finishes and releases both, then T2 gets A and B. No loop forms.
What happens
;; step 1/10 · two-threads
thread T1 thread T2
1 lock(A); 1 lock(B);
2 lock(B); 2 lock(A);
3 work(); 3 work();
lock(X) take lock X; if another thread holds it,
sleep until it is released
work() use A and B, then release both
the same program in C99 with POSIX threads:
#define _POSIX_C_SOURCE 200809L
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
pthread_mutex_t A = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t B = PTHREAD_MUTEX_INITIALIZER;
void *t1(void *arg) {
(void)arg;
pthread_mutex_lock(&A);
sleep(1); /* let the other thread take its first lock */
pthread_mutex_lock(&B); /* blocks until B is unlocked */
puts("T1 done");
pthread_mutex_unlock(&B);
pthread_mutex_unlock(&A);
return NULL;
}
void *t2(void *arg) {
(void)arg;
pthread_mutex_lock(&B);
sleep(1); /* let the other thread take its first lock */
pthread_mutex_lock(&A); /* blocks until A is unlocked */
puts("T2 done");
pthread_mutex_unlock(&A);
pthread_mutex_unlock(&B);
return NULL;
}
int main(void) {
pthread_t x, y;
pthread_create(&x, NULL, t1, NULL);
pthread_create(&y, NULL, t2, NULL);
pthread_join(x, NULL);
pthread_join(y, NULL);
puts("both done");
return 0;
}
The four conditions come from Coffman, Elphick and Shoshani, "System Deadlocks" (ACM Computing Surveys, 1971). They are necessary: if any one is missing, deadlock cannot occur. Deadlock depends on timing. With these two orders, some interleavings finish and some get stuck, so the bug can hide in testing and appear only now and then.