42 #ifndef _DWMLOADBALANCER_HH_ 43 #define _DWMLOADBALANCER_HH_ 79 template <
typename ItemT>
94 Worker(uint32_t waitNotEmptyMicroseconds = 1000)
95 : _workQueue(), _keepRunning(false), _thread(),
96 _waitUsecs(waitNotEmptyMicroseconds)
119 template <
typename InputIterator>
120 void AddWork(InputIterator firstIter, InputIterator lastIter)
122 _workQueue.
PushBack(firstIter, lastIter);
131 return _workQueue.
Length();
170 _keepRunning =
false;
171 if (_thread.joinable()) {
182 return (_thread.joinable());
190 while (_keepRunning) {
192 std::deque<ItemT> myCopy;
193 _workQueue.
Swap(myCopy);
197 for (
auto i : myCopy) {
251 _workers.push_back(worker);
260 auto w = std::min_element(_workers.begin(), _workers.end(),
262 if (w != _workers.end()) {
271 template <
typename InputIterator>
272 void AddWork(InputIterator firstIter, InputIterator lastIter)
274 while (! WorkerReady()) {
277 auto w = std::min_element(_workers.begin(), _workers.end(),
279 (*w)->AddWork(firstIter, lastIter);
288 for (
auto w : _workers) {
303 std::vector<Worker *> _workers;
311 for (
auto w : _workers) {
312 if (w->ReadyForWork()) {
325 #endif // _DWMLOADBALANCER_HH_ bool PushBack(const _ValueType &value)
Inserts value on the back of the queue.
Definition: DwmThreadQueue.hh:123
void Run()
Runs the worker thread.
Definition: DwmLoadBalancer.hh:188
void MaxWork(uint32_t maxItems)
Sets the maximum length of the worker's work queue.
Definition: DwmLoadBalancer.hh:148
bool IsRunning()
Returns true if the worker's thread is running.
Definition: DwmLoadBalancer.hh:180
virtual ~Worker()
Destructor. Stops the worker thread.
Definition: DwmLoadBalancer.hh:102
virtual void ProcessWork(ItemT item)=0
Pure virtual member to process a single work item.
void Stop()
Calls Worker::Stop() on all encapsulated Worker objects.
Definition: DwmLoadBalancer.hh:286
void AddWork(ItemT item)
Adds a work item for the worker.
Definition: DwmLoadBalancer.hh:110
const std::vector< Worker * > & Workers() const
Returns a const reference to the encapsulated workers.
Definition: DwmLoadBalancer.hh:297
uint32_t Swap(std::deque< _ValueType > &c)
This member is a simple optimization for fetching the contents of the queue.
Definition: DwmThreadQueue.hh:331
bool Start()
Starts the worker.
Definition: DwmLoadBalancer.hh:156
void AddWork(InputIterator firstIter, InputIterator lastIter)
Adds work to be done with load balancing.
Definition: DwmLoadBalancer.hh:272
void AddWork(ItemT item)
Adds work to be done with load balancing.
Definition: DwmLoadBalancer.hh:258
Dwm::SysLogger class definition and Syslog() macro.
void Stop()
Stops the worker.
Definition: DwmLoadBalancer.hh:168
virtual bool ProcessWork(std::deque< ItemT > &items)
Process a deque of work items.
Definition: DwmLoadBalancer.hh:217
uint32_t QueueLength() const
Returns the current length of the worker's work queue.
Definition: DwmLoadBalancer.hh:129
uint32_t MaxLength() const
Returns the max length of the queue.
Definition: DwmThreadQueue.hh:95
Comparison class for workers within the LoadBalancer.
Definition: DwmLoadBalancer.hh:232
Definition: DwmBZ2IO.hh:67
void AddWork(InputIterator firstIter, InputIterator lastIter)
Adds work items for the worker.
Definition: DwmLoadBalancer.hh:120
Worker class for LoadBalancer.
Definition: DwmLoadBalancer.hh:86
Dwm::Thread::Queue class template definition.
std::deque< _ValueType >::size_type Length() const
Returns the current length of the queue.
Definition: DwmThreadQueue.hh:113
A simple load balancer class template which balances work across Worker objects that each run in thei...
Definition: DwmLoadBalancer.hh:80
bool ReadyForWork() const
Returns true if the worker is ready for more work (has room in its work queue and is running)...
Definition: DwmLoadBalancer.hh:138
void AddWorker(Worker *worker)
Adds the given worker to the load balancer.
Definition: DwmLoadBalancer.hh:249
Worker(uint32_t waitNotEmptyMicroseconds=1000)
Constructs the worker.
Definition: DwmLoadBalancer.hh:94
bool TimedWaitForNotEmpty(const std::chrono::duration< Rep, Period > &timeToWait)
Waits timeToWait for the queue to be non-empty.
Definition: DwmThreadQueue.hh:268