| 1 | /*****************************************************************************
|
|---|
| 2 | * SOURCE: This is a translation of a Pthread program from the Lawrence Livermore
|
|---|
| 3 | * Computing Center POSIX Threads Programming Exercise at:
|
|---|
| 4 | * https://computing.llnl.gov/tutorials/pthreads/exercise.html
|
|---|
| 5 | * SUPERSOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
|---|
| 6 | * et al. O'Reilly and Associates.
|
|---|
| 7 | * FILE: bug4.cvl
|
|---|
| 8 | * DESCRIPTION:
|
|---|
| 9 | * This program demonstrates a condition variable race/synchronization
|
|---|
| 10 | * problem. It resembles the condvar.cvl program. One possible solution can
|
|---|
| 11 | * be found in bug4fix.cvl
|
|---|
| 12 | * Command line execution:
|
|---|
| 13 | * civl verify -inputNUM_THREADS=3 -inputITERATIONS=10 -inputTHRESHOLD=12 bug4.cvl
|
|---|
| 14 | ******************************************************************************/
|
|---|
| 15 |
|
|---|
| 16 | #include "pthread.cvh"
|
|---|
| 17 | #include <civlc.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include "math.cvh"
|
|---|
| 21 |
|
|---|
| 22 | /* Define and scope what needs to be seen by everyone */
|
|---|
| 23 | $input int NUM_THREADS;
|
|---|
| 24 | $input int ITERATIONS;
|
|---|
| 25 | $input int THRESHOLD;
|
|---|
| 26 |
|
|---|
| 27 | int count = 0;
|
|---|
| 28 | double finalresult=0.0;
|
|---|
| 29 | pthread_mutex_t count_mutex;
|
|---|
| 30 | pthread_cond_t count_condvar;
|
|---|
| 31 |
|
|---|
| 32 | void *sub1(void *t)
|
|---|
| 33 | {
|
|---|
| 34 | int i;
|
|---|
| 35 | long tid = (long)*t;
|
|---|
| 36 | double myresult=0.0;
|
|---|
| 37 |
|
|---|
| 38 | /* do some work */
|
|---|
| 39 | //sleep(1); Removed, unnecessary
|
|---|
| 40 |
|
|---|
| 41 | /*
|
|---|
| 42 | Lock mutex and wait for signal only if count is what is expected. Note
|
|---|
| 43 | that the pthread_cond_wait routine will automatically and atomically
|
|---|
| 44 | unlock mutex while it waits. Also, note that if THRESHOLD is reached
|
|---|
| 45 | before this routine is run by the waiting thread, the loop will be skipped
|
|---|
| 46 | to prevent pthread_cond_wait from never returning, and that this thread's
|
|---|
| 47 | work is now done within the mutex lock of count.
|
|---|
| 48 | */
|
|---|
| 49 | pthread_mutex_lock(&count_mutex);
|
|---|
| 50 | printf("sub1: thread=%d going into wait. count=%d\n",tid,count); // Removed l from %ld
|
|---|
| 51 | pthread_cond_wait(&count_condvar, &count_mutex);
|
|---|
| 52 | printf("sub1: thread=%d Condition variable signal received.",tid); // Removed l from %ld
|
|---|
| 53 | printf(" count=%d\n",count);
|
|---|
| 54 | count++;
|
|---|
| 55 | finalresult += myresult;
|
|---|
| 56 | printf("sub1: thread=%d count now equals=%d myresult=%e. Done.\n",tid,count,myresult); // Removed l from %ld
|
|---|
| 57 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 58 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void *sub2(void *t)
|
|---|
| 62 | {
|
|---|
| 63 | int j,i;
|
|---|
| 64 | long tid = (long)*t;
|
|---|
| 65 | double myresult=0.0;
|
|---|
| 66 |
|
|---|
| 67 | for (i=0; i<ITERATIONS; i++) {
|
|---|
| 68 | for (j=0; j<10; j++)
|
|---|
| 69 | myresult += sin(j) * tan(i);
|
|---|
| 70 | pthread_mutex_lock(&count_mutex);
|
|---|
| 71 | finalresult += myresult;
|
|---|
| 72 | count++;
|
|---|
| 73 |
|
|---|
| 74 | /*
|
|---|
| 75 | Check the value of count and signal waiting thread when condition is
|
|---|
| 76 | reached. Note that this occurs while mutex is locked.
|
|---|
| 77 | */
|
|---|
| 78 | if (count == THRESHOLD) {
|
|---|
| 79 | printf("sub2: thread=%d Threshold reached. count=%d. ",tid,count); // Removed l from %ld
|
|---|
| 80 | pthread_cond_signal(&count_condvar);
|
|---|
| 81 | printf("Just sent signal.\n");
|
|---|
| 82 | }
|
|---|
| 83 | else {
|
|---|
| 84 | printf("sub2: thread=%d did work. count=%d\n",tid,count); // Removed l from %ld
|
|---|
| 85 | }
|
|---|
| 86 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 87 | }
|
|---|
| 88 | printf("sub2: thread=%d myresult=%e. Done. \n",tid,myresult); // Removed l from %ld
|
|---|
| 89 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | int main(void)
|
|---|
| 93 | {
|
|---|
| 94 | long t1=1, t2=2, t3=3;
|
|---|
| 95 | int i, rc;
|
|---|
| 96 | pthread_t threads[3];
|
|---|
| 97 | pthread_attr_t attr;
|
|---|
| 98 |
|
|---|
| 99 | /* Initialize mutex and condition variable objects */
|
|---|
| 100 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 101 | pthread_cond_init (&count_condvar, NULL);
|
|---|
| 102 |
|
|---|
| 103 | /* For portability, explicitly create threads in a joinable state */
|
|---|
| 104 | pthread_attr_init(&attr);
|
|---|
| 105 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 106 | pthread_create(&threads[0], &attr, sub1, (void *)&t1); //Pointers used as conversion from int to void* is not supported
|
|---|
| 107 | pthread_create(&threads[1], &attr, sub2, (void *)&t2);
|
|---|
| 108 | pthread_create(&threads[2], &attr, sub2, (void *)&t3);
|
|---|
| 109 |
|
|---|
| 110 | /* Wait for all threads to complete */
|
|---|
| 111 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 112 | pthread_join(threads[i], NULL);
|
|---|
| 113 | }
|
|---|
| 114 | printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
|
|---|
| 115 | NUM_THREADS,finalresult);
|
|---|
| 116 |
|
|---|
| 117 | /* Clean up and exit */
|
|---|
| 118 | pthread_attr_destroy(&attr);
|
|---|
| 119 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 120 | pthread_cond_destroy(&count_condvar);
|
|---|
| 121 | pthread_exit (NULL, true, NULL, 0); //Different parameters
|
|---|
| 122 |
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|