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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#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 NJOB{
void(*func)(void *arg);
void *user_data;
struct NJOB *prev;
struct NJOB *next;
};
//执行队列
struct NWORKER{
struct NMANAGER *pool;
pthread_t id;
struct NWORKER *prev;
struct NWORKER *next;
};
typedef struct NMANAGER{
pthread_mutex_t mtx;
pthread_cond_t cond;//条件变量
struct NJOB *jobs;
struct NWORKER *workers;
}nThreadPool;
void *thread_cb(void *arg){
struct NWORKER * worker = (struct NWORKER *)arg;
while(1){
pthread_mutex_lock(&worker->pool->mtx);
while (worker->pool->jobs == NULL)
{
pthread_cond_wait(&worker->pool->cond,&worker->pool->mtx);
}
//取一个job
struct NJOB *job = worker->pool->jobs;
if(job != NULL){
LL_REMOVE(job, worker->pool->jobs);
}
pthread_mutex_unlock(&worker->pool->mtx);//应该放在这里
job->func(job);
}
}
//创建线程池
int nThreadPoolCreate(nThreadPool *pool,int numWorkers)
{
//参数判断
if(numWorkers < 1) numWorkers = 1;
if(pool == NULL) return -1;
memset(pool, 0, sizeof(nThreadPool));
//mutex初始化
pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
memcpy(&pool->mtx, &blank_mutex, sizeof(pthread_mutex_t));
//cond初始化
pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
memcpy(&pool->cond, &blank_cond, sizeof(pthread_cond_t));
//works
int i = 0;
for(i = 0; i < numWorkers; i++)
{
struct NWORKER *worker = (struct NWORKER *)malloc(sizeof(struct NWORKER ));
if(worker == NULL)
{
perror("malloc:Null err");
return -2;
}
memset(worker,0,sizeof(struct NWORKER));
worker->pool = pool;
pthread_create(&worker->id, NULL,thread_cb,worker);
//线程添加到队列
LL_ADD(worker,pool->workers);
}
}
//销毁线程池
int nThreadPoolDestory(nThreadPool *pool)
{
if(pool == NULL) return -1;
struct NWORKER * tmp = pool->workers, *curr;
while(tmp != NULL)
{
pthread_cancel(tmp->id); //杀死线程
curr = tmp;
tmp = tmp ->next;
free(curr);
}
pthread_mutex_destroy(&pool->mtx);
pthread_cond_destroy(&pool->cond);
}
//push任务
int nThreadPoolPushTask(nThreadPool *pool,struct NJOB *job)
{
if(pool == NULL || job == NULL) return -1;
pthread_mutex_lock(&pool->mtx);
LL_ADD(job, pool->jobs);
pthread_cond_signal(&pool->cond);//条件通知
pthread_mutex_unlock(&pool->mtx);
}
#if 1
//测试程序
void jobs_cb(void *arg){
struct NJOB * job = (struct NJOB *)arg;
printf("I am %lu , data = %d\n",pthread_self(),*((int *)job->user_data));
srand(time(0));
usleep(rand()%100000);
//usleep(1000);
}
int main()
{
nThreadPool threadpool;
nThreadPool *pool = &threadpool;
nThreadPoolCreate(pool,10);
int a[1000] = {0};
struct NJOB job[1000];
for(int i = 0; i<1000; ++i)
{
a[i] = i;
job[i].func = jobs_cb;
job[i].user_data = &a[i];
job[i].prev = NULL;
job[i].next = NULL;
//添加任务
nThreadPoolPushTask(pool,&job[i]);
}
sleep(5);
nThreadPoolDestory(pool);
return 0;
}
#endif
|