#include <threadpool.h>
Inheritance diagram for PThreadPoolBase:
Public Member Functions | |
PThreadPoolBase (unsigned maxWorkerCount=10, unsigned maxWorkUnitCount=0) | |
~PThreadPoolBase () | |
virtual WorkerThreadBase * | CreateWorkerThread ()=0 |
virtual WorkerThreadBase * | AllocateWorker () |
virtual WorkerThreadBase * | NewWorker () |
Protected Types | |
typedef std::vector< WorkerThreadBase * > | WorkerList_t |
Protected Member Functions | |
virtual bool | CheckWorker (WorkerThreadBase *worker) |
void | StopWorker (WorkerThreadBase *worker) |
Protected Attributes | |
PMutex | m_listMutex |
WorkerList_t | m_workers |
unsigned | m_maxWorkerCount |
unsigned | m_maxWorkUnitCount |
Classes | |
class | InternalWorkBase |
class | WorkerThreadBase |
To use them, declare the following:
Constructor with one parameter declared as "PThreadPoolBase & threadPool" unsigned GetWorkSize() const; void OnAddWork(work_unit *); void OnRemoveWork(work_unit *);
void Shutdown(); void Main();
Example declarations:
struct MyWorkUnit { PString work; };
class MyWorkerThread : public PThreadPoolWorkerBase { public: MyWorkerThread(PThreadPoolBase & threadPool) : PThreadPoolWorkerBase(threadPool) { }
void Main(); void Shutdown(); unsigned GetWorkSize() const; void OnAddWork(MyWorkUnit * work); void OnRemoveWork(MyWorkUnit * work); };
class SIPMainThreadPool : public PThreadPool<MyWorkUnit, MyWorkerThread> { public: virtual PThreadPoolWorkerBase * CreateWorkerThread() { return new MyWorkerThread(*this); } };
The worker thread member functions operate as follows:
Constructor Called whenever a new worker thread is required
void Main() Called when the worker thread starts up
unsigned GetWorkSize() Called whenever the thread pool wants to know how "busy" the thread is. This is used when deciding how to allocate new work units
void OnAddWork(work_unit *) Called to add a new work unit to the thread
void OnRemoveWork(work_unit *); Called to remove a work unit from the thread
void Shutdown(); Called to close down the worker thread
The thread pool is used simply by instantiation as shown below.
MyThreadPool myThreadPool(10, 30);
If the second parameter is zero, the first paramater sets the maximum number of worker threads that will be created. If the second parameter is not zero, this is the maximum number of work units each thread can handle. The first parameter is then the "quanta" in which worker threads will be allocated
Once instantiated, the AddWork and RemoveWork member functions can be used to add and remove work units as required. The thread pool code will take care of starting, stopping and load balancing worker threads as required.
typedef std::vector<WorkerThreadBase *> PThreadPoolBase::WorkerList_t [protected] |
PThreadPoolBase::PThreadPoolBase | ( | unsigned | maxWorkerCount = 10 , |
|
unsigned | maxWorkUnitCount = 0 | |||
) |
PThreadPoolBase::~PThreadPoolBase | ( | ) |
virtual WorkerThreadBase* PThreadPoolBase::AllocateWorker | ( | ) | [virtual] |
virtual bool PThreadPoolBase::CheckWorker | ( | WorkerThreadBase * | worker | ) | [protected, virtual] |
virtual WorkerThreadBase* PThreadPoolBase::CreateWorkerThread | ( | ) | [pure virtual] |
virtual WorkerThreadBase* PThreadPoolBase::NewWorker | ( | ) | [virtual] |
void PThreadPoolBase::StopWorker | ( | WorkerThreadBase * | worker | ) | [protected] |
PMutex PThreadPoolBase::m_listMutex [protected] |
unsigned PThreadPoolBase::m_maxWorkerCount [protected] |
unsigned PThreadPoolBase::m_maxWorkUnitCount [protected] |
WorkerList_t PThreadPoolBase::m_workers [protected] |