-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread_signal_example.cc
More file actions
142 lines (116 loc) · 3.33 KB
/
pthread_signal_example.cc
File metadata and controls
142 lines (116 loc) · 3.33 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#include <unistd.h>
pthread_mutex_t mutex_work_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_work_2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_work_3 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_work_1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_work_2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_work_3 = PTHREAD_COND_INITIALIZER;
int work1_cond_ready = 0;
int work2_cond_ready = 0;
int work3_cond_ready = 0;
int exit_cond_ready = 0;
long int global_index = 0;
void *
work_thread_1(void * arg){
while(1){
pthread_mutex_lock(&mutex_work_1);
if(1 != work1_cond_ready){
pthread_cond_wait(&cond_work_1, &mutex_work_1);
}
if(9999 == global_index){
exit_cond_ready = 1;
}
if(exit_cond_ready != 0){
work2_cond_ready = 1;
pthread_cond_signal(&cond_work_2);
usleep(5);
pthread_exit(0);
}
printf("Start work thread 1.\n");
work1_cond_ready = 0;
/* DO SOMETHING IN THREAD ONE */
global_index++;
printf("global_index = %ld\n", global_index);
work2_cond_ready = 1;
usleep(500);
pthread_mutex_unlock(&mutex_work_1);
pthread_cond_signal(&cond_work_2);
printf("End of thread 1.\n");
}
}
void *
work_thread_2(void * arg){
while(1){
pthread_mutex_lock(&mutex_work_2);
if(1 != work2_cond_ready){
pthread_cond_wait(&cond_work_2, &mutex_work_2);
}
if(exit_cond_ready != 0){
work3_cond_ready = 1;
pthread_cond_signal(&cond_work_3);
usleep(5);
pthread_exit(0);
}
printf("Start work thread 2.\n");
work2_cond_ready = 0;
/* DO SOMETHING IN THREAD TWO */
work3_cond_ready = 1;
usleep(500);
pthread_mutex_unlock(&mutex_work_2);
pthread_cond_signal(&cond_work_3);
printf("End of thread 2.\n");
}
}
void *
work_thread_3(void * arg){
while(1){
pthread_mutex_lock(&mutex_work_3);
if(1 != work3_cond_ready){
pthread_cond_wait(&cond_work_3, &mutex_work_3);
}
if(exit_cond_ready != 0){
usleep(5);
pthread_exit(0);
}
printf("Start work thread 3.\n");
work3_cond_ready = 0;
/* DO SOMETHING IN THREAD THREE */
work1_cond_ready = 1;
usleep(500);
pthread_mutex_unlock(&mutex_work_3);
pthread_cond_signal(&cond_work_1);
printf("End of thread 3.\n");
}
}
int
main(){
float *workspace_thread_1;
float *workspace_thread_2;
float *workspace_thread_3;
workspace_thread_1 = (float *)malloc(1000 * sizeof(float));
workspace_thread_2 = (float *)malloc(1000 * sizeof(float));
workspace_thread_3 = (float *)malloc(1000 * sizeof(float));
pthread_t thread_1;
pthread_create(&thread_1, NULL, work_thread_1, NULL);
printf("Thread 1 lanched!\n");
pthread_t thread_2;
pthread_create(&thread_2, NULL, work_thread_2, NULL);
printf("Thread 2 lanched!\n");
pthread_t thread_3;
pthread_create(&thread_3, NULL, work_thread_3, NULL);
printf("Thread 3 lanched!\n");
work1_cond_ready = 1;
pthread_cond_signal(&cond_work_1);
pthread_join(thread_1, NULL);
pthread_mutex_destroy(&mutex_work_1);
pthread_cond_destroy(&cond_work_1);
pthread_mutex_destroy(&mutex_work_2);
pthread_cond_destroy(&cond_work_2);
pthread_mutex_destroy(&mutex_work_3);
pthread_cond_destroy(&cond_work_3);
return 0;
}