Don't use .Xo/.Xc. Fix date format.
[netbsd-mini2440.git] / regress / lib / libpthread / cond5 / cond5.c
blob12588d615218671c4597b5951b0486f84b5ced4c
1 /* $NetBSD: cond5.c,v 1.1 2003/01/30 18:57:07 thorpej Exp $ */
3 #include <assert.h>
4 #include <err.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 void *threadfunc(void *arg);
11 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
13 int count, total, toggle;
14 #define COUNT 50000
16 int main(void)
18 int ret;
19 pthread_t new;
20 void *joinval;
21 int sharedval;
23 printf("1: condition variable test 5\n");
25 ret = pthread_mutex_lock(&mutex);
26 if (ret)
27 err(1, "pthread_mutex_lock(1)");
29 count = COUNT;
30 toggle = 0;
32 ret = pthread_create(&new, NULL, threadfunc, &sharedval);
33 if (ret != 0)
34 err(1, "pthread_create");
36 printf("1: Before waiting.\n");
37 while (count>0) {
38 count--;
39 total++;
40 toggle = 1;
41 pthread_cond_broadcast(&cond);
42 do {
43 pthread_cond_wait(&cond, &mutex);
44 } while (toggle != 0);
46 printf("1: After the loop.\n");
48 toggle = 1;
49 pthread_mutex_unlock(&mutex);
50 pthread_cond_signal(&cond);
52 printf("1: After releasing the mutex.\n");
53 ret = pthread_join(new, &joinval);
54 if (ret != 0)
55 err(1, "pthread_join");
57 printf("1: Thread joined. Final count = %d, total = %d\n",
58 count, total);
59 assert(count == 0);
60 assert(total == COUNT);
62 return 0;
65 void *
66 threadfunc(void *arg)
68 #if 0
69 int *share = (int *) arg;
70 #endif
72 printf("2: Second thread.\n");
73 pthread_mutex_lock(&mutex);
74 while (count>0) {
75 count--;
76 total++;
77 toggle = 0;
78 pthread_cond_signal(&cond);
79 do {
80 pthread_cond_wait(&cond, &mutex);
81 } while (toggle != 1);
83 printf("2: After the loop.\n");
84 pthread_mutex_unlock(&mutex);
86 return NULL;