source: CIVL/examples/pthread/bug4.cvl@ 761eb6f

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

Updated headers for examples

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

  • Property mode set to 100644
File size: 4.0 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: 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#include "pthread.cvh"
16#include <civlc.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <math.h>
20
21/* Define and scope what needs to be seen by everyone */
22$input int NUM_THREADS;
23$input int ITERATIONS;
24$input int THRESHOLD;
25
26int count = 0;
27double finalresult=0.0;
28pthread_mutex_t count_mutex;
29pthread_cond_t count_condvar;
30
31
32void *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 Lock mutex and wait for signal only if count is what is expected. Note
42 that the pthread_cond_wait routine will automatically and atomically
43 unlock mutex while it waits. Also, note that if THRESHOLD is reached
44 before this routine is run by the waiting thread, the loop will be skipped
45 to prevent pthread_cond_wait from never returning, and that this thread's
46 work is now done within the mutex lock of count.
47 */
48 pthread_mutex_lock(&count_mutex);
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 count++;
54 finalresult += myresult;
55 printf("sub1: thread=%d count now equals=%d myresult=%e. Done.\n",tid,count,myresult); // Removed l from %ld
56 pthread_mutex_unlock(&count_mutex);
57 pthread_exit(NULL);
58}
59
60void *sub2(void *t)
61{
62 int j,i;
63 long tid = (long)*t;
64 double myresult=0.0;
65
66 for (i=0; i<ITERATIONS; i++) {
67 for (j=0; j<100000; j++)
68 myresult += sin(j) * tan(i);
69 pthread_mutex_lock(&count_mutex);
70 finalresult += myresult;
71 count++;
72 /*
73 Check the value of count and signal waiting thread when condition is
74 reached. Note that this occurs while mutex is locked.
75 */
76 if (count == THRESHOLD) {
77 printf("sub2: thread=%d Threshold reached. count=%d. ",tid,count); // Removed l from %ld
78 pthread_cond_signal(&count_condvar);
79 printf("Just sent signal.\n");
80 }
81 else {
82 printf("sub2: thread=%d did work. count=%d\n",tid,count); // Removed l from %ld
83 }
84 pthread_mutex_unlock(&count_mutex);
85 }
86 printf("sub2: thread=%d myresult=%e. Done. \n",tid,myresult); // Removed l from %ld
87 pthread_exit(NULL);
88}
89
90
91
92int main(int argc, char *argv[])
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);
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);
122
123}
124
125
Note: See TracBrowser for help on using the repository browser.