9 void *threadfunc(void *arg
);
11 pthread_mutex_t mutex
;
15 main(int argc
, char *argv
[])
22 printf("1: condition variable test 1\n");
24 pthread_mutex_init(&mutex
, NULL
);
25 pthread_cond_init(&cond
, NULL
);
28 pthread_mutex_lock(&mutex
);
32 ret
= pthread_create(&new, NULL
, threadfunc
, &sharedval
);
34 err(1, "pthread_create");
36 printf("1: Before waiting.\n");
39 pthread_cond_wait(&cond
, &mutex
);
40 printf("1: After waiting, in loop.\n");
41 } while (sharedval
!= 0);
43 printf("1: After the loop.\n");
45 pthread_mutex_unlock(&mutex
);
47 printf("1: After releasing the mutex.\n");
48 ret
= pthread_join(new, &joinval
);
50 err(1, "pthread_join");
52 printf("1: Thread joined.\n");
60 int *share
= (int *) arg
;
62 printf("2: Second thread.\n");
64 printf("2: Locking mutex\n");
65 pthread_mutex_lock(&mutex
);
66 printf("2: Got mutex.\n");
67 printf("Shared value: %d. Changing to 0.\n", *share
);
70 pthread_mutex_unlock(&mutex
);
71 pthread_cond_signal(&cond
);