A simple load balancer class template which balances work across Worker objects that each run in their own thread. More...
#include <DwmLoadBalancer.hh>
Classes | |
| class | Worker |
| Worker class for LoadBalancer. More... | |
Public Member Functions | |
| void | AddWorker (Worker *worker) |
| Adds the given worker to the load balancer. | |
| bool | AddWork (ItemT item) |
| Adds work to be done with load balancing. | |
| template<typename InputIterator > | |
| bool | AddWork (InputIterator firstIter, InputIterator lastIter) |
| Adds work to be done with load balancing. | |
| void | Stop () |
| Calls Worker::Stop() on all encapsulated Worker objects. | |
| const std::vector< std::unique_ptr< Worker > > & | Workers () const |
| Returns a const reference to the encapsulated workers. | |
A simple load balancer class template which balances work across Worker objects that each run in their own thread.
A user would derive a worker class from LoadBalancer<ItemT>::Worker, and implement the Worker::ProcessWork(ItemT) member. The worker can be added to a LoadBalancer via the LoadBalancer::AddWorker() member. Work can be added to the LoadBalancer with AddWork(ItemT). A unit of work is encapsulated in the ItemT template parameter. This is a template so that any copyable type can be used as a unit of work.
Today, this class template may not be all that useful (we have the concurrency features of C++11 and C++14). I originally wrote this class using pthreads, before we had C++11 and C++14. It's still in use in some of my code, but I may revisit this soon and modernize it. For now the only modernization is the migration to std::thread.
|
inline |
Adds work to be done with load balancing.
Returns true on success, false on failure.
|
inline |
Adds the given worker to the load balancer.
Note that on return, the caller no longer owns worker. It is assigned to a unique_ptr that is owned by the LoadBalancer.
|
inline |
Returns a const reference to the encapsulated workers.
NOT thread safe! This is mostly just for unit testing.