| 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.h>
|
|---|
| 13 | #include <stdio.h>
|
|---|
| 14 | #include <stdlib.h>
|
|---|
| 15 |
|
|---|
| 16 | #define NUM_THREADS 6
|
|---|
| 17 | #define TCOUNT 10
|
|---|
| 18 | #define COUNT_LIMIT 12
|
|---|
| 19 |
|
|---|
| 20 | int count = 0;
|
|---|
| 21 | pthread_mutex_t count_mutex;
|
|---|
| 22 | pthread_cond_t count_threshold_cv;
|
|---|
| 23 |
|
|---|
| 24 | void *inc_count(void *idp)
|
|---|
| 25 | {
|
|---|
| 26 | int j,i;
|
|---|
| 27 | double result=0.0;
|
|---|
| 28 | long my_id = (long)idp;
|
|---|
| 29 | for (i=0; i < TCOUNT; i++) {
|
|---|
| 30 | pthread_mutex_lock(&count_mutex);
|
|---|
| 31 | count++;
|
|---|
| 32 |
|
|---|
| 33 | /*
|
|---|
| 34 | Check the value of count and signal waiting thread when condition is
|
|---|
| 35 | reached. Note that this occurs while mutex is locked.
|
|---|
| 36 | */
|
|---|
| 37 | if (count == COUNT_LIMIT) {
|
|---|
| 38 | pthread_cond_signal(&count_threshold_cv);
|
|---|
| 39 | printf("inc_count(): thread %ld, count = %d Threshold reached.\n", my_id, count);
|
|---|
| 40 | }
|
|---|
| 41 | printf("inc_count(): thread %ld, count = %d, unlocking mutex\n", my_id, count);
|
|---|
| 42 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 43 |
|
|---|
| 44 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 45 | sleep(1);
|
|---|
| 46 | }
|
|---|
| 47 | pthread_exit(NULL);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void *watch_count(void *idp)
|
|---|
| 51 | {
|
|---|
| 52 | long my_id = (long)idp;
|
|---|
| 53 |
|
|---|
| 54 | printf("Starting watch_count(): thread %ld\n", my_id);
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 58 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 59 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 60 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 61 | from never returning.
|
|---|
| 62 | */
|
|---|
| 63 | pthread_mutex_lock(&count_mutex);
|
|---|
| 64 | printf("***Before cond_wait: thread %ld\n", my_id);
|
|---|
| 65 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 66 | printf("***Thread %ld Condition signal received.\n", my_id);
|
|---|
| 67 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 68 | pthread_exit(NULL);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | int main(int argc, char *argv[])
|
|---|
| 72 | {
|
|---|
| 73 | int i, rc;
|
|---|
| 74 | pthread_t threads[6];
|
|---|
| 75 | pthread_attr_t attr;
|
|---|
| 76 |
|
|---|
| 77 | /* Initialize mutex and condition variable objects */
|
|---|
| 78 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 79 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 80 |
|
|---|
| 81 | /*
|
|---|
| 82 | For portability, explicitly create threads in a joinable state
|
|---|
| 83 | */
|
|---|
| 84 | pthread_attr_init(&attr);
|
|---|
| 85 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 86 | pthread_create(&threads[2], &attr, watch_count, (void *)2);
|
|---|
| 87 | pthread_create(&threads[3], &attr, watch_count, (void *)3);
|
|---|
| 88 | pthread_create(&threads[4], &attr, watch_count, (void *)4);
|
|---|
| 89 | pthread_create(&threads[5], &attr, watch_count, (void *)5);
|
|---|
| 90 | pthread_create(&threads[0], &attr, inc_count, (void *)0);
|
|---|
| 91 | pthread_create(&threads[1], &attr, inc_count, (void *)1);
|
|---|
| 92 |
|
|---|
| 93 | /* Wait for all threads to complete */
|
|---|
| 94 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 95 | pthread_join(threads[i], NULL);
|
|---|
| 96 | }
|
|---|
| 97 | printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
|
|---|
| 98 |
|
|---|
| 99 | /* Clean up and exit */
|
|---|
| 100 | pthread_attr_destroy(&attr);
|
|---|
| 101 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 102 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 103 | pthread_exit (NULL);
|
|---|
| 104 |
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|