source: CIVL/examples/pthread/bug4fix.cvl@ 6fe2cd9

1.23 2.0 main test-branch
Last change on this file since 6fe2cd9 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.3 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 <stdio.h>
19#include <stdlib.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
31void *sub1(void *t)
32{
33 int i;
34 long tid = (long)*t;
35 double myresult=0.0;
36
37 /*
38 Lock mutex and wait for signal only if count is what is expected. Note
39 that the pthread_cond_wait routine will automatically and atomically
40 unlock mutex while it waits. Also, note that if THRESHOLD is reached
41 before this routine is run by the waiting thread, the loop will be skipped
42 to prevent pthread_cond_wait from never returning, and that this thread's
43 work is now done within the mutex lock of count.
44 */
45 pthread_mutex_lock(&count_mutex);
46 if (count < THRESHOLD) {
47 printf("sub1: thread=%d going into wait. count=%d\n",tid,count); // Removed l from %ld
48 pthread_cond_wait(&count_condvar, &count_mutex);
49 printf("sub1: thread=%d Condition variable signal received.",tid); // Removed l from %ld
50 printf(" count=%d\n",count);
51 /* do some work */
52 //sleep(1);
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 }
57 else {
58 printf("sub1: count=%d. Not as expected.",count);
59 printf(" Probably missed signal. Skipping work and exiting.\n");
60 }
61 pthread_mutex_unlock(&count_mutex);
62 pthread_exit(NULL, false, NULL, 0); //Different parameters
63}
64
65void *sub2(void *t)
66{
67 int j,i;
68 long tid = (long)*t;
69 double myresult=0.0;
70
71 for (i=0; i<ITERATIONS; i++) {
72 for (j=0; j<100000; j++)
73 myresult += sin(j) * tan(i);
74 pthread_mutex_lock(&count_mutex);
75 finalresult += myresult;
76 count++;
77 /*
78 Check the value of count and signal waiting thread when condition is
79 reached. Note that this occurs while mutex is locked.
80 */
81 if (count == THRESHOLD) {
82 printf("sub2: thread=%d Threshold reached. count=%d. ",tid,count); // Removed l from %ld
83 pthread_cond_signal(&count_condvar);
84 printf("Just sent signal.\n");
85 }
86 else {
87 printf("sub2: thread=%d did work. count=%d\n",tid,count); // Removed l from %ld
88 }
89 pthread_mutex_unlock(&count_mutex);
90 }
91 printf("sub2: thread=%d myresult=%e. Done. \n",tid,myresult); // Removed l from %ld
92 pthread_exit(NULL, false, NULL, 0); //Different parameters
93}
94
95int main(void)
96{
97 int i, rc;
98 long t1=1, t2=2, t3=3;
99 pthread_t threads[3];
100 pthread_attr_t attr;
101
102 /* Initialize mutex and condition variable objects */
103 pthread_mutex_init(&count_mutex, NULL);
104 pthread_cond_init (&count_condvar, NULL);
105
106 /* For portability, explicitly create threads in a joinable state */
107 pthread_attr_init(&attr);
108 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
109 pthread_create(&threads[0], &attr, sub1, (void *)&t1);
110 pthread_create(&threads[1], &attr, sub2, (void *)&t2);
111 pthread_create(&threads[2], &attr, sub2, (void *)&t3);
112
113 /* Wait for all threads to complete */
114 for (i = 0; i < NUM_THREADS; i++) {
115 pthread_join(threads[i], NULL);
116 }
117 printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
118 NUM_THREADS,finalresult);
119
120 /* Clean up and exit */
121 pthread_attr_destroy(&attr);
122 pthread_mutex_destroy(&count_mutex);
123 pthread_cond_destroy(&count_condvar);
124 pthread_exit(NULL, true, NULL, 0); //Different parameters
125
126}
Note: See TracBrowser for help on using the repository browser.