source: CIVL/examples/pthread/bug4fix.cvl@ 37bfb99

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

Updated examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1031 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: 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#include <pthread.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20/* Define and scope what needs to be seen by everyone */
21$input int NUM_THREADS;
22$input int ITERATIONS;
23$input int THRESHOLD;
24
25int count = 0;
26double finalresult=0.0;
27pthread_mutex_t count_mutex;
28pthread_cond_t count_condvar;
29
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);
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);
93}
94
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);
127
128}
Note: See TracBrowser for help on using the repository browser.