-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadpool.stlQueue.hpp
More file actions
161 lines (119 loc) · 3.18 KB
/
Threadpool.stlQueue.hpp
File metadata and controls
161 lines (119 loc) · 3.18 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
#include <list>
#include <vector>
#include <thread>
#include <functional> // for std::function, std::bind
#include <assert.h>
#include <future>
#include <atomic>
using namespace std;
class ThreadPool
{
public:
using Task = std::function<void()>;
public:
ThreadPool(uint32_t threadNum):ThreadPool(threadNum, threadNum){};
ThreadPool(uint32_t threadNum, uint32_t queueSize);
~ThreadPool();
public:
template <class Function, class... Args>
std::future<typename std::result_of<Function(Args...)>::type> addTask(Function &&, Args &&...);
void stop();
uint32_t size();
Task take();
private:
void *threadFunc(void* arg);
private:
ThreadPool &operator=(const ThreadPool &);
ThreadPool(const ThreadPool &);
private:
std::atomic<bool> isRunning_;
std::vector<std::thread> threads_;
std::mutex mutex_;
std::condition_variable condition_;
std::list<Task> taskQueue_;
uint32_t threadsNum_;
uint32_t queueSize_ = 0;
};
ThreadPool::ThreadPool(uint32_t threadNum, uint32_t queueSize)
{
isRunning_.store(true, std::memory_order_release);
threadsNum_ = threadNum;
for (uint32_t i = 0; i < threadsNum_; i++)
{
threads_.push_back(thread(&ThreadPool::threadFunc, this, nullptr));
}
}
ThreadPool::~ThreadPool()
{
stop();
}
template <class Function, class... Args>
std::future<typename std::result_of<Function(Args...)>::type>
ThreadPool::addTask(Function &&fcn, Args &&... args)
{
using retType = typename std::result_of<Function(Args...)>::type;
using asyncTaskType = std::packaged_task<retType()>;
asyncTaskType ptask(std::bind(fcn, args...));
shared_ptr<asyncTaskType> t = std::make_shared<asyncTaskType>(std::move(ptask));
auto ret = t->get_future();
{
std::lock_guard<std::mutex> lg(mutex_);
taskQueue_.push_back([t]() { (*t)(); });
condition_.notify_one();
}
return ret;
}
void ThreadPool::stop()
{
if (!isRunning_.load(std::memory_order_consume))
{
return;
}
isRunning_.store(false, std::memory_order_release);
condition_.notify_all();
for (size_t i = 0; i != threads_.size(); ++i)
{
if (threads_[i].joinable())
{
threads_[i].join();
}
}
}
uint32_t ThreadPool::size()
{
std::unique_lock<std::mutex> ulk(this->mutex_);
uint32_t size = taskQueue_.size();
return size;
}
ThreadPool::Task ThreadPool::take()
{
Task task = NULL;
std::unique_lock<std::mutex> ulk(this->mutex_);
condition_.wait(ulk, [this] {
return !isRunning_.load(std::memory_order_consume)
|| !this->taskQueue_.empty(); });
if (!isRunning_.load(std::memory_order_consume))
{
ulk.unlock();
return task;
}
assert(!taskQueue_.empty());
task = taskQueue_.front();
taskQueue_.pop_front();
return task;
}
void *ThreadPool::threadFunc(void *arg)
{
auto &&pool = this;
while (pool->isRunning_)
{
ThreadPool::Task task = pool->take();
if (!task)
{
break;
}
assert(task);
task();
}
return 0;
}