forked from 834810071/NetworkProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
161 lines (146 loc) · 4.19 KB
/
threadpool.cpp
File metadata and controls
161 lines (146 loc) · 4.19 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
//
// Created by jxq on 19-8-15.
//
// p41 线程池
#include "threadpool.h"
#include <pthread.h>
#include <memory>
#include <iostream>
using namespace std;
void *thread_routine (void *arg)
{
struct timespec abstime;
int timeout;
printf("thread 0x%x is starting\n", (int)pthread_self());
threadpool_t *pool = (threadpool_t*)arg;
while (1)
{
timeout = 0;
condition_lock(&pool->ready);
++pool->idle;
// 等待队列有任务到来或者线程池销毁通知
while (pool->first == NULL && !pool->quit)
{
printf("thread 0x%x is waiting\n", (int)pthread_self());
//condition_wait(&pool->ready);
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += 2;
// 超时设置2秒
int state = condition_timewait(&pool->ready, &abstime);
if (state == ETIMEDOUT)
{
printf("thread 0x%x is wait time out\n", (int)pthread_self());
timeout = 1;
break;
}
}
// 等待到条件,处于工作状态
--pool->idle;
if (pool->first != NULL)
{
// 从队头取出任务
task_t *t = pool->first;
pool->first = t->next;
// 执行任务需要一定的时间,所以需要先解锁,以便生产者进程
// 能够往队列中天剑任务,其他消费者线程能够进入等待任务
condition_unlock(&pool->ready);
t->run(t->arg);
free(t);
condition_lock(&pool->ready);
}
// 如果等待到销毁线程池通知,且任务都执行完毕
if (pool->quit && pool->first == NULL)
{
pool->counter--;
if (pool->counter == 0)
{
condition_signal(&pool->ready);
}
condition_unlock(&pool->ready);
// 跳出循环之前记得解锁
break;
}
if (timeout && pool->first == NULL)
{
pool->counter--;
condition_unlock(&pool->ready);
// 跳出循环之前记得解锁
break;
}
condition_unlock(&pool->ready);
}
printf("thread 0x%x is exiting \n", (int)pthread_self());
return NULL;
}
// 初始化线程池
void threadpool_init(threadpool_t *pool, int threads)
{
// pool = (threadpool_t*)malloc(sizeof(pool));
condition_init(&(pool->ready));
pool->first = NULL;
pool->last = NULL;
pool->counter = 0;
pool->idle = 0;
pool->max_threads = threads;
pool->quit = 0;
}
// 在线程池中添加任务
void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg)
{
// 生成新任务
task_t* newtask = (task_t *)malloc(sizeof(task_t));
//cout << (char *)pool->first->arg << endl;
newtask->run = run;
newtask->arg = arg;
newtask->next = NULL;
condition_lock(&pool->ready);
// 将任务添加到队列中
if (pool->first == NULL)
{
pool->first = newtask;
}
else
{
pool->last->next = newtask;
}
pool->last = newtask;
// 如果有等待任务的线程,则唤醒其中一个
if (pool->idle > 0)
{
condition_signal(&pool->ready);
}
else if (pool->counter < pool->max_threads)
{
// 没有等待线程,并且当前线程数不超过最大线程数,则创建一个新线程
pthread_t tid;
pthread_create(&tid, NULL, thread_routine, pool);
++pool->counter;
}
condition_unlock(&pool->ready);
}
// 销毁线程池
void threadpool_destroy(threadpool_t *pool)
{
if (pool->quit)
{
return;
}
condition_lock(&pool->ready);
pool->quit = 1;
if (pool->counter > 0)
{
if (pool->idle > 0)
{
condition_broadcast(&pool->ready);
}
// 处于执行任务状态中的线程,不会收到广播
// 线程池需要等执行任务状态中的线程全部退出
while (pool->counter > 0)
{
condition_wait(&pool->ready);
}
}
condition_unlock(&pool->ready);
condition_destroy(&pool->ready);
//free(pool);
}