source: CIVL/examples/pthread/bug1fix.cvl@ f6ce0eb

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

Almost done

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