source: CIVL/examples/pthread/bug1fix.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.4 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 bug1fix.cvl
14******************************************************************************/
15#include <pthread.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#define NUM_THREADS 6
20#define TCOUNT 10
21#define COUNT_LIMIT 12
22
23int count = 0;
24pthread_mutex_t count_mutex;
25pthread_cond_t count_threshold_cv;
26
27void *inc_count(void *idp)
28{
29 int j,i;
30 double result=0.0;
31 long my_id = (long)idp;
32 for (i=0; i < TCOUNT; i++) {
33 pthread_mutex_lock(&count_mutex);
34 count++;
35
36 /*
37 Check the value of count and signal waiting thread when condition is
38 reached. Note that this occurs while mutex is locked.
39 */
40 if (count == COUNT_LIMIT) {
41 pthread_cond_broadcast(&count_threshold_cv);
42 printf("inc_count(): thread %ld, count = %d Threshold reached.\n",
43 my_id, count);
44 }
45 printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
46 my_id, count);
47 pthread_mutex_unlock(&count_mutex);
48
49 /* Do some work so threads can alternate on mutex lock */
50 sleep(1);
51
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 %ld\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 %ld\n", my_id);
71 pthread_cond_wait(&count_threshold_cv, &count_mutex);
72 printf("***Thread %ld 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{
79int i, rc;
80pthread_t threads[6];
81pthread_attr_t attr;
82
83/* Initialize mutex and condition variable objects */
84pthread_mutex_init(&count_mutex, NULL);
85pthread_cond_init (&count_threshold_cv, NULL);
86
87/*
88For portability, explicitly create threads in a joinable state
89*/
90pthread_attr_init(&attr);
91pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
92pthread_create(&threads[2], &attr, watch_count, (void *)2);
93pthread_create(&threads[3], &attr, watch_count, (void *)3);
94pthread_create(&threads[4], &attr, watch_count, (void *)4);
95pthread_create(&threads[5], &attr, watch_count, (void *)5);
96pthread_create(&threads[0], &attr, inc_count, (void *)0);
97pthread_create(&threads[1], &attr, inc_count, (void *)1);
98
99/* Wait for all threads to complete */
100for (i = 0; i < NUM_THREADS; i++) {
101 pthread_join(threads[i], NULL);
102 }
103printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
104
105/* Clean up and exit */
106pthread_attr_destroy(&attr);
107pthread_mutex_destroy(&count_mutex);
108pthread_cond_destroy(&count_threshold_cv);
109pthread_exit (NULL);
110
111}
112
Note: See TracBrowser for help on using the repository browser.