source: CIVL/examples/pthread/bug1.cvl@ 325d439

1.23 2.0 main test-branch
Last change on this file since 325d439 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.0 KB
RevLine 
[38374b7]1/*****************************************************************************
[9f23e8b]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
[38374b7]8* DESCRIPTION:
[9f23e8b]9* This example has a bug. It is a variation on the condvar.cvl example.
[38374b7]10* Instead of just one thread waiting for the condition signal, there are
[6fe2cd9]11* four threads waiting for the same signal causing a deadlock.
12* The solution program is bug1fix.cvl
[9f23e8b]13* Command line execution:
[bf81b8c]14* civl verify -inputNUM_THREADS=6 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 bug1.cvl
[38374b7]15******************************************************************************/
[9f23e8b]16
[38374b7]17#include "pthread.cvh"
18#include <civlc.h>
19#include <stdio.h>
20#include <stdlib.h>
21
[bf81b8c]22$input int NUM_THREADS; //Changed definitions to input variables
23$input int TCOUNT;
24$input int COUNT_LIMIT;
[38374b7]25
[bf81b8c]26int count = 0;
[38374b7]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
[6fe2cd9]35
[38374b7]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_signal(&count_threshold_cv);
[bf81b8c]46 printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count); //removed l from %ld
[38374b7]47 }
[bf81b8c]48 printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count); //removed l from %ld
[38374b7]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
[38374b7]53 }
[37bfb99]54 pthread_exit(NULL, false, NULL, 0);
[38374b7]55}
56
57void *watch_count(void *idp)
58{
59 long my_id = (long)*idp;
60
61 printf("Starting watch_count(): thread %d\n", my_id);
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);
71 printf("***Before cond_wait: thread %d\n", my_id);
72 pthread_cond_wait(&count_threshold_cv, &count_mutex);
73 printf("***Thread %d Condition signal received.\n", my_id);
74 pthread_mutex_unlock(&count_mutex);
[6fe2cd9]75 pthread_exit(NULL, false, NULL, 0); //Different parameters
[38374b7]76}
77
[ca52517]78int main(void)
[38374b7]79{
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, 0);
86 pthread_cond_init (&count_threshold_cv, NULL);
87
88 /*
89 For portability, explicitly create threads in a joinable state
[bf81b8c]90 Created variables for casting as we are unable to currently cast raw integer values as
91 void pointers.
[38374b7]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, &two);
102 pthread_create(&threads[3], &attr, watch_count, &three);
103 pthread_create(&threads[4], &attr, watch_count, &four);
104 pthread_create(&threads[5], &attr, watch_count, &five);
105 pthread_create(&threads[0], &attr, inc_count, &zero);
106 pthread_create(&threads[1], &attr, inc_count, &one);
107
[ca52517]108 pthread_attr_destroy(&attr);
109
[38374b7]110 /* Wait for all threads to complete */
111 for (i = 0; i < NUM_THREADS; i++) {
112 pthread_join(threads[i], NULL);
113 }
114 printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
115
116 /* Clean up and exit */
[ca52517]117
[38374b7]118 pthread_mutex_destroy(&count_mutex);
119 pthread_cond_destroy(&count_threshold_cv);
[6fe2cd9]120 pthread_exit (NULL, true, NULL, 0); //Different parameters
[38374b7]121 return 0;
122}
Note: See TracBrowser for help on using the repository browser.