-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cc
More file actions
85 lines (75 loc) · 1.82 KB
/
Copy pathThreadPool.cc
File metadata and controls
85 lines (75 loc) · 1.82 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
#include "../include/ThreadPool.h"
#include <unistd.h>
ThreadPool::ThreadPool(size_t threadNum, size_t queSize)
: _threadNum(threadNum)
, _queSize(queSize)
, _taskQue(queSize)
, _isExit(false)
{
//预留空间
_threads.reserve(_threadNum);
}
ThreadPool::~ThreadPool()
{
if(!_isExit)
{
stop();
_isExit = true;
}
}
//线程池的启动
void ThreadPool::start()
{
for(size_t idx = 0; idx != _threadNum; ++idx)
{
//function<void()> f = std::bind(&ThreadPool::threadFunc, this);
unique_ptr<Thread> up(new Thread(std::bind(&ThreadPool::threadFunc, this)));
_threads.push_back(std::move(up));//unique_ptr不能复制或者赋值
}
for(auto &th : _threads)
{
th->start();//启动所有的子线程
}
}
void ThreadPool::stop()
{
//先要判断任务有没有执行完毕,没有执行完毕就不让工作线程退出
while(!_taskQue.empty())
{
sleep(1);
}
_isExit = true;
_taskQue.wakeup();//唤醒所有休眠的线程
for(auto &th : _threads)
{
th->stop();//回收所有的子线程
}
}
void ThreadPool::addTask(Task &&task)
{
if(task)
{
_taskQue.push(std::move(task));
}
}
Task ThreadPool::getTask()
{
return _taskQue.pop();
}
//线程池交给工作线程做的任务
void ThreadPool::threadFunc()
{
while(!_isExit)
{
//工作线程取获取任务的时候,如果getTask执行的比较慢,任务是
//可以执行完毕,工作线程也是可以退出来的;但是如果工作线程
//获取任务getTask的速度非常快的时候,会阻塞在getTask上面,
//因为没有任务,就会阻塞
//获取任务
Task taskcb = getTask();
if(taskcb)
{
taskcb();//执行任务
}
}
}