sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1 | /******************************************************** |
| 2 | * An example source module to accompany... |
| 3 | * |
| 4 | * "Using POSIX Threads: Programming with Pthreads" |
| 5 | * by Brad nichols, Dick Buttlar, Jackie Farrell |
| 6 | * O'Reilly & Associates, Inc. |
| 7 | * |
| 8 | ******************************************************** |
| 9 | * |
| 10 | * cvsimple.c |
| 11 | * |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 12 | * Demonstrates pthread condvars. |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | #define NUM_THREADS 3 |
| 20 | #define TCOUNT 10 |
| 21 | #define COUNT_THRES 12 |
| 22 | |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 23 | int condvar_was_hit = 0; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 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; |
| 28 | |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 29 | void *inc_count(void *null) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 30 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 31 | int i=0; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 32 | |
| 33 | for (i=0; i<TCOUNT; i++) { |
| 34 | pthread_mutex_lock(&count_lock); |
| 35 | count++; |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 36 | printf("inc_counter(): count = %d, unlocking mutex\n", count); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 37 | if (count == COUNT_THRES) { |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 38 | printf("hit threshold!\n"); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 39 | pthread_cond_signal(&count_hit_threshold); |
| 40 | } |
| 41 | pthread_mutex_unlock(&count_lock); |
| 42 | } |
| 43 | |
| 44 | return(NULL); |
| 45 | } |
| 46 | |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 47 | void *watch_count(void *null) |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 48 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 49 | pthread_mutex_lock(&count_lock); |
| 50 | |
| 51 | while (count < COUNT_THRES) { |
| 52 | pthread_cond_wait(&count_hit_threshold, &count_lock); |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 53 | condvar_was_hit = 1; |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | pthread_mutex_unlock(&count_lock); |
| 57 | |
| 58 | return(NULL); |
| 59 | } |
| 60 | |
| 61 | extern int |
| 62 | main(void) |
| 63 | { |
| 64 | int i; |
| 65 | pthread_t threads[3]; |
| 66 | |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 67 | pthread_create(&threads[0], NULL, watch_count, NULL); |
| 68 | pthread_create(&threads[1], NULL, inc_count, NULL); |
| 69 | pthread_create(&threads[2], NULL, inc_count, NULL); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | for (i = 0; i < NUM_THREADS; i++) { |
| 72 | pthread_join(threads[i], NULL); |
| 73 | } |
| 74 | |
nethercote | ab59478 | 2004-11-09 14:58:02 +0000 | [diff] [blame] | 75 | // Nb: it's not certain that we'll hit here. It's possible that the two |
| 76 | // inc_count threads could fully run before watch_count begins, and so |
| 77 | // pthread_cond_wait() is never called. Or, we could get a spurious |
| 78 | // wake-up in watch_count(). Nonetheless, it's very likely that things |
| 79 | // will work out as expected, since we're starting watch_count() first. |
| 80 | if (condvar_was_hit == 1) |
| 81 | printf("condvar was hit!\n"); |
| 82 | else if (condvar_was_hit > 1) |
| 83 | printf("condvar was multi-hit...\n"); |
| 84 | else |
| 85 | printf("condvar was missed...\n"); |
| 86 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |