-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThreadPool.cpp
93 lines (80 loc) · 1.72 KB
/
ThreadPool.cpp
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
#include "ThreadPool.h"
ThreadPool::ThreadPool(int threads_size)
:m_threads_size(threads_size),m_mutex(),m_cond(),m_started(false)
{
start();
}
ThreadPool::~ThreadPool()
{
if(m_started)
{
stop();
}
}
//线程池开始
void ThreadPool::start()
{
assert(m_threads.empty());
assert(!m_started);
m_started = true;
m_threads.reserve(m_threads_size);
for(int i=0;i<m_threads_size;++i)
{
m_threads.push_back(new std::thread(std::bind(&ThreadPool::threadLoop,this)));
}
}
//线程池终止
void ThreadPool::stop()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_started = false;
m_cond.notify_all();
for(auto it= m_threads.begin();it!=m_threads.end();++it)
{
(*it)->join();
delete *it;
}
m_threads.clear();
}
//线程池循环
void ThreadPool::threadLoop()
{
while(m_started)
{
Task_type task = take();
if(task)
task();
}
}
//添加任务
void ThreadPool::addTask(const Task_type& task)
{
std::unique_lock<std::mutex> lock(m_mutex);
TaskPair taskPair(MIDDLE,task);
m_tasks.emplace(taskPair);
m_cond.notify_one();
}
void ThreadPool::addTask(const TaskPair& taskPair)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_tasks.emplace(taskPair);
m_cond.notify_one();
}
//从任务队列拿任务
ThreadPool::Task_type ThreadPool::take()
{
std::unique_lock<std::mutex> lock(m_mutex);
while(m_tasks.empty()&&m_started)
{
m_cond.wait(lock);
}
Task_type task;
int size = m_tasks.size();
if(!m_tasks.empty()&&m_started)
{
task = m_tasks.top().second;
m_tasks.pop();
assert(size -1 == m_tasks.size());
}
return task;
}