1 | /******************************************************************************
|
---|
2 | * FILE: condvar.c
|
---|
3 | * DESCRIPTION:
|
---|
4 | * Example code for using Pthreads condition variables. The main thread
|
---|
5 | * creates three threads. Two of those threads increment a "count" variable,
|
---|
6 | * while the third thread watches the value of "count". When "count"
|
---|
7 | * reaches a predefined limit, the waiting thread is signaled by one of the
|
---|
8 | * incrementing threads. The waiting thread "awakens" and then modifies
|
---|
9 | * count. The program continues until the incrementing threads reach
|
---|
10 | * TCOUNT. The main program prints the final value of count.
|
---|
11 | * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
---|
12 | * et al. O'Reilly and Associates.
|
---|
13 | * LAST REVISED: 07/16/09 Blaise Barney
|
---|
14 | ******************************************************************************/
|
---|
15 | #include <pthread.h>
|
---|
16 | #include <stdio.h>
|
---|
17 | #include <stdlib.h>
|
---|
18 |
|
---|
19 | #define NUM_THREADS 3
|
---|
20 | #define TCOUNT 10
|
---|
21 | #define COUNT_LIMIT 12
|
---|
22 |
|
---|
23 | int count = 0;
|
---|
24 | pthread_mutex_t count_mutex;
|
---|
25 | pthread_cond_t count_threshold_cv;
|
---|
26 |
|
---|
27 | void *inc_count(void *t)
|
---|
28 | {
|
---|
29 | int i;
|
---|
30 | long my_id = (long)t;
|
---|
31 |
|
---|
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 | printf("inc_count(): thread %ld, count = %d Threshold reached. ",
|
---|
42 | my_id, count);
|
---|
43 | pthread_cond_signal(&count_threshold_cv);
|
---|
44 | printf("Just sent signal.\n");
|
---|
45 | }
|
---|
46 | printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
|
---|
47 | my_id, count);
|
---|
48 | pthread_mutex_unlock(&count_mutex);
|
---|
49 |
|
---|
50 | /* Do some work so threads can alternate on mutex lock */
|
---|
51 | sleep(1);
|
---|
52 | }
|
---|
53 | pthread_exit(NULL);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void *watch_count(void *t)
|
---|
57 | {
|
---|
58 | long my_id = (long)t;
|
---|
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 | while (count < COUNT_LIMIT) {
|
---|
71 | printf("watch_count(): thread %ld going into wait...\n", my_id);
|
---|
72 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
---|
73 | }
|
---|
74 |
|
---|
75 | printf("watch_count(): thread %ld Condition signal received.\n", my_id);
|
---|
76 | count += 125;
|
---|
77 | printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
|
---|
78 |
|
---|
79 | pthread_mutex_unlock(&count_mutex);
|
---|
80 | pthread_exit(NULL);
|
---|
81 | }
|
---|
82 |
|
---|
83 | int main(int argc, char *argv[])
|
---|
84 | {
|
---|
85 | int i, rc;
|
---|
86 | long t1=1, t2=2, t3=3;
|
---|
87 | pthread_t threads[3];
|
---|
88 | pthread_attr_t attr;
|
---|
89 |
|
---|
90 | /* Initialize mutex and condition variable objects */
|
---|
91 | pthread_mutex_init(&count_mutex, NULL);
|
---|
92 | pthread_cond_init (&count_threshold_cv, NULL);
|
---|
93 |
|
---|
94 | /* For portability, explicitly create threads in a joinable state */
|
---|
95 | pthread_attr_init(&attr);
|
---|
96 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
---|
97 | pthread_create(&threads[0], &attr, watch_count, (void *)t1);
|
---|
98 | pthread_create(&threads[1], &attr, inc_count, (void *)t2);
|
---|
99 | pthread_create(&threads[2], &attr, inc_count, (void *)t3);
|
---|
100 |
|
---|
101 | /* Wait for all threads to complete */
|
---|
102 | for (i = 0; i < NUM_THREADS; i++) {
|
---|
103 | pthread_join(threads[i], NULL);
|
---|
104 | }
|
---|
105 | printf ("Main(): Waited on %d threads. Final value of count = %d. Done.\n",
|
---|
106 | NUM_THREADS, count);
|
---|
107 |
|
---|
108 | /* Clean up and exit */
|
---|
109 | pthread_attr_destroy(&attr);
|
---|
110 | pthread_mutex_destroy(&count_mutex);
|
---|
111 | pthread_cond_destroy(&count_threshold_cv);
|
---|
112 | pthread_exit (NULL);
|
---|
113 |
|
---|
114 | }
|
---|