PTLib
Version 2.14.3
|
These classes and templates implement a generic thread pooling mechanism. More...
#include <threadpool.h>
Classes | |
class | InternalWorkBase |
class | WorkerThreadBase |
Public Member Functions | |
~PThreadPoolBase () | |
virtual WorkerThreadBase * | CreateWorkerThread ()=0 |
virtual WorkerThreadBase * | AllocateWorker () |
virtual WorkerThreadBase * | NewWorker () |
unsigned | GetMaxWorkers () const |
void | SetMaxWorkers (unsigned count) |
unsigned | GetMaxUnits () const |
void | SetMaxUnits (unsigned count) |
![]() | |
unsigned | GetTraceContextIdentifier () const |
Get PTRACE context identifier. More... | |
void | SetTraceContextIdentifier (unsigned id) |
void | GetTraceContextIdentifier (PObject &obj) |
void | GetTraceContextIdentifier (PObject *obj) |
void | SetTraceContextIdentifier (const PObject &obj) |
void | SetTraceContextIdentifier (const PObject *obj) |
virtual | ~PObject () |
virtual PObject * | Clone () const |
Create a copy of the class on the heap. More... | |
template<class CLS > | |
CLS * | CloneAs () const |
As for Clone() but converts to specified type. More... | |
virtual PINDEX | HashFunction () const |
This function yields a hash value required by the PDictionary class. More... | |
virtual const char * | GetClass (unsigned ancestor=0) const |
Get the current dynamic type of the object instance. More... | |
PBoolean | IsClass (const char *cls) const |
virtual PBoolean | InternalIsDescendant (const char *clsName) const |
Determine if the dynamic type of the current instance is a descendent of the specified class. More... | |
__inline const PObject * | PTraceObjectInstance () const |
virtual Comparison | Compare (const PObject &obj) const |
Compare the two objects and return their relative rank. More... | |
virtual Comparison | CompareObjectMemoryDirect (const PObject &obj) const |
Determine the byte wise comparison of two objects. More... | |
bool | operator== (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator!= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator< (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator> (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator<= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator>= (const PObject &obj) const |
Compare the two objects. More... | |
virtual void | PrintOn (ostream &strm) const |
Output the contents of the object to the stream. More... | |
virtual void | ReadFrom (istream &strm) |
Input the contents of the object from the stream. More... | |
Protected Types | |
typedef std::vector < WorkerThreadBase * > | WorkerList_t |
Protected Member Functions | |
PThreadPoolBase (unsigned maxWorkerCount, unsigned maxWorkUnitCount, const char *threadName, PThread::Priority priority) | |
virtual bool | CheckWorker (WorkerThreadBase *worker) |
void | StopWorker (WorkerThreadBase *worker) |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Protected Attributes | |
PMutex | m_listMutex |
WorkerList_t | m_workers |
unsigned | m_maxWorkerCount |
unsigned | m_maxWorkUnitCount |
unsigned | m_highWaterMark |
PString | m_threadName |
PThread::Priority | m_priority |
![]() | |
unsigned | m_traceContextIdentifier |
Additional Inherited Members | |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
![]() | |
static const char * | Class () |
Get the name of the class as a C string. More... | |
static __inline const PObject * | PTraceObjectInstance (const char *) |
static __inline const PObject * | PTraceObjectInstance (const PObject *obj) |
static Comparison | InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size) |
Internal function caled from CompareObjectMemoryDirect() More... | |
![]() |
These classes and templates implement a generic thread pooling mechanism.
There are two forms, low level and high level. For high level, it is assumed that there is a pool of threads each with a queue of work items to be processed. TO use simply decare a class containing the void Work() function and create the poolwith PQueuedThreadPool. e.g.
class MyWork { void Work() { doIt(); } }
PQueuedThreadPool<MyWork> m_pool;
m_pool.AddWork(new MyWork());
To use low level, 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.Base class for thread pools.
|
protected |
PThreadPoolBase::~PThreadPoolBase | ( | ) |
|
protected |
|
virtual |
Referenced by PThreadPool< Timeout >::AddWork().
|
protectedvirtual |
|
pure virtual |
Implemented in PQueuedThreadPool< Work_T >, and PQueuedThreadPool< Timeout >.
|
inline |
References m_maxWorkUnitCount.
|
inline |
References m_maxWorkerCount.
|
virtual |
|
inline |
References m_maxWorkUnitCount.
|
inline |
References m_maxWorkerCount.
|
protected |
|
protected |
|
protected |
Referenced by PThreadPool< Timeout >::AddWork().
|
protected |
Referenced by GetMaxWorkers(), and SetMaxWorkers().
|
protected |
Referenced by GetMaxUnits(), and SetMaxUnits().
|
protected |
Referenced by PQueuedThreadPool< Timeout >::CreateWorkerThread().
|
protected |
Referenced by PQueuedThreadPool< Timeout >::CreateWorkerThread().
|
protected |