source: CIVL/examples/pthread/bug4.c@ f6ce0eb

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

Updated pthread.cvh and examples

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

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