| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: bug1.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This example has a bug. It is a variation on the condvar.c example.
|
|---|
| 5 | * Instead of just one thread waiting for the condition signal, there are
|
|---|
| 6 | * four threads waiting for the same signal. Find out how to fix the
|
|---|
| 7 | * program. The solution program is bug1fix.c.
|
|---|
| 8 | * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
|---|
| 9 | * et al. O'Reilly and Associates.
|
|---|
| 10 | * LAST REVISED: 07/06/05 Blaise Barney
|
|---|
| 11 | ******************************************************************************/
|
|---|
| 12 | #include "pthread.cvh"
|
|---|
| 13 | #include <civlc.h>
|
|---|
| 14 | #include <stdio.h>
|
|---|
| 15 | #include <stdlib.h>
|
|---|
| 16 |
|
|---|
| 17 | #define NUM_THREADS 6
|
|---|
| 18 | #define TCOUNT 10
|
|---|
| 19 | #define COUNT_LIMIT 12
|
|---|
| 20 |
|
|---|
| 21 | int count = 0;
|
|---|
| 22 | pthread_mutex_t count_mutex;
|
|---|
| 23 | pthread_cond_t count_threshold_cv;
|
|---|
| 24 |
|
|---|
| 25 | void *inc_count(void *idp)
|
|---|
| 26 | {
|
|---|
| 27 | int j,i;
|
|---|
| 28 | double result=0.0;
|
|---|
| 29 | long my_id = (long)*idp;
|
|---|
| 30 | for (i=0; i < TCOUNT; i++) {
|
|---|
| 31 | pthread_mutex_lock(&count_mutex);
|
|---|
| 32 | count++;
|
|---|
| 33 |
|
|---|
| 34 | /*
|
|---|
| 35 | Check the value of count and signal waiting thread when condition is
|
|---|
| 36 | reached. Note that this occurs while mutex is locked.
|
|---|
| 37 | */
|
|---|
| 38 | if (count == COUNT_LIMIT) {
|
|---|
| 39 | pthread_cond_signal(&count_threshold_cv);
|
|---|
| 40 | printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count);
|
|---|
| 41 | }
|
|---|
| 42 | printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count);
|
|---|
| 43 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 44 |
|
|---|
| 45 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 46 | //sleep(1);
|
|---|
| 47 | }
|
|---|
| 48 | pthread_exit(NULL);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | void *watch_count(void *idp)
|
|---|
| 52 | {
|
|---|
| 53 | long my_id = (long)*idp;
|
|---|
| 54 |
|
|---|
| 55 | printf("Starting watch_count(): thread %d\n", my_id);
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 59 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 60 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 61 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 62 | from never returning.
|
|---|
| 63 | */
|
|---|
| 64 | pthread_mutex_lock(&count_mutex);
|
|---|
| 65 | printf("***Before cond_wait: thread %d\n", my_id);
|
|---|
| 66 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 67 | printf("***Thread %d Condition signal received.\n", my_id);
|
|---|
| 68 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 69 | pthread_exit(NULL);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int main(int argc, char *argv[])
|
|---|
| 73 | {
|
|---|
| 74 | int i, rc;
|
|---|
| 75 | pthread_t threads[6];
|
|---|
| 76 | pthread_attr_t attr;
|
|---|
| 77 |
|
|---|
| 78 | /* Initialize mutex and condition variable objects */
|
|---|
| 79 | pthread_mutex_init(&count_mutex, 0);
|
|---|
| 80 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 81 |
|
|---|
| 82 | /*
|
|---|
| 83 | For portability, explicitly create threads in a joinable state
|
|---|
| 84 | */
|
|---|
| 85 | int zero = 0;
|
|---|
| 86 | int one = 1;
|
|---|
| 87 | int two = 2;
|
|---|
| 88 | int three = 3;
|
|---|
| 89 | int four = 4;
|
|---|
| 90 | int five = 5;
|
|---|
| 91 | pthread_attr_init(&attr);
|
|---|
| 92 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 93 | pthread_create(&threads[2], &attr, watch_count, &two);
|
|---|
| 94 | pthread_create(&threads[3], &attr, watch_count, &three);
|
|---|
| 95 | pthread_create(&threads[4], &attr, watch_count, &four);
|
|---|
| 96 | pthread_create(&threads[5], &attr, watch_count, &five);
|
|---|
| 97 | pthread_create(&threads[0], &attr, inc_count, &zero);
|
|---|
| 98 | pthread_create(&threads[1], &attr, inc_count, &one);
|
|---|
| 99 |
|
|---|
| 100 | /* Wait for all threads to complete */
|
|---|
| 101 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 102 | pthread_join(threads[i], NULL);
|
|---|
| 103 | }
|
|---|
| 104 | printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
|
|---|
| 105 |
|
|---|
| 106 | /* Clean up and exit */
|
|---|
| 107 | pthread_attr_destroy(&attr);
|
|---|
| 108 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 109 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 110 | pthread_exit (NULL);
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|