source: CIVL/examples/translation/pthread/bug4fix.cvl@ d87ec9c

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

Changed location of pthread.cvh, and updated files to use pthread.h (which includes pthread.cvh). New translations from pthread-atomic, and updated pthread.cvh

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

  • Property mode set to 100644
File size: 4.4 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: bug4fix.cvl
8* DESCRIPTION:
9* This is just one way to resolve the synchronization problem demonstrated
10* by bug4.cvl. A check is made in sub1 to make sure the pthread_cond_wait()
11* call is not made if the value of count is not what it expects. Its work is
12* also placed after it is awakened, while count is locked.
13* Command line execution:
14* civl verify -inputNUM_THREADS=3 -inputITERATIONS=10 -inputTHRESHOLD=12 bug4fix.cvl
15******************************************************************************/
16
17#include "pthread.h"
18#include <civlc.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include "math.cvh"
22
23/* Define and scope what needs to be seen by everyone */
24$input int NUM_THREADS;
25$input int ITERATIONS;
26$input int THRESHOLD;
27
28int count = 0;
29double finalresult=0.0;
30pthread_mutex_t count_mutex;
31pthread_cond_t count_condvar;
32
33void *sub1(void *t)
34{
35 int i;
36 long tid = (long)*t;
37 double myresult=0.0;
38
39 /*
40 Lock mutex and wait for signal only if count is what is expected. Note
41 that the pthread_cond_wait routine will automatically and atomically
42 unlock mutex while it waits. Also, note that if THRESHOLD is reached
43 before this routine is run by the waiting thread, the loop will be skipped
44 to prevent pthread_cond_wait from never returning, and that this thread's
45 work is now done within the mutex lock of count.
46 */
47 pthread_mutex_lock(&count_mutex);
48 if (count < THRESHOLD) {
49 printf("sub1: thread=%d going into wait. count=%d\n",tid,count); // Removed l from %ld
50 pthread_cond_wait(&count_condvar, &count_mutex);
51 printf("sub1: thread=%d Condition variable signal received.",tid); // Removed l from %ld
52 printf(" count=%d\n",count);
53 /* do some work */
54 //sleep(1);
55 count++;
56 finalresult += myresult;
57 printf("sub1: thread=%d count now equals=%d myresult=%e. Done.\n",tid,count,myresult); // Removed l from %ld
58 }
59 else {
60 printf("sub1: count=%d. Not as expected.",count);
61 printf(" Probably missed signal. Skipping work and exiting.\n");
62 }
63 pthread_mutex_unlock(&count_mutex);
64 pthread_exit(NULL, false, NULL, 0); //Different parameters
65}
66
67void *sub2(void *t)
68{
69 int j,i;
70 long tid = (long)*t;
71 double myresult=0.0;
72
73 for (i=0; i<ITERATIONS; i++) {
74 for (j=0; j<100000; j++)
75 myresult += sin(j) * tan(i);
76 pthread_mutex_lock(&count_mutex);
77 finalresult += myresult;
78 count++;
79 /*
80 Check the value of count and signal waiting thread when condition is
81 reached. Note that this occurs while mutex is locked.
82 */
83 if (count == THRESHOLD) {
84 printf("sub2: thread=%d Threshold reached. count=%d. ",tid,count); // Removed l from %ld
85 pthread_cond_signal(&count_condvar);
86 printf("Just sent signal.\n");
87 }
88 else {
89 printf("sub2: thread=%d did work. count=%d\n",tid,count); // Removed l from %ld
90 }
91 pthread_mutex_unlock(&count_mutex);
92 }
93 printf("sub2: thread=%d myresult=%e. Done. \n",tid,myresult); // Removed l from %ld
94 pthread_exit(NULL, false, NULL, 0); //Different parameters
95}
96
97int main(void)
98{
99 int i, rc;
100 long t1=1, t2=2, t3=3;
101 pthread_t threads[3];
102 pthread_attr_t attr;
103
104 /* Initialize mutex and condition variable objects */
105 pthread_mutex_init(&count_mutex, NULL);
106 pthread_cond_init (&count_condvar, NULL);
107
108 /* For portability, explicitly create threads in a joinable state */
109 pthread_attr_init(&attr);
110 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
111 pthread_create(&threads[0], &attr, sub1, (void *)&t1);
112 pthread_create(&threads[1], &attr, sub2, (void *)&t2);
113 pthread_create(&threads[2], &attr, sub2, (void *)&t3);
114
115 /* Wait for all threads to complete */
116 for (i = 0; i < NUM_THREADS; i++) {
117 pthread_join(threads[i], NULL);
118 }
119 printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
120 NUM_THREADS,finalresult);
121
122 /* Clean up and exit */
123 pthread_attr_destroy(&attr);
124 pthread_mutex_destroy(&count_mutex);
125 pthread_cond_destroy(&count_condvar);
126 pthread_exit(NULL, true, NULL, 0); //Different parameters
127
128}
Note: See TracBrowser for help on using the repository browser.