-
Notifications
You must be signed in to change notification settings - Fork 6
/
threadqueuevoid.h
166 lines (131 loc) · 4.09 KB
/
threadqueuevoid.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
/*
* Function returns void version.
*
* Create a certain number of threads in the background and process data as it
* is added to a queue.
*
* Example:
*
* void function(Input) { }
* ThreadQueueVoid<Input> ts(function);
* ts.queue(Input);
* // other processing
* ts.wait();
*/
#ifndef H_THREADQUEUEVOID
#define H_THREADQUEUEVOID
#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 ThreadsExitedVoid { };
// 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 Item> class ThreadVoid
{
void (*function)(Item);
std::condition_variable& moreData;
std::queue<Item>& q;
std::mutex& qMutex;
std::atomic_bool& waiting;
std::atomic_bool& killed;
public:
ThreadVoid(void (*function)(Item), std::condition_variable& moreData,
std::queue<Item>& q, std::mutex& qMutex, std::atomic_bool& waiting,
std::atomic_bool& killed)
: function(function), moreData(moreData), q(q), qMutex(qMutex),
waiting(waiting), killed(killed) { }
void operator()();
};
template<class Item> class ThreadQueueVoid
{
// 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;
// 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.
ThreadQueueVoid(void (*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. This will also exit
// all of the threads, so adding anything more to the queue will throw an
// exception.
void wait();
// Quit processing new items in the queue
void exit();
};
template<class Item> void ThreadVoid<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();
}
function(item);
}
}
template<class Item> ThreadQueueVoid<Item>::ThreadQueueVoid(
void (*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(ThreadVoid<Item>(function,
moreData, q, qMutex, waiting, killed)));
}
template<class Item> void ThreadQueueVoid<Item>::queue(Item i)
{
// Just to make sure we will actually process these eventually
if (waiting)
throw ThreadsExitedVoid();
{
std::lock_guard<std::mutex> lck(qMutex);
q.push(i);
}
moreData.notify_one();
}
template<class Item> void ThreadQueueVoid<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 Item> void ThreadQueueVoid<Item>::wait()
{
// 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();
}
#endif