| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: bug4.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This program demonstrates a condition variable race/synchronization
|
|---|
| 5 | * problem. It resembles the condvar.c program. One possible solution can
|
|---|
| 6 | * be found in bug4fix.c
|
|---|
| 7 | * SOURCE: 07/06/05 Blaise Barney
|
|---|
| 8 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 9 | ******************************************************************************/
|
|---|
| 10 | #include <pthread.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #include <math.h>
|
|---|
| 14 |
|
|---|
| 15 | /* Define and scope what needs to be seen by everyone */
|
|---|
| 16 | #define NUM_THREADS 3
|
|---|
| 17 | #define ITERATIONS 3
|
|---|
| 18 | #define THRESHOLD 6
|
|---|
| 19 | int count = 0;
|
|---|
| 20 | double finalresult=0.0;
|
|---|
| 21 | pthread_mutex_t count_mutex;
|
|---|
| 22 | pthread_cond_t count_condvar;
|
|---|
| 23 |
|
|---|
| 24 | void *sub1(void *t)
|
|---|
| 25 | {
|
|---|
| 26 | int i;
|
|---|
| 27 | long tid = (long)t;
|
|---|
| 28 | double myresult=0.0;
|
|---|
| 29 |
|
|---|
| 30 | /* do some work */
|
|---|
| 31 | sleep(1);
|
|---|
| 32 | /*
|
|---|
| 33 | Lock mutex and wait for signal only if count is what is expected. Note
|
|---|
| 34 | that the pthread_cond_wait routine will automatically and atomically
|
|---|
| 35 | unlock mutex while it waits. Also, note that if THRESHOLD is reached
|
|---|
| 36 | before this routine is run by the waiting thread, the loop will be skipped
|
|---|
| 37 | to prevent pthread_cond_wait from never returning, and that this thread's
|
|---|
| 38 | work is now done within the mutex lock of count.
|
|---|
| 39 | */
|
|---|
| 40 | pthread_mutex_lock(&count_mutex);
|
|---|
| 41 | printf("sub1: thread=%ld going into wait. count=%d\n",tid,count);
|
|---|
| 42 | pthread_cond_wait(&count_condvar, &count_mutex);
|
|---|
| 43 | printf("sub1: thread=%ld Condition variable signal received.",tid);
|
|---|
| 44 | printf(" count=%d\n",count);
|
|---|
| 45 | count++;
|
|---|
| 46 | finalresult += myresult;
|
|---|
| 47 | printf("sub1: thread=%ld count now equals=%d myresult=%e. Done.\n",
|
|---|
| 48 | tid,count,myresult);
|
|---|
| 49 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 50 | pthread_exit(NULL);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void *sub2(void *t)
|
|---|
| 54 | {
|
|---|
| 55 | int j,i;
|
|---|
| 56 | long tid = (long)t;
|
|---|
| 57 | double myresult=0.0;
|
|---|
| 58 |
|
|---|
| 59 | for (i=0; i<ITERATIONS; i++) {
|
|---|
| 60 | for (j=0; j<5; j++)
|
|---|
| 61 | myresult += sin(j) * tan(i);
|
|---|
| 62 | pthread_mutex_lock(&count_mutex);
|
|---|
| 63 | finalresult += myresult;
|
|---|
| 64 | count++;
|
|---|
| 65 | /*
|
|---|
| 66 | Check the value of count and signal waiting thread when condition is
|
|---|
| 67 | reached. Note that this occurs while mutex is locked.
|
|---|
| 68 | */
|
|---|
| 69 | if (count == THRESHOLD) {
|
|---|
| 70 | printf("sub2: thread=%ld Threshold reached. count=%d. ",tid,count);
|
|---|
| 71 | pthread_cond_signal(&count_condvar);
|
|---|
| 72 | printf("Just sent signal.\n");
|
|---|
| 73 | }
|
|---|
| 74 | else {
|
|---|
| 75 | printf("sub2: thread=%ld did work. count=%d\n",tid,count);
|
|---|
| 76 | }
|
|---|
| 77 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 78 | }
|
|---|
| 79 | printf("sub2: thread=%ld myresult=%e. Done. \n",tid,myresult);
|
|---|
| 80 | pthread_exit(NULL);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int main(int argc, char *argv[])
|
|---|
| 84 | {
|
|---|
| 85 | long t1=1, t2=2, t3=3;
|
|---|
| 86 | int i, rc;
|
|---|
| 87 | pthread_t threads[3];
|
|---|
| 88 | pthread_attr_t attr;
|
|---|
| 89 |
|
|---|
| 90 | /* Initialize mutex and condition variable objects */
|
|---|
| 91 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 92 | pthread_cond_init (&count_condvar, NULL);
|
|---|
| 93 |
|
|---|
| 94 | /* For portability, explicitly create threads in a joinable state */
|
|---|
| 95 | pthread_attr_init(&attr);
|
|---|
| 96 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 97 | pthread_create(&threads[0], &attr, sub1, (void *)t1);
|
|---|
| 98 | pthread_create(&threads[1], &attr, sub2, (void *)t2);
|
|---|
| 99 | pthread_create(&threads[2], &attr, sub2, (void *)t3);
|
|---|
| 100 |
|
|---|
| 101 | /* Wait for all threads to complete */
|
|---|
| 102 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 103 | pthread_join(threads[i], NULL);
|
|---|
| 104 | }
|
|---|
| 105 | printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
|
|---|
| 106 | NUM_THREADS,finalresult);
|
|---|
| 107 |
|
|---|
| 108 | /* Clean up and exit */
|
|---|
| 109 | pthread_attr_destroy(&attr);
|
|---|
| 110 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 111 | pthread_cond_destroy(&count_condvar);
|
|---|
| 112 | pthread_exit (NULL);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|