8 static pthread_cond_t cond1;
9 static pthread_cond_t cond2;
10 static pthread_mutex_t test_lock = PTHREAD_MUTEX_INITIALIZER;
13 thread1_func(void* arg)
15 printf("Thread 1 (arg=%p tid=%d) entered.\n", arg, gettid());
16 printf("1 waiting for cond1\n");
17 pthread_mutex_lock(&test_lock);
18 pthread_cond_wait(&cond1, &test_lock );
19 pthread_mutex_unlock(&test_lock);
20 printf("Thread 1 done.\n");
25 thread2_func(void* arg)
27 printf("Thread 2 (arg=%p tid=%d) entered.\n", arg, gettid());
28 printf("2 waiting for cond2\n");
29 pthread_mutex_lock(&test_lock);
30 pthread_cond_wait(&cond2, &test_lock );
31 pthread_mutex_unlock(&test_lock);
33 printf("Thread 2 done.\n");
38 thread3_func(void* arg)
40 printf("Thread 3 (arg=%p tid=%d) entered.\n", arg, gettid());
41 printf("3 waiting for cond1\n");
42 pthread_mutex_lock(&test_lock);
43 pthread_cond_wait(&cond1, &test_lock );
44 pthread_mutex_unlock(&test_lock);
45 printf("3 Sleeping\n");
47 printf("3 signal cond2\n");
48 pthread_cond_signal(&cond2);
50 printf("Thread 3 done.\n");
55 thread4_func(void* arg)
57 printf("Thread 4 (arg=%p tid=%d) entered.\n", arg, gettid());
58 printf("4 Sleeping\n");
61 printf("4 broadcast cond1\n");
62 pthread_cond_broadcast(&cond1);
63 printf("Thread 4 done.\n");
67 int main(int argc __unused, const char *argv[] __unused)
71 pthread_cond_init(&cond1, NULL);
72 pthread_cond_init(&cond2, NULL);
73 pthread_create( &t[0], NULL, thread1_func, (void *)1 );
74 pthread_create( &t[1], NULL, thread2_func, (void *)2 );
75 pthread_create( &t[2], NULL, thread3_func, (void *)3 );
76 pthread_create( &t[3], NULL, thread4_func, (void *)4 );
78 pthread_join(t[0], NULL);
79 pthread_join(t[1], NULL);
80 pthread_join(t[2], NULL);
81 pthread_join(t[3], NULL);