-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadPool.c
More file actions
262 lines (237 loc) · 7.93 KB
/
threadPool.c
File metadata and controls
262 lines (237 loc) · 7.93 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "threadPool.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NUMBER 2
typedef struct Task {
void (* func)(void* arg);
void * arg;
}Task;
/**
* @brief one task's struct in task queue
*
*/
typedef struct Threadpool {
Task* taskQ;// task queue
pthread_t managerID;
pthread_t* threadIDs;// an arry of threads, we put a task in a thread to let a thread work
int queueSize;// how many thread
int queueCapacity;
int Front;
int Rear;
int maxNum;
int minNum;
int liveNum;
int busyNum;
int exitNum;
pthread_mutex_t mutexPool;
pthread_mutex_t mutexBusy;
pthread_cond_t notFull;
pthread_cond_t notEmpty;
int shutDown;//its time to close
};//define a struct of Threadpool, with all sources
//create an func to init the threadpool
Threadpool* createThreadpool(int min, int max, int queueCapacity) {
printf("try to create a pool...\n");
Threadpool* pool = (Threadpool*)malloc(sizeof(Threadpool));//init threadpool
printf("threadpool create successful!\n");
do{
if (pool == NULL) {
printf("init pool fail...\n");
break;
}
//init taskQ
pool->taskQ = (Task*)malloc(sizeof(Task) * queueCapacity);
if (pool->taskQ == NULL) {
printf("init taskQ fail...\n");
break;
}
//init data
printf("init pool's data...\n");
pool->minNum = min;
pool->maxNum = max;
pool->busyNum = 0;
pool->liveNum = min;
pool->queueCapacity = queueCapacity;
pool->shutDown = 0;
pool->exitNum = 0;
pool->Rear = 0;
pool->Front = 0;
printf("data init successful!\n");
//init threadIDs
pool->threadIDs = (pthread_t*)malloc(sizeof(pthread_t) * max);
if (pool->threadIDs == NULL) {
printf("init threadIDs fail....\n");
break;
}
memset(pool->threadIDs, 0, sizeof(pthread_t) * max);
pthread_create(&pool->managerID, NULL, manager, pool); //manager othres
printf("create some work threads...\n");
for (int i = 0; i < min; ++i) {
pthread_create(&pool->threadIDs[i], NULL, worker, pool);
}
//init mutex
if (pthread_mutex_init(&pool->mutexPool, NULL) != NULL ||\
pthread_mutex_init(&pool->mutexBusy, NULL) != NULL ||\
pthread_cond_init(&pool->notEmpty, NULL) != NULL ||\
pthread_cond_init(&pool->notFull, NULL) != NULL) {
printf("init mutex fail...\n");
break;
}
return pool;
} while(0);
if (pool->threadIDs) free(pool->threadIDs);
if (pool->taskQ) free(pool->taskQ);
if (pool) free(pool);
return NULL;
}
void manager(void* arg) {//its manager func with a void* param
Threadpool* pool = (Threadpool*)arg;
printf("manager thread start working...\n");
while (!pool->shutDown) {
sleep(3);//every 3 second, it will check the pool,and this thread run all the
//time,until the pool changes the shutdown flag
//take data from threadpool
//before, we must lock the pool
pthread_mutex_lock(&pool->mutexPool);
int liveNum = pool->liveNum;
int queueSize = pool->queueSize;
pthread_mutex_unlock(&pool->mutexPool);
pthread_mutex_lock(&pool->mutexBusy);
int busyNum = pool->busyNum;
pthread_mutex_unlock(&pool->mutexBusy);
//to create some threads?
if (queueSize > liveNum && liveNum < pool->maxNum) {
int counter= 0;
pthread_mutex_lock(&pool->mutexPool);
for (int i = 0; i < pool->maxNum && counter < NUMBER && pool->liveNum < pool->maxNum; ++i) {
if (pool->threadIDs[i] == 0) {
counter++;
pthread_create(&pool->threadIDs[i], NULL, worker, pool);//there is a worker func
pool->liveNum++;
}
}
pthread_mutex_unlock(&pool->mutexPool);
}//ends when to create
//to kill some threads?
if (liveNum > busyNum * 2 && liveNum > pool->minNum) {
//set number or flag
pthread_mutex_lock(&pool->mutexPool);
pool->exitNum = NUMBER;
pthread_mutex_unlock(&pool->mutexPool);
//sig the threads to kill themself
for (int i = 0; i < NUMBER; ++i) {
pthread_cond_signal(&pool->notEmpty);
}
}
}//ends whilt
return;
}
void worker(void* arg) {
Threadpool* pool = (Threadpool*)arg;
printf("an busy work thread add...\n");
while (1) {
pthread_mutex_lock(&pool->mutexPool);
while (pool->queueSize == 0 && !pool->shutDown) {
pthread_cond_wait(&pool->notEmpty, &pool->mutexPool);
if (pool->exitNum > 0) {
pool->exitNum--;
if (pool->liveNum > pool->minNum) {
pool->liveNum--;
printf("exit extra thread...\n");
pthread_mutex_unlock(&pool->mutexPool);
exitThread(pool);
}
}
}//cond while
if (pool->shutDown) {
pthread_mutex_unlock(&pool->mutexPool);
exitThread(pool);
}
Task task;
task.func = pool->taskQ[pool->Front].func;
task.arg = pool->taskQ[pool->Front].arg;
pool->Front = (pool->Front + 1) % pool->queueCapacity;
pool->queueSize--;
pthread_cond_signal(&pool->notFull);
pthread_mutex_unlock(&pool->mutexPool);
printf("thread %ld start working...\n", pthread_self());
pthread_mutex_lock(&pool->mutexBusy);
pool->busyNum++;
pthread_mutex_unlock(&pool->mutexBusy);
task.func(task.arg);
free(task.arg);
task.arg = NULL;
printf("thread %ld end working....\n", pthread_self());
pthread_mutex_lock(&pool->mutexBusy);
pool->busyNum--;
pthread_mutex_unlock(&pool->mutexBusy);
}
return;
}
//add some task into taskQ
void threadAdd(Threadpool* pool, void (*func)(void*), void* arg) {
pthread_mutex_lock(&pool->mutexPool);
while (pool->queueSize == pool->queueCapacity && !pool->shutDown) {
pthread_cond_wait(&pool->notFull, &pool->mutexPool);
}
if (pool->shutDown) {
pthread_mutex_unlock(&pool->mutexPool);
return;
}
pool->taskQ[pool->Rear].func = func;
pool->taskQ[pool->Rear].arg = arg;
pool->Rear = (pool->Rear + 1) % pool->queueCapacity;
pool->queueSize++;
pthread_cond_signal(&pool->notEmpty);//notify
pthread_mutex_unlock(&pool->mutexPool);
}
//its time to exit thread
void exitThread(Threadpool* pool) {
pthread_t tid = pthread_self();
for (int i = 0; i < pool->maxNum; ++i) {
if (pool->threadIDs[i] == tid) {
pool->threadIDs[i] = 0;
printf("exitThread() called, thread %ld end...\n", tid);
pthread_exit(NULL);
}
}
}
int threadPoolDestroyed(Threadpool* pool) {
//switch off the pool
if (pool == NULL) return -1;
pool->shutDown = 1;
//collect manager thread
printf("wait 4 the managerID shut down...\n");
pthread_join(pool->managerID, NULL);
printf("the manager thread shutdow successfully!\n");
//destroy live threads
printf("destory all the live thread...\n");
for (int i = 0; i < pool->liveNum; ++i) {
pthread_cond_signal(&pool->notEmpty);
}
//destroy the taskQ
if (pool->taskQ) {
printf("release taskQ...\n");
free(pool->taskQ);
pool->taskQ = NULL;
}
if (pool->threadIDs) {
printf("release threadIDs...\n");
free(pool->threadIDs);
pool->threadIDs = NULL;
}
//destroy the mutex
printf("release mutex....\n");
pthread_mutex_destroy(&pool->mutexPool);
pthread_mutex_destroy(&pool->mutexBusy);
printf("relese cond...\n");
pthread_cond_destroy(&pool->notEmpty);
pthread_cond_destroy(&pool->notFull);
printf("desroty the pool...\n");
free(pool);
pool = NULL;
return 1;
}