1 /* Simple possible deadlock */ 2 #include <pthread.h> 3 4 static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; 5 static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; 6 7 static void *t1(void *v) 8 { 9 pthread_mutex_lock(&m1); 10 pthread_mutex_lock(&m2); 11 pthread_mutex_unlock(&m1); 12 pthread_mutex_unlock(&m2); 13 14 return 0; 15 } 16 17 static void *t2(void *v) 18 { 19 pthread_mutex_lock(&m2); 20 pthread_mutex_lock(&m1); 21 pthread_mutex_unlock(&m1); 22 pthread_mutex_unlock(&m2); 23 24 return 0; 25 } 26 27 int main() 28 { 29 pthread_t a, b; 30 31 /* prevent spurious messages from the dynamic linker */ 32 pthread_mutex_lock(&m1); 33 pthread_mutex_unlock(&m1); 34 35 pthread_create(&a, NULL, t1, NULL); 36 pthread_create(&b, NULL, t2, NULL); 37 38 pthread_join(a, NULL); 39 pthread_join(b, NULL); 40 41 return 0; 42 } 43 44