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