source: CIVL/examples/pthread/bug4.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: 3.9 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 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#define NUM_THREADS 3
23#define ITERATIONS 10
24#define THRESHOLD 12
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 /* do some work */
38 //sleep(1); Removed, unnecessary
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 printf("sub1: thread=%d going into wait. count=%d\n",tid,count); // Removed l from %ld
49 pthread_cond_wait(&count_condvar, &count_mutex);
50 printf("sub1: thread=%d Condition variable signal received.",tid); // Removed l from %ld
51 printf(" count=%d\n",count);
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 pthread_mutex_unlock(&count_mutex);
56 pthread_exit(NULL);
57}
58
59void *sub2(void *t)
60{
61 int j,i;
62 long tid = (long)*t;
63 double myresult=0.0;
64
65 for (i=0; i<ITERATIONS; i++) {
66 for (j=0; j<100000; j++)
67 myresult += sin(j) * tan(i);
68 pthread_mutex_lock(&count_mutex);
69 finalresult += myresult;
70 count++;
71 /*
72 Check the value of count and signal waiting thread when condition is
73 reached. Note that this occurs while mutex is locked.
74 */
75 if (count == THRESHOLD) {
76 printf("sub2: thread=%d Threshold reached. count=%d. ",tid,count); // Removed l from %ld
77 pthread_cond_signal(&count_condvar);
78 printf("Just sent signal.\n");
79 }
80 else {
81 printf("sub2: thread=%d did work. count=%d\n",tid,count); // Removed l from %ld
82 }
83 pthread_mutex_unlock(&count_mutex);
84 }
85 printf("sub2: thread=%d myresult=%e. Done. \n",tid,myresult); // Removed l from %ld
86 pthread_exit(NULL);
87}
88
89
90
91int main(int argc, char *argv[])
92{
93 long t1=1, t2=2, t3=3;
94 int i, rc;
95 pthread_t threads[3];
96 pthread_attr_t attr;
97
98 /* Initialize mutex and condition variable objects */
99 pthread_mutex_init(&count_mutex, NULL);
100 pthread_cond_init (&count_condvar, NULL);
101
102 /* For portability, explicitly create threads in a joinable state */
103 pthread_attr_init(&attr);
104 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
105 pthread_create(&threads[0], &attr, sub1, (void *)&t1);
106 pthread_create(&threads[1], &attr, sub2, (void *)&t2);
107 pthread_create(&threads[2], &attr, sub2, (void *)&t3);
108
109 /* Wait for all threads to complete */
110 for (i = 0; i < NUM_THREADS; i++) {
111 pthread_join(threads[i], NULL);
112 }
113 printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
114 NUM_THREADS,finalresult);
115
116 /* Clean up and exit */
117 pthread_attr_destroy(&attr);
118 pthread_mutex_destroy(&count_mutex);
119 pthread_cond_destroy(&count_condvar);
120 pthread_exit (NULL);
121
122}
123
124
Note: See TracBrowser for help on using the repository browser.