Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libpthread / cond2 / cond2.c
blob45d1e3dd66ee86801525531304e307b43ecd5b2c
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;
12 pthread_cond_t cond;
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 2\n");
24 pthread_mutex_init(&mutex, NULL);
25 pthread_cond_init(&cond, NULL);
27 x = 20;
28 pthread_mutex_lock(&mutex);
30 sharedval = 1;
32 ret = pthread_create(&new, NULL, threadfunc, &sharedval);
33 if (ret != 0)
34 err(1, "pthread_create");
36 printf("1: Before waiting.\n");
37 do {
38 sleep(2);
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);
49 if (ret != 0)
50 err(1, "pthread_join");
52 printf("1: Thread joined.\n");
54 return 0;
57 void *
58 threadfunc(void *arg)
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);
68 *share = 0;
70 /* Signal first, then unlock, for a different test than #1. */
71 pthread_cond_signal(&cond);
72 pthread_mutex_unlock(&mutex);
74 return NULL;