source: CIVL/examples/pthread/condvar.cvl@ f6ce0eb

1.23 2.0 main test-branch
Last change on this file since f6ce0eb was 6fe2cd9, checked in by John Edenhofner <johneden@…>, 12 years ago

Almost done

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1040 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
20#include "pthread.cvh"
21#include <civlc.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25$input int NUM_THREADS;
26$input int TCOUNT;
27$input int COUNT_LIMIT;
28
29int count = 0;
30pthread_mutex_t count_mutex;
31pthread_cond_t count_threshold_cv;
32
33void *inc_count(void *t)
34{
35 int i;
36 long my_id = (long)*t; // Dereference rather than direct conv
37
38 for (i=0; i < TCOUNT; i++) {
39 pthread_mutex_lock(&count_mutex);
40 count++;
41
42 /*
43 Check the value of count and signal waiting thread when condition is
44 reached. Note that this occurs while mutex is locked.
45 */
46 if (count == COUNT_LIMIT) {
47 printf("inc_count(): thread %d, count = %d Threshold reached. ", 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", my_id, count);
52 pthread_mutex_unlock(&count_mutex);
53
54 /* Do some work so threads can alternate on mutex lock */
55 //sleep(1);
56 }
57 pthread_exit(NULL, false, NULL, 0); //Different parameters
58}
59
60void *watch_count(void *t)
61{
62 long my_id = (long)*t; // Dereference rather than direct conv
63
64 printf("Starting watch_count(): thread %d\n", my_id);
65
66 /*
67 Lock mutex and wait for signal. Note that the pthread_cond_wait routine
68 will automatically and atomically unlock mutex while it waits.
69 Also, note that if COUNT_LIMIT is reached before this routine is run by
70 the waiting thread, the loop will be skipped to prevent pthread_cond_wait
71 from never returning.
72 */
73 pthread_mutex_lock(&count_mutex);
74 while (count < COUNT_LIMIT) {
75 printf("watch_count(): thread %d Count= %d. Going into wait...\n", my_id,count);
76 pthread_cond_wait(&count_threshold_cv, &count_mutex);
77 printf("watch_count(): thread %d Condition signal received. Count= %d\n", my_id,count);
78 printf("watch_count(): thread %d Updating the value of count...\n", my_id,count);
79 count += 125;
80 printf("watch_count(): thread %d count now = %d.\n", my_id, count);
81 }
82 printf("watch_count(): thread %d Unlocking mutex.\n", my_id);
83 pthread_mutex_unlock(&count_mutex);
84 pthread_exit(NULL, false, NULL, 0); //Different parameters
85}
86
87int main(void)
88{
89 int i, rc;
90 long t1=1, t2=2, t3=3;
91 pthread_t threads[3];
92 pthread_attr_t attr;
93
94 /* Initialize mutex and condition variable objects */
95 pthread_mutex_init(&count_mutex, NULL);
96 pthread_cond_init (&count_threshold_cv, NULL);
97
98 /* For portability, explicitly create threads in a joinable state */
99 pthread_attr_init(&attr);
100 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
101 pthread_create(&threads[0], &attr, watch_count, (void *)&t1);
102 pthread_create(&threads[1], &attr, inc_count, (void *)&t2);
103 pthread_create(&threads[2], &attr, inc_count, (void *)&t3);
104
105 /* Wait for all threads to complete */
106 for (i = 0; i < NUM_THREADS; i++) {
107 pthread_join(threads[i], NULL);
108 }
109 printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n", NUM_THREADS, count);
110
111 /* Clean up and exit */
112 pthread_attr_destroy(&attr);
113 pthread_mutex_destroy(&count_mutex);
114 pthread_cond_destroy(&count_threshold_cv);
115 pthread_exit(NULL, true, NULL, 0); //Different parameters
116 return 0;
117}
Note: See TracBrowser for help on using the repository browser.