-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorkPile.h
74 lines (54 loc) · 1.31 KB
/
WorkPile.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
#pragma once
#include <ConcurrentQueue.h>
#include <boost/thread/thread.hpp>
template<typename T>
class WorkPile
{
public:
WorkPile(int workerCount) :
init_(false),
done_(false),
workerCount_(workerCount) {}
virtual ~WorkPile() {};
bool init() {
assert(! init_);
for (int i=0; i < workerCount_; ++i) {
workers_.create_thread(boost::bind(&WorkPile::doWork, this));
}
init_ = true;
}
virtual void doWork() = 0;
bool getWork(T & item) {
return queue_.pop(item);
}
bool putWork(const T & item) {
assert(init_);
return queue_.push(item);
}
bool hasWork() {
return ! queue_.empty();
}
void finishWork() {
done_ = true;
queue_.abort();
workers_.join_all();
}
bool done() {
return done_;
}
unsigned int size() {
return queue_.size();
}
unsigned int getMaxPileSize() {
return queue_.getMaxSize();
}
void setMaxPileSize(const unsigned int & size) {
queue_.setMaxSize(size);
}
protected:
bool init_;
bool done_;
unsigned int workerCount_;
boost::thread_group workers_;
ConcurrentQueue<T> queue_;
};