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