-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadpool.casQueue.hpp
More file actions
168 lines (124 loc) · 3.53 KB
/
Threadpool.casQueue.hpp
File metadata and controls
168 lines (124 loc) · 3.53 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
162
163
164
165
166
167
168
#include <deque>
#include <thread>
#include <functional>
#include <assert.h>
#include <future>
#include <atomic>
#include "RingQueue.hpp"
using namespace std;
using namespace rq;
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 &&...);
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 emptycond_;
std::condition_variable fullcond_;
rq::LockFreeRingQueue<Task> taskQueue_;
uint32_t threadsNum_;
};
ThreadPool::ThreadPool(uint32_t threadNum, uint32_t queueSize):taskQueue_(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()
{
if (!isRunning_.load(std::memory_order_consume))
{
return;
}
isRunning_.store(false, std::memory_order_release);
emptycond_.notify_all();
for (size_t i = 0; i != threads_.size(); ++i)
{
if (threads_[i].joinable())
{
threads_[i].join();
}
}
}
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;
{
std::unique_lock<std::mutex> ulk(this->mutex_);
fullcond_.wait(ulk, [this] {
return !isRunning_.load(std::memory_order_consume)
|| (taskQueue_.size() != taskQueue_.capicity()); });
if (!isRunning_.load(std::memory_order_consume))
{
ulk.unlock();
return std::future<retType>();
}
}
using asyncTaskType = std::packaged_task<retType()>;
auto t = std::make_shared<asyncTaskType>(std::bind(fcn, args...));
taskQueue_.enQueue( [t]() { (*t)(); });
emptycond_.notify_one();
return t->get_future();
}
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 = {0};
{
std::unique_lock<std::mutex> ulk(this->mutex_);
emptycond_.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());
// TODO
taskQueue_.deQueue(task);
}
return task;
}
void *ThreadPool::threadFunc(void *arg)
{
auto &&pool = this;
while (pool->isRunning_)
{
ThreadPool::Task &&task = pool->take();
if (!task)
{
break;
}
fullcond_.notify_all();
assert(task);
task();
}
return 0;
}