• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <pthread.h>
2 
3 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
4 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
5 pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
6 
7 void *
thread3(void * input)8 thread3 (void *input)
9 {
10     pthread_mutex_unlock(&mutex2); // Set break point at this line.
11     pthread_mutex_unlock(&mutex1);
12     return NULL;
13 }
14 
15 void *
thread2(void * input)16 thread2 (void *input)
17 {
18     pthread_mutex_unlock(&mutex3);
19     pthread_mutex_lock(&mutex2);
20     pthread_mutex_unlock(&mutex2);
21 
22     return NULL;
23 }
24 
25 void *
thread1(void * input)26 thread1 (void *input)
27 {
28     pthread_t thread_2;
29     pthread_create (&thread_2, NULL, thread2, NULL);
30 
31     pthread_mutex_lock(&mutex1);
32     pthread_mutex_unlock(&mutex1);
33 
34     pthread_join(thread_2, NULL);
35 
36     return NULL;
37 }
38 
main()39 int main ()
40 {
41   pthread_t thread_1;
42   pthread_t thread_3;
43 
44   pthread_mutex_lock (&mutex1);
45   pthread_mutex_lock (&mutex2);
46   pthread_mutex_lock (&mutex3);
47 
48   pthread_create (&thread_1, NULL, thread1, NULL);
49 
50   pthread_mutex_lock(&mutex3);
51   pthread_create (&thread_3, NULL, thread3, NULL);
52 
53   pthread_join (thread_1, NULL);
54   pthread_join (thread_3, NULL);
55 
56   return 0;
57 
58 }
59