1 /* $NetBSD: cond4.c,v 1.1 2003/01/30 18:53:48 thorpej Exp $ */
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
;
16 int main(int argc
, char *argv
[])
23 printf("1: condition variable test 4\n");
25 ret
= pthread_mutex_lock(&mutex
);
27 err(1, "pthread_mutex_lock(1)");
32 ret
= pthread_create(&new, NULL
, threadfunc
, &sharedval
);
34 err(1, "pthread_create");
36 printf("1: Before waiting.\n");
42 printf("1: Before signal %d.\n", count
);
44 pthread_cond_signal(&cond
);
46 pthread_cond_wait(&cond
, &mutex
);
47 } while (toggle
!= 0);
49 printf("1: After the loop.\n");
52 pthread_mutex_unlock(&mutex
);
53 pthread_cond_signal(&cond
);
55 printf("1: After releasing the mutex.\n");
56 ret
= pthread_join(new, &joinval
);
58 err(1, "pthread_join");
60 printf("1: Thread joined. Final count = %d, total = %d\n",
63 assert(total
== COUNT
);
68 void *threadfunc(void *arg
)
71 int *share
= (int *) arg
;
74 printf("2: Second thread.\n");
75 pthread_mutex_lock(&mutex
);
76 printf("2: Before the loop.\n");
82 printf("2: Before signal %d.\n", count
);
84 pthread_cond_signal(&cond
);
86 pthread_cond_wait(&cond
, &mutex
);
87 } while (toggle
!= 1);
89 printf("2: After the loop.\n");
90 pthread_mutex_unlock(&mutex
);