-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadpool.h
More file actions
165 lines (139 loc) · 3.63 KB
/
threadpool.h
File metadata and controls
165 lines (139 loc) · 3.63 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
#pragma once
#include <thread>
#include <vector>
#include <deque>
#include <atomic>
#include <mutex>
#include <memory>
#include <functional>
#include <condition_variable>
template<class T,class C = std::deque<T>>
class Threadpool
{
private:
std::vector<std::thread> Threads;
C WorkItems;
std::mutex WorkItemsLock;
std::recursive_mutex WaitLock;
std::condition_variable_any WaitVariable;
std::atomic_bool Stopping;
std::atomic_bool Stopped;
unsigned int ThreadCount;
void ThreadFunc();
void Initialize();
std::pair<bool,T> GetWorkItemThread();
std::function<void(T&&)> ProcessWorkItem;
public:
Threadpool() = delete;
Threadpool(const Threadpool&) = delete;
Threadpool(unsigned int ThreadCount, std::function<void(T&&)>&& ProcessWorkitem);
~Threadpool();
void EnqueueWorkItem(T&& WorkItem);
void Stop();
unsigned int GetThreadCount() { return ThreadCount; }
auto GetWorkItemCount() { std::lock_guard<std::mutex> lock(WorkItemsLock); return WorkItems.size(); }
};
template<class T,class C>
Threadpool<T,C>::Threadpool(unsigned int ThreadCount, std::function<void(T&&)>&& ProcessWorkitemFunc) :
Threads(ThreadCount),
Stopping(false),
Stopped(false),
ThreadCount(ThreadCount),
ProcessWorkItem(std::move(ProcessWorkitemFunc))
{
Initialize();
}
template<class T,class C>
Threadpool<T,C>::~Threadpool()
{
Stop();
}
template<class T,class C>
inline void Threadpool<T,C>::Initialize()
{
for (unsigned i = 0; i < ThreadCount; i++)
{
Threads[i] = std::move(std::thread(&Threadpool::ThreadFunc, this));
}
}
template<class T,class C>
inline void Threadpool<T,C>::Stop()
{
if (Stopped || Stopping)
{
return;
}
Stopping = true;
// Drain any remaining workitems
WorkItemsLock.lock();
while (WorkItems.size() > 0)
{
WorkItemsLock.unlock();
{
std::unique_lock<std::recursive_mutex> lock(WaitLock);
WaitVariable.notify_all();
}
WorkItemsLock.lock();
}
WorkItemsLock.unlock();
Stopped = true;
{
std::unique_lock<std::recursive_mutex> lock(WaitLock);
WaitVariable.notify_all();
}
for (unsigned i = 0; i < Threads.size(); i++)
{
if (Threads[i].joinable())
{
Threads[i].join();
}
}
WorkItems.clear();
Threads.clear();
}
template<class T,class C>
inline std::pair<bool,T> Threadpool<T,C>::GetWorkItemThread()
{
std::lock_guard<std::mutex> lock(WorkItemsLock);
if (WorkItems.size() == 0)
{
return std::make_pair<bool, T>(false, {});
}
std::pair<bool,T> Workitem(true, std::move(WorkItems.front()));
WorkItems.pop_front();
return Workitem;
}
template<class T,class C>
inline void Threadpool<T,C>::ThreadFunc()
{
while (!Stopped)
{
{
std::unique_lock<std::recursive_mutex> lock(WaitLock);
// Wait for work to do/wait to exit
WaitVariable.wait(lock, [this] () -> bool { return (WorkItems.size() > 0) || Stopped; });
}
// check WorkItems, and process a workitem
std::pair<bool, T> Workitem = GetWorkItemThread();
if (Workitem.first)
{
ProcessWorkItem(std::move(Workitem.second));
}
}
}
template<class T,class C>
inline void Threadpool<T,C>::EnqueueWorkItem(T&& WorkItem)
{
if (Stopping)
{
return;
}
{
std::lock_guard<std::mutex> lock(WorkItemsLock);
WorkItems.push_back(std::move(WorkItem));
}
{
std::unique_lock<std::recursive_mutex> lock(WaitLock);
WaitVariable.notify_all();
}
}