-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.h
More file actions
184 lines (134 loc) · 2.72 KB
/
thread_pool.h
File metadata and controls
184 lines (134 loc) · 2.72 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
// Simple thread pool implementation.
// This implementation works effectively if the amount of threads in a pool is small, < 50.
#include <functional>
#include <thread>
#include <atomic>
#include <vector>
#include <stdexcept>
#include "common/latch.h"
class thread_pool
{
public:
thread_pool(unsigned pool_size)
: threads(pool_size)
{
}
void perform(std::function<void()>&& routine)
{
for(auto& t : threads)
{
if(t.is_not_busy())
{
t.perform(std::move(routine));
return;
}
}
throw out_of_threads();
}
class thread;
thread* get_free_thread(){
for(auto& t : threads)
if(t.is_not_busy())
return &t;
return 0;
}
unsigned busy_threads_count()
{
unsigned r = 0;
for(auto& t : threads)
{
if(!t.is_not_busy())
r++;
}
return r;
}
void wait_until_all_are_finished()
{
for(auto& t : threads)
t.wait_until_finished();
}
// exception
class out_of_threads : public std::runtime_error
{
public:
out_of_threads() : std::runtime_error("all threads of thread pool are busy") {}
};
class thread
{
public:
thread()
: //log("tpool"),
latch(0)
{
time_to_stop = false;
std::unique_lock<std::mutex> lock(mutex);
the_thread = std::thread(std::bind(&thread::thread_func, this));
condition.wait(lock); // wait until the thread is started
}
~thread()
{
time_to_stop = true;
condition.notify_one();
the_thread.join();
}
void perform(const std::function<void()>& proutine)
{
std::unique_lock<std::mutex> lock(mutex);
latch.reset(1);
routine = proutine;
condition.notify_one();
}
bool is_not_busy()
{
return latch.try_wait();
}
void wait_until_finished()
{
latch.wait();
}
private:
//log_channel log;
// disallow copying
thread(const thread&) = delete;
thread& operator=(const thread&) = delete;
void thread_func()
{
try
{
std::unique_lock<std::mutex> lock(mutex);
condition.notify_one();
while (!time_to_stop)
{
condition.wait(lock);
try_drop_a_latch drop_latch(latch); // notify that we're done on scope exit
if (time_to_stop) break;
try
{
routine();
}
catch(std::exception& e)
{
//log.error() << "error while performing routine in thread pool: " << e.what();
}
}
}
catch(std::exception& e)
{
//log.error() << "thread exception: " << e.what();
}
catch(...)
{
//log.error() << "unknown exception in the thread";
}
}
class latch latch;
std::function<void()> routine;
std::atomic<bool> time_to_stop;
std::mutex mutex;
std::condition_variable condition;
std::thread the_thread;
};
private:
std::vector<thread> threads;
};