Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libpthread / cond4 / cond4.c
blob7395f45d17b340acd9e313f9ca5d1ce7ec31b607
1 /* $NetBSD: cond4.c,v 1.1 2003/01/30 18:53:48 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(int argc, char *argv[])
18 int ret;
19 pthread_t new;
20 void *joinval;
21 int sharedval;
23 printf("1: condition variable test 4\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 #if 0
42 printf("1: Before signal %d.\n", count);
43 #endif
44 pthread_cond_signal(&cond);
45 do {
46 pthread_cond_wait(&cond, &mutex);
47 } while (toggle != 0);
49 printf("1: After the loop.\n");
51 toggle = 1;
52 pthread_mutex_unlock(&mutex);
53 pthread_cond_signal(&cond);
55 printf("1: After releasing the mutex.\n");
56 ret = pthread_join(new, &joinval);
57 if (ret != 0)
58 err(1, "pthread_join");
60 printf("1: Thread joined. Final count = %d, total = %d\n",
61 count, total);
62 assert(count == 0);
63 assert(total == COUNT);
65 return 0;
68 void *threadfunc(void *arg)
70 #if 0
71 int *share = (int *) arg;
72 #endif
74 printf("2: Second thread.\n");
75 pthread_mutex_lock(&mutex);
76 printf("2: Before the loop.\n");
77 while (count>0) {
78 count--;
79 total++;
80 toggle = 0;
81 #if 0
82 printf("2: Before signal %d.\n", count);
83 #endif
84 pthread_cond_signal(&cond);
85 do {
86 pthread_cond_wait(&cond, &mutex);
87 } while (toggle != 1);
89 printf("2: After the loop.\n");
90 pthread_mutex_unlock(&mutex);
92 return NULL;