-
Notifications
You must be signed in to change notification settings - Fork 6
/
threadqueue.h
180 lines (142 loc) · 4.71 KB
/
threadqueue.h
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
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Function returns a result version.
*
* Create a certain number of threads in the background and process data as it
* is added to a queue.
*
* Example:
*
* Output function(Input) { return Output(Input); }
* ThreadQueue<Output, Input> ts(function);
* ts.queue(Input);
* // other processing
* std::vector<Output> results = ts.results();
*/
#ifndef H_THREADQUEUE
#define H_THREADQUEUE
#include <queue>
#include <mutex>
#include <atomic>
#include <thread>
#include <vector>
#include <condition_variable>
#include "cores.h"
// Thrown if attempting to add more items to the queue after all the threads
// have exited.
class ThreadsExited { };
// Each of the threads created, just waits for data in the queue and calls the
// function on an item when one is available.
template<class Result, class Item> class Thread
{
Result (*function)(Item);
std::condition_variable& moreData;
std::vector<Result>& r;
std::mutex& rMutex;
std::queue<Item>& q;
std::mutex& qMutex;
std::atomic_bool& waiting;
std::atomic_bool& killed;
public:
Thread(Result (*function)(Item), std::condition_variable& moreData,
std::vector<Result>& r, std::mutex& rMutex, std::queue<Item>& q,
std::mutex& qMutex, std::atomic_bool& waiting, std::atomic_bool& killed)
: function(function), moreData(moreData), r(r), rMutex(rMutex), q(q),
qMutex(qMutex), waiting(waiting), killed(killed) { }
void operator()();
};
template<class Result, class Item> class ThreadQueue
{
// Our thread pool
std::vector<std::thread> pool;
// Are we waiting for the results now? If so, exit once the queue
// becomes empty.
std::atomic_bool waiting;
// Did we already exit, don't allow adding more items to the queue
std::atomic_bool killed;
// Items to process
std::queue<Item> q;
std::mutex qMutex;
// Results
std::vector<Result> r;
std::mutex rMutex;
// Signal we have more data (wake up a thread to process)
std::condition_variable moreData;
public:
// Create a thread queue with a certain number of threads. Normally you'd
// detect and specify the number of CPU cores in the computer. Defaults to
// supposed number of cores if zero. Initialization does not block.
ThreadQueue(Result (*function)(Item), int threads = 0);
// Add another item to the queue to be processed and signal any waiting
// threads that there's more data. Throws ThreadsExited() if already called
// results().
void queue(Item);
// Block until all items in queue have been processed and return the return
// values of the function. This will also exit all of the threads, so
// adding anything more to the queue will throw an exception.
std::vector<Result> results();
// Quit processing new items in the queue
void exit();
};
template<class Result, class Item> void Thread<Result,Item>::operator()()
{
while (true)
{
Item item;
{
std::unique_lock<std::mutex> lck(qMutex);
moreData.wait(lck, [this]{ return !q.empty() || waiting || killed; });
if ((waiting && q.empty()) || killed)
break;
item = q.front();
q.pop();
}
Result result = function(item);
{
std::lock_guard<std::mutex> lck(rMutex);
r.push_back(result);
}
}
}
template<class Result, class Item> ThreadQueue<Result,Item>::ThreadQueue(
Result (*function)(Item), int threads)
: waiting(false), killed(false)
{
if (threads <= 0)
threads = core_count();
for (int i = 0; i < threads; ++i)
pool.push_back(std::thread(Thread<Result,Item>(function,
moreData, r, rMutex, q, qMutex, waiting, killed)));
}
template<class Result, class Item> void ThreadQueue<Result,Item>::queue(Item i)
{
// Just to make sure we will actually process these eventually
if (waiting)
throw ThreadsExited();
{
std::lock_guard<std::mutex> lck(qMutex);
q.push(i);
}
moreData.notify_one();
}
template<class Result, class Item> void ThreadQueue<Result,Item>::exit()
{
killed = true;
// Cause all other non-working threads to die
moreData.notify_all();
// Wait for these to exit
for (std::thread& t : pool)
t.join();
}
template<class Result, class Item> std::vector<Result> ThreadQueue<Result,Item>::results()
{
// We want all threads to die as the queue becomes empty.
waiting = true;
// Cause all other non-working threads to die
moreData.notify_all();
// Wait for the results
for (std::thread& t : pool)
t.join();
std::lock_guard<std::mutex> lck(rMutex); // Not really needed
return r;
}
#endif