source: CIVL/examples/pthread/bug1.cvl@ 068c228

1.23 2.0 main test-branch
Last change on this file since 068c228 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: 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 -inputNUM_THREADS=6 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 bug1.cvl
15******************************************************************************/
16
17#include "pthread.cvh"
18#include <civlc.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22$input int NUM_THREADS; //Changed definitions to input variables
23$input int TCOUNT;
24$input int COUNT_LIMIT;
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; // Changed to dereference
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); //removed l from %ld
46 }
47 printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count); //removed l from %ld
48 pthread_mutex_unlock(&count_mutex);
49
50 /* Do some work so threads can alternate on mutex lock */
51 //sleep(1); removed sleep, unnecessary
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 Created variables for casting as we are unable to currently cast raw integer values as
90 void pointers.
91 */
92 int zero = 0;
93 int one = 1;
94 int two = 2;
95 int three = 3;
96 int four = 4;
97 int five = 5;
98 pthread_attr_init(&attr);
99 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
100 pthread_create(&threads[2], &attr, watch_count, &two);
101 pthread_create(&threads[3], &attr, watch_count, &three);
102 pthread_create(&threads[4], &attr, watch_count, &four);
103 pthread_create(&threads[5], &attr, watch_count, &five);
104 pthread_create(&threads[0], &attr, inc_count, &zero);
105 pthread_create(&threads[1], &attr, inc_count, &one);
106
107 /* Wait for all threads to complete */
108 for (i = 0; i < NUM_THREADS; i++) {
109 pthread_join(threads[i], NULL);
110 }
111 printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
112
113 /* Clean up and exit */
114 pthread_attr_destroy(&attr);
115 pthread_mutex_destroy(&count_mutex);
116 pthread_cond_destroy(&count_threshold_cv);
117 pthread_exit (NULL);
118 return 0;
119}
Note: See TracBrowser for help on using the repository browser.