Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libpthread / cond3 / cond3.c
bloba40fe6733f4aa758da10f0ace64385bb0dfcd9fb
1 /* $NetBSD$ */
3 #include <err.h>
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
9 void *threadfunc(void *arg);
11 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
14 int
15 main(int argc, char *argv[])
17 int x,ret;
18 pthread_t new;
19 void *joinval;
20 int sharedval;
22 printf("1: condition variable test 3\n");
24 x = 20;
25 pthread_mutex_lock(&mutex);
27 sharedval = 1;
29 ret = pthread_create(&new, NULL, threadfunc, &sharedval);
30 if (ret != 0)
31 err(1, "pthread_create");
33 printf("1: Before waiting.\n");
34 do {
35 sleep(2);
36 pthread_cond_wait(&cond, &mutex);
37 printf("1: After waiting, in loop.\n");
38 } while (sharedval != 0);
40 printf("1: After the loop.\n");
42 pthread_mutex_unlock(&mutex);
44 printf("1: After releasing the mutex.\n");
45 ret = pthread_join(new, &joinval);
46 if (ret != 0)
47 err(1, "pthread_join");
49 printf("1: Thread joined.\n");
51 return 0;
54 void *
55 threadfunc(void *arg)
57 int *share = (int *) arg;
59 printf("2: Second thread.\n");
61 printf("2: Locking mutex\n");
62 pthread_mutex_lock(&mutex);
63 printf("2: Got mutex.\n");
64 printf("Shared value: %d. Changing to 0.\n", *share);
65 *share = 0;
67 /* Signal first, then unlock, for a different test than #1. */
68 pthread_cond_signal(&cond);
69 pthread_mutex_unlock(&mutex);
71 return NULL;