| [506de9d] | 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>
|
|---|
| [d23db14] | 13 | #include <math.h>
|
|---|
| [afa100f] | 14 | #include <unistd.h>
|
|---|
| [506de9d] | 15 |
|
|---|
| 16 | /* Define and scope what needs to be seen by everyone */
|
|---|
| 17 | #define NUM_THREADS 3
|
|---|
| [d23db14] | 18 | #define ITERATIONS 3
|
|---|
| 19 | #define THRESHOLD 6
|
|---|
| [506de9d] | 20 | int count = 0;
|
|---|
| 21 | double finalresult=0.0;
|
|---|
| 22 | pthread_mutex_t count_mutex;
|
|---|
| 23 | pthread_cond_t count_condvar;
|
|---|
| 24 |
|
|---|
| 25 | void *sub1(void *t)
|
|---|
| 26 | {
|
|---|
| 27 | int i;
|
|---|
| 28 | long tid = (long)t;
|
|---|
| 29 | double myresult=0.0;
|
|---|
| 30 |
|
|---|
| 31 | /* do some work */
|
|---|
| 32 | sleep(1);
|
|---|
| 33 | /*
|
|---|
| 34 | Lock mutex and wait for signal only if count is what is expected. Note
|
|---|
| 35 | that the pthread_cond_wait routine will automatically and atomically
|
|---|
| 36 | unlock mutex while it waits. Also, note that if THRESHOLD is reached
|
|---|
| 37 | before this routine is run by the waiting thread, the loop will be skipped
|
|---|
| 38 | to prevent pthread_cond_wait from never returning, and that this thread's
|
|---|
| 39 | work is now done within the mutex lock of count.
|
|---|
| 40 | */
|
|---|
| 41 | pthread_mutex_lock(&count_mutex);
|
|---|
| 42 | printf("sub1: thread=%ld going into wait. count=%d\n",tid,count);
|
|---|
| 43 | pthread_cond_wait(&count_condvar, &count_mutex);
|
|---|
| 44 | printf("sub1: thread=%ld Condition variable signal received.",tid);
|
|---|
| 45 | printf(" count=%d\n",count);
|
|---|
| 46 | count++;
|
|---|
| 47 | finalresult += myresult;
|
|---|
| 48 | printf("sub1: thread=%ld count now equals=%d myresult=%e. Done.\n",
|
|---|
| 49 | tid,count,myresult);
|
|---|
| 50 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 51 | pthread_exit(NULL);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void *sub2(void *t)
|
|---|
| 55 | {
|
|---|
| 56 | int j,i;
|
|---|
| 57 | long tid = (long)t;
|
|---|
| 58 | double myresult=0.0;
|
|---|
| 59 |
|
|---|
| 60 | for (i=0; i<ITERATIONS; i++) {
|
|---|
| [d23db14] | 61 | for (j=0; j<5; j++)
|
|---|
| [506de9d] | 62 | myresult += sin(j) * tan(i);
|
|---|
| 63 | pthread_mutex_lock(&count_mutex);
|
|---|
| 64 | finalresult += myresult;
|
|---|
| 65 | count++;
|
|---|
| 66 | /*
|
|---|
| 67 | Check the value of count and signal waiting thread when condition is
|
|---|
| 68 | reached. Note that this occurs while mutex is locked.
|
|---|
| 69 | */
|
|---|
| 70 | if (count == THRESHOLD) {
|
|---|
| 71 | printf("sub2: thread=%ld Threshold reached. count=%d. ",tid,count);
|
|---|
| 72 | pthread_cond_signal(&count_condvar);
|
|---|
| 73 | printf("Just sent signal.\n");
|
|---|
| 74 | }
|
|---|
| 75 | else {
|
|---|
| 76 | printf("sub2: thread=%ld did work. count=%d\n",tid,count);
|
|---|
| 77 | }
|
|---|
| 78 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 79 | }
|
|---|
| 80 | printf("sub2: thread=%ld myresult=%e. Done. \n",tid,myresult);
|
|---|
| 81 | pthread_exit(NULL);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | int main(int argc, char *argv[])
|
|---|
| 85 | {
|
|---|
| 86 | long t1=1, t2=2, t3=3;
|
|---|
| 87 | int i, rc;
|
|---|
| 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_condvar, 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, sub1, (void *)t1);
|
|---|
| 99 | pthread_create(&threads[1], &attr, sub2, (void *)t2);
|
|---|
| 100 | pthread_create(&threads[2], &attr, sub2, (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 on %d threads. Final result=%e. Done.\n",
|
|---|
| 107 | NUM_THREADS,finalresult);
|
|---|
| 108 |
|
|---|
| 109 | /* Clean up and exit */
|
|---|
| 110 | pthread_attr_destroy(&attr);
|
|---|
| 111 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 112 | pthread_cond_destroy(&count_condvar);
|
|---|
| 113 | pthread_exit (NULL);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|