-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_thread_cond.c
146 lines (136 loc) · 4.09 KB
/
test_thread_cond.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#define debug_log printf("%s:%d--", __FUNCTION__, __LINE__);printf
/*
* The test content is :
* 1> the common area work_area[1024]
* 2> main loop: record user input, recv "end" for mainloop returning.
* 3> thread1 : print user input and count the input number.
* 4> thread2 : backup the user input to work_backup[1024] and clear the work_area
*
* we need to protect the work_area using the sem.
*/
static char work_area[1024], back_area[1024];
static pthread_mutex_t mutex;
static pthread_cond_t cond;
static int common = 0;
// common ++ after 3s
// the producer will inform the consumer the common ++.
static void *thread_producer(void* args)
{
for ( ; ; ) {
pthread_mutex_lock(&mutex);
common += 2;
debug_log("thread producer add common is ++++++++++++++++++++++++++%d \n", common);
debug_log("call pthread_cond_broadcase\n");
//pthread_cond_signal(&cond);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
sleep(3);
}
pthread_exit("pthread 01: exit");
}
static void *thread_consumer_1(void* args)
{
for ( ; ; ) {
pthread_mutex_lock(&mutex);
debug_log("call pthread_cond_wait\n");
pthread_cond_wait(&cond, &mutex);
debug_log("common --\n");
common --;
debug_log("thread consume_1 sub common is ----------------------------%d \n", common);
if (common < 0) {
debug_log("common is neg, failed\n");
goto finish;
}
pthread_mutex_unlock(&mutex);
}
finish:
pthread_exit("pthread 01: exit");
}
static void *thread_consumer_2(void* args)
{
for ( ; ; ) {
pthread_mutex_lock(&mutex);
debug_log("call pthread_cond_wait\n");
pthread_cond_wait(&cond, &mutex);
debug_log("common --\n");
common --;
debug_log("thread consume_2 sub common is ---------------------------------%d \n", common);
if (common < 0) {
debug_log("common is neg, failed\n");
goto finish;
}
pthread_mutex_unlock(&mutex);
}
finish:
pthread_exit("pthread 01: exit");
}
static int test1_basic_cond()
{
int ret = 0;
pthread_t thread01, thread02, thread03;
void *thread_result = NULL;
ret = pthread_mutex_init(&mutex, NULL);
if (ret != 0) {
debug_log("failed on mutex_init ret = %d\n", ret);
perror("failed reason: ");
goto exit;
}
ret = pthread_cond_init(&cond, NULL);
if (ret != 0) {
debug_log("failed on cond_init ret = %d\n", ret);
perror("failed reason: ");
goto exit;
}
ret = pthread_create(&thread01, NULL, thread_producer, NULL);
if (ret != 0) {
debug_log("create failed, ret = %d\n", ret);
goto exit;
}
ret = pthread_create(&thread02, NULL, thread_consumer_1, NULL);
if (ret != 0) {
debug_log("create failed, ret = %d\n", ret);
goto exit;
}
ret = pthread_create(&thread02, NULL, thread_consumer_2, NULL);
if (ret != 0) {
debug_log("create failed, ret = %d\n", ret);
goto exit;
}
debug_log("Wait for sub thread.\n");
ret = pthread_join(thread01, &thread_result);
if (ret != 0) {
debug_log("join failed, ret = %d\n", ret);
goto exit;
}
debug_log("sub thread msg : %s\n test1 finish\n", (const char *)thread_result);
ret = pthread_join(thread02, &thread_result);
if (ret != 0) {
debug_log("join failed, ret = %d\n", ret);
goto exit;
}
ret = pthread_join(thread03, &thread_result);
if (ret != 0) {
debug_log("join failed, ret = %d\n", ret);
goto exit;
}
debug_log("back up area is %s\n", back_area);
debug_log("sub thread msg : %s\n test1 finish\n", (const char *)thread_result);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit:
return ret;
}
int main(void)
{
int ret = 0;
ret = test1_basic_cond();
printf("%s : test done !!\n", __FUNCTION__);
return ret;
}