source: CIVL/examples/pthread/bug1fix.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: 4.0 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: bug1fix.cvl
8* DESCRIPTION:
9* Solution for the bug1.cvl program. The inc_count routine uses a
10* pthread_cond_broadcast() routine instead of the pthread_cond_signal()
11* routine.
12* Command line execution:
13* civl verify -inputNUM_THREADS=6 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 bug1fix.cvl
14******************************************************************************/
15#include "pthread.cvh"
16#include <civlc.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20$input int NUM_THREADS; //Changed definitions to input variables
21$input int TCOUNT;
22$input int COUNT_LIMIT;
23
24int count = 0;
25pthread_mutex_t count_mutex;
26pthread_cond_t count_threshold_cv;
27
28void *inc_count(void *idp)
29{
30 int j,i;
31 double result=0.0;
32 long my_id = (long)*idp; //Changed to dereference, can't directly convert from void* to long
33 for (i=0; i < TCOUNT; i++) {
34 pthread_mutex_lock(&count_mutex);
35 count++;
36
37 /*
38 Check the value of count and signal waiting thread when condition is
39 reached. Note that this occurs while mutex is locked.
40 */
41 if (count == COUNT_LIMIT) {
42 pthread_cond_broadcast(&count_threshold_cv);
43 // Removed l from %ld, unsupported
44 printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count);
45 }
46 // Removed l from %ld, unsupported
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); Removed sleep, unnecessary
52
53 }
54 pthread_exit(NULL);
55}
56
57void *watch_count(void *idp)
58{
59 long my_id = (long)*idp;
60
61 // Removed l from %ld, unsupported
62 printf("Starting watch_count(): thread %d\n", my_id);
63
64 /*
65 Lock mutex and wait for signal. Note that the pthread_cond_wait routine
66 will automatically and atomically unlock mutex while it waits.
67 Also, note that if COUNT_LIMIT is reached before this routine is run by
68 the waiting thread, the loop will be skipped to prevent pthread_cond_wait
69 from never returning.
70 */
71 pthread_mutex_lock(&count_mutex);
72 printf("***Before cond_wait: thread %d\n", my_id); // Removed l from %ld, unsupported
73 pthread_cond_wait(&count_threshold_cv, &count_mutex);
74 printf("***Thread %d Condition signal received.\n", my_id); // Removed l from %ld, unsupported
75 pthread_mutex_unlock(&count_mutex);
76 pthread_exit(NULL);
77}
78
79int main(int argc, char *argv[])
80{
81 int i, rc;
82 pthread_t threads[6];
83 pthread_attr_t attr;
84
85 /* Initialize mutex and condition variable objects */
86 pthread_mutex_init(&count_mutex, NULL);
87 pthread_cond_init (&count_threshold_cv, NULL);
88
89 /*
90 For portability, explicitly create threads in a joinable state
91 Created variables for casting as we are unable to currently cast raw integer values as
92 void pointers.
93 */
94 int zero = 0;
95 int one = 1;
96 int two = 2;
97 int three = 3;
98 int four = 4;
99 int five = 5;
100 pthread_attr_init(&attr);
101 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
102 pthread_create(&threads[2], &attr, watch_count, (void *)&two);
103 pthread_create(&threads[3], &attr, watch_count, (void *)&three);
104 pthread_create(&threads[4], &attr, watch_count, (void *)&four);
105 pthread_create(&threads[5], &attr, watch_count, (void *)&five);
106 pthread_create(&threads[0], &attr, inc_count, (void *)&zero);
107 pthread_create(&threads[1], &attr, inc_count, (void *)&one);
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. Done.\n", NUM_THREADS);
114
115 /* Clean up and exit */
116 pthread_attr_destroy(&attr);
117 pthread_mutex_destroy(&count_mutex);
118 pthread_cond_destroy(&count_threshold_cv);
119 pthread_exit (NULL);
120
121}
122
Note: See TracBrowser for help on using the repository browser.