forked from wcatykid/Graph-Based-Molecular-Synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread_Pool.h
279 lines (227 loc) · 7.3 KB
/
Thread_Pool.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* This file is part of esynth.
*
* esynth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* esynth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with esynth. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _THREAD_POOL_GUARD
#define _THREAD_POOL_GUARD 1
#include <iostream>
#include <queue>
#include <unistd.h>
#include <pthread.h>
template <class In_Type, class Out_Type> class Thread_Pool;
template <class In_Type, class Out_Type> void *process_data_func(void * This);
template <class In_Type, class Out_Type> void *worker_func(void * This);
template <class In_Type, class Out_Type>
class Thread_Pool
{
public:
Thread_Pool(Out_Type (*p)(In_Type)); // function pointer only
Thread_Pool(int num_threads, Out_Type (*p)(In_Type)); // num threads and function pointer
~Thread_Pool(); // destructor
void print(); // display some debuggin info
void push(In_Type data); // push
int in_q_size(); // size of un-processed item q
int out_q_size(); // size of processed item q
Out_Type front(); // front
void pop(); // pop
private:
pthread_t _manager; // for manager thread
pthread_t *threads; // for worker threads
int num_threads; // total threads
std::queue<In_Type> in_q; // items to be processed
std::queue<Out_Type> out_q; // finished results
pthread_mutex_t lock_in_q, lock_out_q; // mutex locks for q's
Out_Type (*process)(In_Type); // misc processing function entered by user
bool processing_data; // flag to stop processing data - threads will all die, currently no way to start again excpet making a new thread pool
void process_data(); // asynchronously process all incoming data
void stop_processing(); // finish with in-progress data then stop processing
//template <class In_Type, class Out_Type> friend void *process_data_func(void * This);
friend void *process_data_func<In_Type, Out_Type>(void * This); // manager thread
friend void *worker_func<In_Type, Out_Type>(void * This); // worker thread
};
template <class In_Type, class Out_Type>
Thread_Pool<In_Type, Out_Type>::Thread_Pool(Out_Type (*p)(In_Type))
{
num_threads = 10;
threads=new pthread_t[num_threads];
process=p;
pthread_mutex_init(&lock_in_q, NULL);
pthread_mutex_init(&lock_out_q, NULL);
process_data();
}
template <class In_Type, class Out_Type>
Thread_Pool<In_Type, Out_Type>::Thread_Pool(int num_threads, Out_Type (*p)(In_Type))
{
this->num_threads = num_threads;
threads = new pthread_t[num_threads];
process = p;
pthread_mutex_init(&lock_in_q, NULL);
pthread_mutex_init(&lock_out_q, NULL);
process_data();
}
template <class In_Type, class Out_Type>
Thread_Pool<In_Type, Out_Type>::~Thread_Pool()
{
stop_processing();
}
//
// display some debugging info
//
template <class In_Type, class Out_Type>
void Thread_Pool<In_Type, Out_Type>::print()
{
std::cout << "num_threads = " << num_threads << std::endl;
std::cout << "in_q length = " << in_q_size() << std::endl;
std::cout << "out_q length = " << out_q_size() << std::endl;
}
template <class In_Type, class Out_Type>
void Thread_Pool<In_Type, Out_Type>::push(In_Type data)
{
pthread_mutex_lock(&lock_in_q);
in_q.push(data);
pthread_mutex_unlock(&lock_in_q);
}
//
// size of unprocessed item queue
//
template <class In_Type, class Out_Type>
int Thread_Pool<In_Type, Out_Type>::in_q_size()
{
int size;
pthread_mutex_lock(&lock_in_q);
size = in_q.size();
pthread_mutex_unlock(&lock_in_q);
return size;
}
//
// size of processed item queue
//
template <class In_Type, class Out_Type>
int Thread_Pool<In_Type, Out_Type>::out_q_size()
{
int size;
pthread_mutex_lock(&lock_out_q);
size = out_q.size();
pthread_mutex_unlock(&lock_out_q);
return size;
}
template <class In_Type, class Out_Type>
Out_Type Thread_Pool<In_Type, Out_Type>::front()
{
Out_Type result;
pthread_mutex_lock(&lock_out_q);
result = out_q.front();
pthread_mutex_unlock(&lock_out_q);
return result;
}
template <class In_Type, class Out_Type>
void Thread_Pool<In_Type, Out_Type>::pop()
{
pthread_mutex_lock(&lock_out_q);
out_q.pop();
pthread_mutex_unlock(&lock_out_q);
}
//
// asynchronously process all incoming data
//
template <class In_Type, class Out_Type>
void Thread_Pool<In_Type, Out_Type>::process_data()
{
processing_data = true;
if (~pthread_create(&_manager, NULL, process_data_func<In_Type, Out_Type>, this))
{
std::cout << "Thread pool manager created." << std::endl;
}
else
{
std::cout << "Thread pool manager creation failed." << std::endl;
processing_data = false;
}
}
//
// Finish with in-progress data then stop processing
//
template <class In_Type, class Out_Type>
void Thread_Pool<In_Type, Out_Type>::stop_processing()
{
// std::cerr << "Stop Processing signal given." << std::endl;
processing_data = false;
//std::cerr << "Before Manager join." << std::endl;
(void) pthread_join(_manager, NULL);
//std::cerr << "After manager join." << std::endl;
}
template <class In_Type, class Out_Type>
void *process_data_func(void *This_void)
{
// manager thread
// init
Thread_Pool<In_Type, Out_Type>* This = (Thread_Pool<In_Type, Out_Type> *)This_void;
// spawn workers
for (int x = 0; x < This->num_threads; x++)
{
if(~pthread_create(&This->threads[x], NULL, worker_func<In_Type, Out_Type>, This_void))
std::cout << "worker " << x << " created" << std::endl;
else
std::cout << "worker " << x << " creation failed" << std::endl;
}
// manage workers
while(This->processing_data)
{
sleep(1); // sleep 1 second
} // not much to manage atm, sleep() maybe?
// de-spawn workers
for (int x = 0; x < This->num_threads; x++)
{
(void) pthread_join(This->threads[x], NULL);
std::cerr << "worker " << x << " removed" << std::endl;
}
// exit
pthread_exit(NULL);
}
template <class In_Type, class Out_Type>
void *worker_func(void *This_void)
{ // worker thread
// init
Thread_Pool<In_Type, Out_Type> * This=(Thread_Pool<In_Type, Out_Type> *)This_void;
In_Type in;
Out_Type out;
while(This->processing_data)
{
// pop
pthread_mutex_lock(&This->lock_in_q);
if (This->in_q.size()>0)
{
in=This->in_q.front();
This->in_q.pop();
pthread_mutex_unlock(&This->lock_in_q);
}
else
{
pthread_mutex_unlock(&This->lock_in_q);
continue;
}
// process
out=This->process(in);
// push
pthread_mutex_lock(&This->lock_out_q);
This->out_q.push(out);
pthread_mutex_unlock(&This->lock_out_q);
}
//exit
pthread_exit(NULL);
}
// list all possile templates or move implementation into .h file...
// template class Thread_Pool<int, int>;
#endif