source: CIVL/examples/pthread/bug1fix.cvl@ 37bfb99

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 37bfb99 was 37bfb99, checked in by John Edenhofner <johneden@…>, 12 years ago

Updated examples and pthread.cvh

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

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