forked from lotusnowshen/ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
126 lines (111 loc) · 2.69 KB
/
ThreadPool.cpp
File metadata and controls
126 lines (111 loc) · 2.69 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
/*
* ThreadPool.cpp
*
* Created on: Apr 23, 2014
* Author: wing
*/
#include "ThreadPool.h"
/*
* 构造函数除了初始化每个成员变量之外
* 还需要依次对线程数组中的每个线程注册自身
*/
ThreadPool::ThreadPool(std::vector<WorkThread>::size_type max_thread) :
_task_queue(), _max_thread(max_thread), _thread_vector(_max_thread), _is_started(
false), _lock(), _cond(&_lock)
{
for (std::vector<WorkThread>::iterator iter = _thread_vector.begin();
iter != _thread_vector.end(); ++iter) {
//注册线程池
iter->register_thread_pool(this);
}
}
ThreadPool::~ThreadPool() {
stop_thread_pool();
}
void ThreadPool::start_thread_pool() {
if (_is_started == false) {
/*
* 下面这行代码必须放在开头,防止线程启动后
* 因为is_start这个变量异常退出
*/
_is_started = true; // first
for (std::vector<WorkThread>::iterator iter = _thread_vector.begin();
iter != _thread_vector.end(); ++iter) {
iter->start();
}
}
}
void ThreadPool::stop_thread_pool() {
if (_is_started == false) {
return;
}
/*
* 这句话同样必须放在开头,以便于线程及时退出
*/
_is_started = false;
/*
* 用来激活所有正在等待任务的线程
* 让它们退出循环,同时检测到布尔值的变化
* 而使得线程退出
*/
_cond.notify_all();
for (std::vector<WorkThread>::iterator iter = _thread_vector.begin();
iter != _thread_vector.end(); ++iter) {
iter->join();
}
//清空队列
while(!_task_queue.empty()){
_task_queue.pop();
}
}
bool ThreadPool::get_task_queue(Task &task) {
_lock.lock();
/*
* 这里必须使用while循环
*/
while (_is_started && _task_queue.empty()) {
_cond.wait();
}
/*
* 这里布尔值为false存在三种情况:
* 1.运行到这里线程池还没有开启
* 2.线程拿到了任务,退出了循环,但是线程池此刻关闭
* 3.线程正在等待任务,被stop函数中的notify_all函数
* 所激活,此时需要退出
*/
if (_is_started == false) {
_lock.unlock();
return false;
}
task = _task_queue.front();
_task_queue.pop();
_lock.unlock();
return true;
}
/*
* 向任务队列中添加任务
*/
bool ThreadPool::add_task_queue(Task task) {
_lock.lock();
bool ret = false;
if (_is_started) {
_task_queue.push(task);
//激活某个正在等待任务的线程
_cond.notify();
ret = true;
}
_lock.unlock();
return ret;
}
bool ThreadPool::is_task_queue_empty() const{
_lock.lock();
bool ret = _task_queue.empty();
_lock.unlock();
return ret;
}
std::queue<Task>::size_type ThreadPool::get_task_queue_size() const{
_lock.lock();
std::queue<Task>::size_type ret = _task_queue.size();
_lock.unlock();
return ret;
}