Repository navigation
Expand file tree
/
Copy paththreadpool.cc
More file actions
278 lines (235 loc) · 7.29 KB
/
Copy paththreadpool.cc
File metadata and controls
278 lines (235 loc) · 7.29 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "threadpool.h"
#include <functional>
#include <thread>
#include <memory>
#include <chrono>
#include <iostream>
const int TASK_MAX_THRESHHOLD = INT32_MAX;
const int THREAD_MAX_THRESHHOLD = 10;
ThreadPool::ThreadPool()
: initThreadSize_(0)
, taskSizeThreadHold_(TASK_MAX_THRESHHOLD)
, poolmode_(PoolMode::MODE_FIXED)
, isPoolRunning_(false)
, idleThreadSize_(0)
, threadSizeThreshold_(THREAD_MAX_THRESHHOLD)
, curThreadSize_(0)
{}
ThreadPool::~ThreadPool()
{
isPoolRunning_ = false;
notEmpty_.notify_all();
// 等待线程池里面的线程都返回 状态: 阻塞状态 | 正在执行状态
std::unique_lock<std::mutex> lock(taskQueMtx);
exitCond_.wait(lock,[&]()->bool{ return threads_.size() == 0;});
}
// 设置线程池的工作模式
void ThreadPool::setMode(PoolMode mode)
{
if (checkRunningState())
{
return;
}
poolmode_ = mode;
}
// 设置task任务队列的上线阈值
void ThreadPool::setTaskQueMaxThreshold(int threshold)
{
if (checkRunningState())
{
return;
}
taskSizeThreadHold_ = threshold;
}
void ThreadPool::setThreadSizeThreshHold(int threshold)
{
if (checkRunningState())
{
return;
}
if (poolmode_ == PoolMode::MODE_CACHED)
{
threadSizeThreshold_ = threshold;
}
}
bool ThreadPool::checkRunningState() const
{
return isPoolRunning_;
}
// 给线程池提交任务
Result ThreadPool::submitTask(std::shared_ptr<Task> sp)
{
// 获取锁
std::unique_lock<std::mutex> lock(taskQueMtx);
// 线程通信 等待
// 用户提交任务,最多不能阻塞超过一秒,否则判定提交失败
if (!notFull_.wait_for(lock,std::chrono::seconds(1),
[&]()->bool{ return taskQueue_.size() == taskSizeThreadHold_ ;}))
{
std::cerr << "task queue is full, submit task fail." << std::endl;
return Result(sp,false);
}
// 如果有空余,把任务放入任务队列
taskQueue_.emplace(sp);
taskSize_++;
// 因为新放了任务,任务队列就非空,notEmppty通知
notEmpty_.notify_all();
// cache: 需要根据任务数量和空闲线程的数量,判断是否需要创建新的线程出来
if (poolmode_ == PoolMode::MODE_CACHED
&& taskSize_ > idleThreadSize_
&& curThreadSize_ > threadSizeThreshold_)
{
auto ptr = std::unique_ptr<Thread>(new Thread(std::bind(&ThreadPool::threadFunc,this,std::placeholders::_1)));
int threadID = ptr->getID();
threads_.emplace(threadID,std::move(ptr));
threads_[threadID]->start();
curThreadSize_++;
idleThreadSize_++;
}
// 返回任务的Result对象
return Result(sp);
}
// 开启线程池
void ThreadPool::start(int initThreadSize)
{
// 设置线程池的启动状态
isPoolRunning_= true;
// 记录初始线程个数
initThreadSize_ = initThreadSize;
curThreadSize_ = initThreadSize;
// 创建线程对象
for (int i = 0; i < initThreadSize; i++)
{
auto ptr = std::unique_ptr<Thread>(new Thread(std::bind(&ThreadPool::threadFunc,this,std::placeholders::_1)));
int threadID = ptr->getID();
threads_.emplace(threadID,std::move(ptr));
}
// 启动所有线程
for (int i = 0; i < initThreadSize; i++)
{
threads_[i]->start(); // 执行一个线程函数
idleThreadSize_++; // 记录初始线程的数量
}
}
void ThreadPool::threadFunc(int threadID)
{
auto lastTime = std::chrono::high_resolution_clock().now();
// 所有任务必须完成,线程池才可以回收所有资源
for(;;)
{
std::shared_ptr<Task> task;
{
// 先获取锁
std::unique_lock<std::mutex> lock(taskQueMtx);
// 每一秒中返回一次 怎么区分:超时返回?还是有任务待执行返回
while (taskQueue_.size() == 0)
{
if (!isPoolRunning_)
{
threads_.erase(threadID);
std::cout << "threadid:"<< std::this_thread::get_id() << "exit !" << std::endl;
exitCond_.notify_all();
return;
}
if(poolmode_ == PoolMode::MODE_CACHED)
{
// 条件变量, 超时返回了
if(std::cv_status::timeout == notEmpty_.wait_for(lock,std::chrono::seconds(1)))
{
auto now = std::chrono::high_resolution_clock().now();
auto dur = std::chrono::duration_cast<std::chrono::seconds>(now - lastTime);
if (dur.count() >= 60
&& curThreadSize_ > initThreadSize_)
{
// 开始回收当前线程
// 记录线程数量的相关变量的只修改
// 把线程对象从线程列表容器中删除
// threadid => thread对象 => 删除
threads_.erase(threadID);
curThreadSize_--;
idleThreadSize_--;
std::cout << "threadid:"<< std::this_thread::get_id() << "exit !" << std::endl;
return;
}
}
}
else
{
// 等待notEmpty条件
notEmpty_.wait(lock);
}
}
idleThreadSize_--;
// 从任务队列中取出一个任务
task = taskQueue_.front();
taskQueue_.pop();
taskSize_--;
// 如果依然有剩余任务,继续通知其他线程执行任务
if (taskQueue_.size() > 0)
{
notEmpty_.notify_all();
}
// 取出一个任务,进行通知,通知可以继续提交任务
notFull_.notify_all();
}
// 当前线程执行这个任务
if ( task != nullptr)
{
// task->run(); // 执行任务; 把任务的返回值setVal方法给到Result
task->exec();
}
idleThreadSize_++;
lastTime = std::chrono::high_resolution_clock().now();
}
}
// ------- 线程方法实现 --------
int generateID_ = 0;
int Thread::getID() const
{
return threadID_;
}
Thread::Thread(ThreadFunc func)
: threadFunc_(func)
, threadID_(generateID_++)
{}
void Thread::start()
{
// 创建一个线程来执行一个线程函数
std::thread t(threadFunc_,threadID_);
// 设置分离线程
t.detach();
}
// --------- Task方法实现 ------
Task::Task()
: result_(nullptr)
{}
void Task::exec()
{
result_->setVal(run());
}
void Task::setResult(Result *res)
{
result_ = res;
}
// --------- Result方法实现 ------
Result::Result(std::shared_ptr<Task> task, bool isValid)
: isVaild_(isValid)
, task_(task)
{
task->setResult(this);
}
Any Result::get()
{
if (!isVaild_)
{
return "";
}
sem_.wait();
return std::move(any_);
}
void Result::setVal(Any any)
{
// 存储task的返回值
this->any_ = std::move(any);
sem_.post();
}