| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: condvar.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Example code for using Pthreads condition variables. The main thread
|
|---|
| 5 | * creates three threads. Two of those threads increment a "count" variable,
|
|---|
| 6 | * while the third thread watches the value of "count". When "count"
|
|---|
| 7 | * reaches a predefined limit, the waiting thread is signaled by one of the
|
|---|
| 8 | * incrementing threads. The waiting thread "awakens" and then modifies
|
|---|
| 9 | * count. The program continues until the incrementing threads reach
|
|---|
| 10 | * TCOUNT. The main program prints the final value of count.
|
|---|
| 11 | * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
|---|
| 12 | * et al. O'Reilly and Associates.
|
|---|
| 13 | * LAST REVISED: 10/14/10 Blaise Barney
|
|---|
| 14 | ******************************************************************************/
|
|---|
| 15 | #include "pthread.cvh"
|
|---|
| 16 | #include <civlc.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 |
|
|---|
| 20 | #define NUM_THREADS 3
|
|---|
| 21 | #define TCOUNT 10
|
|---|
| 22 | #define COUNT_LIMIT 12
|
|---|
| 23 |
|
|---|
| 24 | int count = 0;
|
|---|
| 25 | pthread_mutex_t count_mutex;
|
|---|
| 26 | pthread_cond_t count_threshold_cv;
|
|---|
| 27 |
|
|---|
| 28 | void *inc_count(void *t)
|
|---|
| 29 | {
|
|---|
| 30 | int i;
|
|---|
| 31 | long my_id = (long)t;
|
|---|
| 32 |
|
|---|
| 33 | for (i=0; i < TCOUNT; i++) {
|
|---|
| 34 | pthread_mutex_lock(&count_mutex);
|
|---|
| 35 | count++;
|
|---|
| 36 |
|
|---|
| 37 | /*
|
|---|
| 38 | Check the value of count and signal waiting thread when condition is
|
|---|
| 39 | reached. Note that this occurs while mutex is locked.
|
|---|
| 40 | */
|
|---|
| 41 | if (count == COUNT_LIMIT) {
|
|---|
| 42 | printf("inc_count(): thread %ld, count = %d Threshold reached. ",
|
|---|
| 43 | my_id, count);
|
|---|
| 44 | pthread_cond_signal(&count_threshold_cv);
|
|---|
| 45 | printf("Just sent signal.\n");
|
|---|
| 46 | }
|
|---|
| 47 | printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
|
|---|
| 48 | my_id, count);
|
|---|
| 49 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 50 |
|
|---|
| 51 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 52 | sleep(1);
|
|---|
| 53 | }
|
|---|
| 54 | pthread_exit(NULL);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void *watch_count(void *t)
|
|---|
| 58 | {
|
|---|
| 59 | long my_id = (long)t;
|
|---|
| 60 |
|
|---|
| 61 | printf("Starting watch_count(): thread %ld\n", my_id);
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 65 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 66 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 67 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 68 | from never returning.
|
|---|
| 69 | */
|
|---|
| 70 | pthread_mutex_lock(&count_mutex);
|
|---|
| 71 | while (count < COUNT_LIMIT) {
|
|---|
| 72 | printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
|
|---|
| 73 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 74 | printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
|
|---|
| 75 | printf("watch_count(): thread %ld Updating the value of count...\n", my_id,count);
|
|---|
| 76 | count += 125;
|
|---|
| 77 | printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
|
|---|
| 78 | }
|
|---|
| 79 | printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
|
|---|
| 80 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 81 | pthread_exit(NULL);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | int main(int argc, char *argv[])
|
|---|
| 85 | {
|
|---|
| 86 | int i, rc;
|
|---|
| 87 | long t1=1, t2=2, t3=3;
|
|---|
| 88 | pthread_t threads[3];
|
|---|
| 89 | pthread_attr_t attr;
|
|---|
| 90 |
|
|---|
| 91 | /* Initialize mutex and condition variable objects */
|
|---|
| 92 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 93 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 94 |
|
|---|
| 95 | /* For portability, explicitly create threads in a joinable state */
|
|---|
| 96 | pthread_attr_init(&attr);
|
|---|
| 97 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 98 | pthread_create(&threads[0], &attr, watch_count, (void *)t1);
|
|---|
| 99 | pthread_create(&threads[1], &attr, inc_count, (void *)t2);
|
|---|
| 100 | pthread_create(&threads[2], &attr, inc_count, (void *)t3);
|
|---|
| 101 |
|
|---|
| 102 | /* Wait for all threads to complete */
|
|---|
| 103 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 104 | pthread_join(threads[i], NULL);
|
|---|
| 105 | }
|
|---|
| 106 | printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
|
|---|
| 107 | NUM_THREADS, count);
|
|---|
| 108 |
|
|---|
| 109 | /* Clean up and exit */
|
|---|
| 110 | pthread_attr_destroy(&attr);
|
|---|
| 111 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 112 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 113 | pthread_exit (NULL);
|
|---|
| 114 |
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|