FreeBSD _umtx_op: fix bad wrapper for robust lists
[valgrind.git] / none / tests / pth_cvsimple.c
blobb260cfb02ac8accabf15b3438e47c46fc21f0889
1 /********************************************************
2 * An example source module to accompany...
4 * "Using POSIX Threads: Programming with Pthreads"
5 * by Brad nichols, Dick Buttlar, Jackie Farrell
6 * O'Reilly & Associates, Inc.
8 ********************************************************
10 * cvsimple.c
12 * Demonstrates pthread condvars.
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <pthread.h>
19 #define NUM_THREADS 3
20 #define TCOUNT 10
21 #define COUNT_THRES 12
23 int condvar_was_hit = 0;
24 int count = 0;
25 int thread_ids[3] = {0,1,2};
26 pthread_mutex_t count_lock=PTHREAD_MUTEX_INITIALIZER;
27 pthread_cond_t count_hit_threshold=PTHREAD_COND_INITIALIZER;
29 void *inc_count(void *null)
31 int i=0;
33 for (i=0; i<TCOUNT; i++) {
34 pthread_mutex_lock(&count_lock);
35 count++;
36 printf("inc_counter(): count = %d, unlocking mutex\n", count);
37 if (count == COUNT_THRES) {
38 printf("hit threshold!\n");
39 pthread_cond_signal(&count_hit_threshold);
41 pthread_mutex_unlock(&count_lock);
44 return(NULL);
47 void *watch_count(void *null)
49 pthread_mutex_lock(&count_lock);
51 while (count < COUNT_THRES) {
52 pthread_cond_wait(&count_hit_threshold, &count_lock);
53 condvar_was_hit = 1;
56 pthread_mutex_unlock(&count_lock);
58 return(NULL);
61 extern int
62 main(void)
64 int i;
65 pthread_t threads[3];
67 pthread_create(&threads[0], NULL, watch_count, NULL);
68 sleep(1);
69 pthread_create(&threads[1], NULL, inc_count, NULL);
70 pthread_create(&threads[2], NULL, inc_count, NULL);
72 for (i = 0; i < NUM_THREADS; i++) {
73 pthread_join(threads[i], NULL);
76 // Nb: it's not certain that we'll hit here. It's possible that the two
77 // inc_count threads could fully run before watch_count begins, and so
78 // pthread_cond_wait() is never called. Or, we could get a spurious
79 // wake-up in watch_count(). Nonetheless, it's very likely that things
80 // will work out as expected, since we're starting watch_count() first.
81 // (Also since the sleep() call was added after watch_count()!)
82 if (condvar_was_hit == 1)
83 printf("condvar was hit!\n");
84 else if (condvar_was_hit > 1)
85 printf("condvar was multi-hit...\n");
86 else
87 printf("condvar was missed...\n");
89 return 0;