source: CIVL/examples/pthread/bug1.cvl@ 77be3db

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

Added bug fix programs and some documentation

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

  • Property mode set to 100644
File size: 3.6 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: bug1.cvl
8* DESCRIPTION:
9* This example has a bug. It is a variation on the condvar.cvl example.
10* Instead of just one thread waiting for the condition signal, there are
11* four threads waiting for the same signal. Find out how to fix the
12* program. The solution program is bug1fix.cvl
13* Command line execution:
14* civl verify bug1.cvl
15******************************************************************************/
16
17#include "pthread.cvh"
18#include <civlc.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22#define NUM_THREADS 6
23#define TCOUNT 10
24#define COUNT_LIMIT 12
25
26int count = 0;
27pthread_mutex_t count_mutex;
28pthread_cond_t count_threshold_cv;
29
30void *inc_count(void *idp)
31{
32 int j,i;
33 double result=0.0;
34 long my_id = (long)*idp;
35 for (i=0; i < TCOUNT; i++) {
36 pthread_mutex_lock(&count_mutex);
37 count++;
38
39 /*
40 Check the value of count and signal waiting thread when condition is
41 reached. Note that this occurs while mutex is locked.
42 */
43 if (count == COUNT_LIMIT) {
44 pthread_cond_signal(&count_threshold_cv);
45 printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count);
46 }
47 printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count);
48 pthread_mutex_unlock(&count_mutex);
49
50 /* Do some work so threads can alternate on mutex lock */
51 //sleep(1);
52 }
53 pthread_exit(NULL);
54}
55
56void *watch_count(void *idp)
57{
58 long my_id = (long)*idp;
59
60 printf("Starting watch_count(): thread %d\n", my_id);
61
62 /*
63 Lock mutex and wait for signal. Note that the pthread_cond_wait routine
64 will automatically and atomically unlock mutex while it waits.
65 Also, note that if COUNT_LIMIT is reached before this routine is run by
66 the waiting thread, the loop will be skipped to prevent pthread_cond_wait
67 from never returning.
68 */
69 pthread_mutex_lock(&count_mutex);
70 printf("***Before cond_wait: thread %d\n", my_id);
71 pthread_cond_wait(&count_threshold_cv, &count_mutex);
72 printf("***Thread %d Condition signal received.\n", my_id);
73 pthread_mutex_unlock(&count_mutex);
74 pthread_exit(NULL);
75}
76
77int main(int argc, char *argv[])
78{
79 int i, rc;
80 pthread_t threads[6];
81 pthread_attr_t attr;
82
83 /* Initialize mutex and condition variable objects */
84 pthread_mutex_init(&count_mutex, 0);
85 pthread_cond_init (&count_threshold_cv, NULL);
86
87 /*
88 For portability, explicitly create threads in a joinable state
89 */
90 int zero = 0;
91 int one = 1;
92 int two = 2;
93 int three = 3;
94 int four = 4;
95 int five = 5;
96 pthread_attr_init(&attr);
97 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
98 pthread_create(&threads[2], &attr, watch_count, &two);
99 pthread_create(&threads[3], &attr, watch_count, &three);
100 pthread_create(&threads[4], &attr, watch_count, &four);
101 pthread_create(&threads[5], &attr, watch_count, &five);
102 pthread_create(&threads[0], &attr, inc_count, &zero);
103 pthread_create(&threads[1], &attr, inc_count, &one);
104
105 /* Wait for all threads to complete */
106 for (i = 0; i < NUM_THREADS; i++) {
107 pthread_join(threads[i], NULL);
108 }
109 printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
110
111 /* Clean up and exit */
112 pthread_attr_destroy(&attr);
113 pthread_mutex_destroy(&count_mutex);
114 pthread_cond_destroy(&count_threshold_cv);
115 pthread_exit (NULL);
116 return 0;
117}
Note: See TracBrowser for help on using the repository browser.