source: CIVL/examples/pthread/bug4.cvl@ 4b4bbb3

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

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

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/******************************************************************************
2* FILE: bug4.c
3* DESCRIPTION:
4* This program demonstrates a condition variable race/synchronization
5* problem. It resembles the condvar.c program. One possible solution can
6* be found in bug4fix.c
7* SOURCE: 07/06/05 Blaise Barney
8* LAST REVISED: 01/29/09 Blaise Barney
9******************************************************************************/
10#include "pthread.cvh"
11#include <civlc.h>
12#include <stdio.h>
13#include <stdlib.h>
14
15/* Define and scope what needs to be seen by everyone */
16#define NUM_THREADS 3
17#define ITERATIONS 10
18#define THRESHOLD 12
19int count = 0;
20double finalresult=0.0;
21pthread_mutex_t count_mutex;
22pthread_cond_t count_condvar;
23
24
25void *sub1(void *t)
26{
27 int i;
28 long tid = (long)t;
29 double myresult=0.0;
30
31 /* do some work */
32 sleep(1);
33 /*
34 Lock mutex and wait for signal only if count is what is expected. Note
35 that the pthread_cond_wait routine will automatically and atomically
36 unlock mutex while it waits. Also, note that if THRESHOLD is reached
37 before this routine is run by the waiting thread, the loop will be skipped
38 to prevent pthread_cond_wait from never returning, and that this thread's
39 work is now done within the mutex lock of count.
40 */
41 pthread_mutex_lock(&count_mutex);
42 printf("sub1: thread=%ld going into wait. count=%d\n",tid,count);
43 pthread_cond_wait(&count_condvar, &count_mutex);
44 printf("sub1: thread=%ld Condition variable signal received.",tid);
45 printf(" count=%d\n",count);
46 count++;
47 finalresult += myresult;
48 printf("sub1: thread=%ld count now equals=%d myresult=%e. Done.\n",
49 tid,count,myresult);
50 pthread_mutex_unlock(&count_mutex);
51 pthread_exit(NULL);
52}
53
54void *sub2(void *t)
55{
56 int j,i;
57 long tid = (long)t;
58 double myresult=0.0;
59
60 for (i=0; i<ITERATIONS; i++) {
61 for (j=0; j<100000; j++)
62 myresult += sin(j) * tan(i);
63 pthread_mutex_lock(&count_mutex);
64 finalresult += myresult;
65 count++;
66 /*
67 Check the value of count and signal waiting thread when condition is
68 reached. Note that this occurs while mutex is locked.
69 */
70 if (count == THRESHOLD) {
71 printf("sub2: thread=%ld Threshold reached. count=%d. ",tid,count);
72 pthread_cond_signal(&count_condvar);
73 printf("Just sent signal.\n");
74 }
75 else {
76 printf("sub2: thread=%ld did work. count=%d\n",tid,count);
77 }
78 pthread_mutex_unlock(&count_mutex);
79 }
80 printf("sub2: thread=%ld myresult=%e. Done. \n",tid,myresult);
81 pthread_exit(NULL);
82}
83
84
85
86int main(int argc, char *argv[])
87{
88 long t1=1, t2=2, t3=3;
89 int i, rc;
90 pthread_t threads[3];
91 pthread_attr_t attr;
92
93 /* Initialize mutex and condition variable objects */
94 pthread_mutex_init(&count_mutex, NULL);
95 pthread_cond_init (&count_condvar, NULL);
96
97 /* For portability, explicitly create threads in a joinable state */
98 pthread_attr_init(&attr);
99 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
100 pthread_create(&threads[0], &attr, sub1, (void *)t1);
101 pthread_create(&threads[1], &attr, sub2, (void *)t2);
102 pthread_create(&threads[2], &attr, sub2, (void *)t3);
103
104 /* Wait for all threads to complete */
105 for (i = 0; i < NUM_THREADS; i++) {
106 pthread_join(threads[i], NULL);
107 }
108 printf ("Main(): Waited on %d threads. Final result=%e. Done.\n",
109 NUM_THREADS,finalresult);
110
111 /* Clean up and exit */
112 pthread_attr_destroy(&attr);
113 pthread_mutex_destroy(&count_mutex);
114 pthread_cond_destroy(&count_condvar);
115 pthread_exit (NULL);
116
117}
118
119
Note: See TracBrowser for help on using the repository browser.