forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_thread_pool.hpp
140 lines (118 loc) · 3.59 KB
/
sf_thread_pool.hpp
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
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_thread_pool.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
/*
* sf_thread_pool 线程池
*/
#pragma once
#include "sf_thread_pool.h"
namespace skyfire
{
inline sf_thread_pool::sf_thread_pool(size_t thread_count) {
add_thread__(thread_count);
}
inline void sf_thread_pool::thread_run__(sf_thread_pool *this__) {
while (true)
{
while ((!this__->is_exit__) && (this__->is_pause__ || this__->task_deque__.empty()))
{
++this__->busy_thread_num__;
std::unique_lock<std::mutex> lck_cv(this__->mu_thread__cv__);
this__->thread_cv__.wait(lck_cv);
--this__->busy_thread_num__;
}
if (this__->is_exit__)
{
break;
}
{
auto flag_empty = true;
std::function<void()> task;
{
std::lock_guard<std::mutex> lck(this__->mu_task_deque__);
if (!this__->task_deque__.empty())
{
flag_empty = false;
task = this__->task_deque__.front();
this__->task_deque__.pop_front();
}
}
if (!flag_empty)
{
task();
this__->wait_finish_cv__.notify_all();
}
}
}
}
inline void sf_thread_pool::add_thread__(size_t num) {
if (num < 1)
{
num = 1;
}
thread_count__ += num;
for (auto i = 0; i < num; ++i)
{
auto p_thread = std::make_shared<std::thread>(thread_run__, this);
thread_vec__.push_back(p_thread);
}
}
inline void sf_thread_pool::wait_all_task_finished() {
while (!task_deque__.empty())
{
std::unique_lock<std::mutex> lck_cv(mu_wait_finish__);
wait_finish_cv__.wait(lck_cv);
}
}
inline void sf_thread_pool::clear_task() {
std::lock_guard<std::mutex> lck(mu_task_deque__);
task_deque__.clear();
}
inline void sf_thread_pool::clear_thread() {
is_pause__ = false;
is_exit__ = true;
thread_cv__.notify_all();
for (auto &p : thread_vec__)
{
p->join();
}
thread_vec__.clear();
}
inline size_t sf_thread_pool::get_busy_thread_count() const {
return static_cast<size_t>(busy_thread_num__);
}
inline size_t sf_thread_pool::get_thread_count() const {
return thread_count__;
}
inline void sf_thread_pool::add_thread(size_t thread_num) {
add_thread__(thread_num);
}
inline void sf_thread_pool::resume() {
is_pause__ = false;
thread_cv__.notify_all();
}
template<typename Func, typename... T>
void sf_thread_pool::add_task(Func func, T &&... args) {
std::lock_guard<std::mutex> lck(mu_task_deque__);
task_deque__.push_back(std::bind(func, std::forward<T>(args)...));
thread_cv__.notify_all();
}
inline sf_thread_pool::~sf_thread_pool() {
clear_thread();
}
inline void sf_thread_pool::add_task(std::function<void()> func) {
std::lock_guard<std::mutex> lck(mu_task_deque__);
task_deque__.push_back(func);
thread_cv__.notify_all();
}
inline void sf_thread_pool::pause() {
is_pause__ = true;
}
}