| 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: condvar.cvl
|
|---|
| 8 | * DESCRIPTION:
|
|---|
| 9 | * Example code for using Pthreads condition variables. The main thread
|
|---|
| 10 | * creates three threads. Two of those threads increment a "count" variable,
|
|---|
| 11 | * while the third thread watches the value of "count". When "count"
|
|---|
| 12 | * reaches a predefined limit, the waiting thread is signaled by one of the
|
|---|
| 13 | * incrementing threads. The waiting thread "awakens" and then modifies
|
|---|
| 14 | * count. The program continues until the incrementing threads reach
|
|---|
| 15 | * TCOUNT. The main program prints the final value of count.
|
|---|
| 16 | * Command line execution:
|
|---|
| 17 | * civl verify -inputNUM_THREADS=3 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 condvar.cvl
|
|---|
| 18 | ******************************************************************************/
|
|---|
| 19 | #include "pthread.cvh"
|
|---|
| 20 | #include <civlc.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <stdlib.h>
|
|---|
| 23 |
|
|---|
| 24 | $input int NUM_THREADS;
|
|---|
| 25 | $input int TCOUNT;
|
|---|
| 26 | $input int COUNT_LIMIT;
|
|---|
| 27 |
|
|---|
| 28 | int count = 0;
|
|---|
| 29 | pthread_mutex_t count_mutex;
|
|---|
| 30 | pthread_cond_t count_threshold_cv;
|
|---|
| 31 |
|
|---|
| 32 | void *inc_count(void *t)
|
|---|
| 33 | {
|
|---|
| 34 | int i;
|
|---|
| 35 | long my_id = (long)*t;
|
|---|
| 36 |
|
|---|
| 37 | for (i=0; i < TCOUNT; i++) {
|
|---|
| 38 | pthread_mutex_lock(&count_mutex);
|
|---|
| 39 | count++;
|
|---|
| 40 |
|
|---|
| 41 | /*
|
|---|
| 42 | Check the value of count and signal waiting thread when condition is
|
|---|
| 43 | reached. Note that this occurs while mutex is locked.
|
|---|
| 44 | */
|
|---|
| 45 | if (count == COUNT_LIMIT) {
|
|---|
| 46 | printf("inc_count(): thread %d, count = %d Threshold reached. ",
|
|---|
| 47 | my_id, count);
|
|---|
| 48 | pthread_cond_signal(&count_threshold_cv);
|
|---|
| 49 | printf("Just sent signal.\n");
|
|---|
| 50 | }
|
|---|
| 51 | printf("inc_count(): thread %d, count = %d, unlocking mutex\n",
|
|---|
| 52 | my_id, count);
|
|---|
| 53 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 54 |
|
|---|
| 55 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 56 | //sleep(1);
|
|---|
| 57 | }
|
|---|
| 58 | pthread_exit(NULL);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void *watch_count(void *t)
|
|---|
| 62 | {
|
|---|
| 63 | long my_id = (long)*t;
|
|---|
| 64 |
|
|---|
| 65 | printf("Starting watch_count(): thread %d\n", my_id);
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 69 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 70 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 71 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 72 | from never returning.
|
|---|
| 73 | */
|
|---|
| 74 | pthread_mutex_lock(&count_mutex);
|
|---|
| 75 | while (count < COUNT_LIMIT) {
|
|---|
| 76 | printf("watch_count(): thread %d Count= %d. Going into wait...\n", my_id,count);
|
|---|
| 77 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 78 | printf("watch_count(): thread %d Condition signal received. Count= %d\n", my_id,count);
|
|---|
| 79 | printf("watch_count(): thread %d Updating the value of count...\n", my_id,count);
|
|---|
| 80 | count += 125;
|
|---|
| 81 | printf("watch_count(): thread %d count now = %d.\n", my_id, count);
|
|---|
| 82 | }
|
|---|
| 83 | printf("watch_count(): thread %d Unlocking mutex.\n", my_id);
|
|---|
| 84 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 85 | pthread_exit(NULL);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | int main(void)
|
|---|
| 89 | {
|
|---|
| 90 | int i, rc;
|
|---|
| 91 | long t1=1, t2=2, t3=3;
|
|---|
| 92 | pthread_t threads[3];
|
|---|
| 93 | pthread_attr_t attr;
|
|---|
| 94 |
|
|---|
| 95 | /* Initialize mutex and condition variable objects */
|
|---|
| 96 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 97 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 98 |
|
|---|
| 99 | /* For portability, explicitly create threads in a joinable state */
|
|---|
| 100 | pthread_attr_init(&attr);
|
|---|
| 101 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 102 | pthread_create(&threads[0], &attr, watch_count, (void *)&t1);
|
|---|
| 103 | pthread_create(&threads[1], &attr, inc_count, (void *)&t2);
|
|---|
| 104 | pthread_create(&threads[2], &attr, inc_count, (void *)&t3);
|
|---|
| 105 |
|
|---|
| 106 | /* Wait for all threads to complete */
|
|---|
| 107 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 108 | pthread_join(threads[i], NULL);
|
|---|
| 109 | }
|
|---|
| 110 | printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
|
|---|
| 111 | NUM_THREADS, count);
|
|---|
| 112 |
|
|---|
| 113 | /* Clean up and exit */
|
|---|
| 114 | pthread_attr_destroy(&attr);
|
|---|
| 115 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 116 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 117 | pthread_exit (NULL);
|
|---|
| 118 | return 0;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|