-
Notifications
You must be signed in to change notification settings - Fork 7
/
threadpool.h
233 lines (189 loc) · 6.44 KB
/
threadpool.h
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
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
//利用宏定义实现类似c++的template功能
//使用do - while(0) 是为了解决宏定义时代码拷贝时后面的分号问题
#define LL_ADD(item, list) do { \
item->prev = NULL; \
item->next = list; \
if (list != NULL) list->prev = item; \
list = item; \
} while(0)
#define LL_REMOVE(item, list) do { \
if (item->prev != NULL) item->prev->next = item->next; \
if (item->next != NULL) item->next->prev = item->prev; \
if (list == item) list = item->next; \
item->prev = item->next = NULL; \
} while (0)
//执行队列
struct NWORKER{
pthread_t threadid;
struct NMANAGER *pool;
int terminate;
int isWorking;
struct NWORKER *prev;
struct NWORKER *next;
};
//任务队列
struct NJOB{
void (*func)(void *arg); //任务函数
void *user_data;
struct NJOB *prev;
struct NJOB *next;
};
//线程与任务管理
struct NMANAGER{
struct NWORKER *workers;
struct NJOB *jobs;
int sum_thread;
int free_thread;
pthread_cond_t jobs_cond; //线程条件等待
pthread_mutex_t jobs_mutex; //为任务加锁防止一个任务被两个线程执行
};
typedef struct NMANAGER nThreadPool;
int nThreadPoolDelWorker(nThreadPool *pool, int num);
int nThreadPoolAddWorker(nThreadPool *pool, int num);
static void *nThreadCallBack(void *arg){
struct NWORKER *worker = (struct NWORKER*)arg;
while (1){
//线程只有两个状态:执行\等待
pthread_mutex_lock(&worker->pool->jobs_mutex);
while (worker->pool->jobs == NULL) {
if (worker->terminate) break;
pthread_cond_wait(&worker->pool->jobs_cond, &worker->pool->jobs_mutex);
}
if (worker->terminate){
pthread_mutex_unlock(&worker->pool->jobs_mutex);
break;
}
struct NJOB *job = worker->pool->jobs;
LL_REMOVE(job, worker->pool->jobs);
pthread_mutex_unlock(&worker->pool->jobs_mutex);
worker->pool->free_thread--;
if ((float)(worker->pool->free_thread / worker->pool->sum_thread) > 0.8){
nThreadPoolDelWorker(worker->pool, worker->pool->sum_thread - worker->pool->free_thread * 2);
}
worker->isWorking = 1;
job->func(job->user_data);
worker->isWorking = 0;
worker->pool->free_thread++;
if ((float)(worker->pool->free_thread / worker->pool->sum_thread) < 0.2){
nThreadPoolAddWorker(worker->pool, worker->pool->free_thread * 2 - worker->pool->sum_thread);
}
free(job->user_data);
free(job);
}
free(worker);
pthread_exit(NULL);
}
//线程池初始化
int nThreadPoolCreate(nThreadPool *pool, int numWorkers){ //numWorkers:线程数量
if (numWorkers < 1) numWorkers = 1;
if (pool == NULL) return -1;
memset(pool, 0, sizeof(nThreadPool));
//初始化jobs_cond
pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
memcpy(&pool->jobs_cond, &blank_cond, sizeof(pthread_cond_t));
//初始化jobs_mutex
pthread_mutex_t blank_muntex = PTHREAD_MUTEX_INITIALIZER;
memcpy(&pool->jobs_mutex, &blank_muntex, sizeof(pthread_mutex_t));
//初始化workers
for (int i = 0; i < numWorkers; ++i){
struct NWORKER *worker = (struct NWORKER*)malloc(sizeof(struct NWORKER));
if (worker == NULL){
perror("malloc");
return -2;
}
memset(worker, 0, sizeof(struct NWORKER));
worker->pool = pool;
int ret = pthread_create(&worker->threadid, NULL, nThreadCallBack, worker);
if (ret){
perror("pthread_create");
struct NWORKER *w = pool->workers;
for (w = pool->workers; w != NULL; w = w->next)
w->terminate = 1;
//free(worker);
return -3;
}
worker->terminate = 0;
LL_ADD(worker, pool->workers);
}
pool->sum_thread = numWorkers;
pool->free_thread = numWorkers;
return 1;
}
//销毁线程池
void nThreadPoolDestroy(nThreadPool *pool){
struct NWORKER *worker = NULL;
for (worker = pool->workers; worker != NULL; worker = worker->next){
worker->terminate = 1;
}
pthread_mutex_lock(&pool->jobs_mutex);
pthread_cond_broadcast(&pool->jobs_cond);
pthread_mutex_unlock(&pool->jobs_mutex);
}
//向线程池中添加任务
void nThreadPoolAddJob(nThreadPool *pool, struct NJOB *job){
//唤醒休眠的线程
pthread_mutex_lock(&pool->jobs_mutex);
LL_ADD(job, pool->jobs);
pthread_cond_signal(&pool->jobs_cond);
pthread_mutex_unlock(&pool->jobs_mutex);
}
//面向用户的添加任务
int nThreadPush(nThreadPool *pool,void (*func)(void *), void *arg, int len){
struct NJOB *job = (struct NJOB*)malloc(sizeof(struct NJOB));
if (job == NULL){
perror("malloc");
return -2;
}
memset(job, 0, sizeof(struct NJOB));
job->user_data = malloc(len);
memcpy(job->user_data, arg, len);
job->func = func;
job->next = NULL;
job->prev = NULL;
nThreadPoolAddJob(pool, job);
return 1;
}
//线程池增加线程
int nThreadPoolAddWorker(nThreadPool *pool, int num) {
int ret = 0;
for (int i = 0; i < num; ++i) {
struct NWORKER *worker = (struct NWORKER *) malloc(sizeof(struct NWORKER));
if (worker == NULL) {
perror("malloc");
return -2;
}
memset(worker, 0, sizeof(struct NWORKER));
worker->pool = pool;
int ret = pthread_create(&worker->threadid, NULL, nThreadCallBack, worker);
if (ret) {
printf("error: pthread_create\n");
struct NWORKER *w = pool->workers;
for (w = pool->workers; w != NULL; w = w->next)
w->terminate = 1;
//free(worker);
break;
}
worker->terminate = 0;
LL_ADD(worker, pool->workers);
ret++;
}
pool->sum_thread+=ret;
return ret;
}
//线程池减少线程
int nThreadPoolDelWorker(nThreadPool *pool, int num){
if (num > pool->sum_thread) return -1;
int ret = 0;
struct NWORKER *worker = NULL;
for (worker = pool->workers; worker != NULL && ret < num; worker = worker->next){
if (!worker->isWorking)
worker->terminate = 1;
ret++;
}
pool->sum_thread -= ret;
return ret;
}