source: CIVL/examples/pthread/bug4fix.cvl@ bf81b8c

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

Updated examples

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