PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PThreadPoolBase Class Referenceabstract

These classes and templates implement a generic thread pooling mechanism. More...

#include <threadpool.h>

Inheritance diagram for PThreadPoolBase:
Collaboration diagram for PThreadPoolBase:

Classes

class  InternalWorkBase
 
class  WorkerThreadBase
 

Public Member Functions

 ~PThreadPoolBase ()
 
void Shutdown ()
 
virtual void ReclaimWorkers ()
 
unsigned GetMaxWorkers () const
 
void SetMaxWorkers (unsigned count)
 
unsigned GetMaxUnits () const
 
void SetMaxUnits (unsigned count)
 
- Public Member Functions inherited from PObject
__inline unsigned GetTraceContextIdentifier () const
 Get PTRACE context identifier. More...
 
__inline void SetTraceContextIdentifier (unsigned id)
 
__inline void SetTraceContextIdentifier (const PObject &obj)
 
__inline void SetTraceContextIdentifier (const PObject *obj)
 
__inline void CopyTraceContextIdentifier (PObject &obj) const
 
__inline void CopyTraceContextIdentifier (PObject *obj) const
 
virtual ~PObject ()
 
__inline const char * GetClass () const
 
__inline bool IsClass (const char *name) const
 
__inline const PObjectPTraceObjectInstance () const
 
virtual PObjectClone () 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 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 WorkerThreadBaseAllocateWorker ()
 
virtual WorkerThreadBaseCreateWorkerThread ()=0
 
virtual void StopWorker (WorkerThreadBase *worker)
 
virtual bool OnWorkerStarted (WorkerThreadBase &thread)
 
 PDECLARE_MUTEX (m_mutex)
 
- Protected Member Functions inherited from PObject
 PObject ()
 Constructor for PObject, made protected so cannot ever create one on its own. More...
 

Protected Attributes

WorkerList_t m_workers
 
unsigned m_maxWorkerCount
 
unsigned m_maxWorkUnitCount
 
PString m_threadName
 
PThread::Priority m_priority
 
- Protected Attributes inherited from PObject
unsigned m_traceContextIdentifier
 

Additional Inherited Members

- Public Types inherited from PObject
enum  Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 }
 Result of the comparison operation performed by the Compare() function. More...
 
- Static Public Member Functions inherited from PObject
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject *from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject *from)
 
static __inline const char * Class ()
 
static __inline const PObjectPTraceObjectInstance (const char *)
 
static __inline const PObjectPTraceObjectInstance (const PObject *obj)
 
template<typename T >
static Comparison Compare2 (T v1, T v2)
 Compare two types, returning Comparison type. More...
 
static Comparison InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size)
 Internal function caled from CompareObjectMemoryDirect() More...
 

Detailed Description

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:

  • A class that describes a "unit" of work to be performed.
  • A class that described a worker thread within the pool. This class must be a descendant of PThreadPoolWorkerBase and must define the following member functions:
    Constructor with one parameter declared as "PThreadPoolBase & threadPool"
    unsigned GetWorkSize() const;
    void AddWork(Work_T *);
    void RemoveWork(Work_T *);
    
    void Shutdown();
    bool Work();
    
  • A class that describes the thread pool itself. This is defined using PThreadPool template

Example declarations:

struct MyWorkUnit { PString work; };

class MyWorkerThread : public PThreadPoolWorkerBase { public: MyWorkerThread(PThreadPoolBase & threadPool) : PThreadPoolWorkerBase(threadPool) { }

bool Work(); void Shutdown(); unsigned GetWorkSize() const; void AddWork(Work_T * work); void RemoveWork(Work_T * 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

bool Work() Called continuously by worker thread, until it returns false

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 AddWork(Work_T *) Called to add a new work unit to the thread

void RemoveWork(Work_T *); 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.

Member Typedef Documentation

typedef std::vector<WorkerThreadBase *> PThreadPoolBase::WorkerList_t
protected

Constructor & Destructor Documentation

PThreadPoolBase::~PThreadPoolBase ( )
inline

References Shutdown().

PThreadPoolBase::PThreadPoolBase ( unsigned  maxWorkerCount,
unsigned  maxWorkUnitCount,
const char *  threadName,
PThread::Priority  priority 
)
protected

Member Function Documentation

virtual WorkerThreadBase* PThreadPoolBase::AllocateWorker ( )
protectedvirtual
virtual WorkerThreadBase* PThreadPoolBase::CreateWorkerThread ( )
protectedpure virtual
unsigned PThreadPoolBase::GetMaxUnits ( ) const
inline

References m_maxWorkUnitCount.

unsigned PThreadPoolBase::GetMaxWorkers ( ) const
inline

References m_maxWorkerCount.

virtual bool PThreadPoolBase::OnWorkerStarted ( WorkerThreadBase thread)
protectedvirtual
PThreadPoolBase::PDECLARE_MUTEX ( m_mutex  )
protected
virtual void PThreadPoolBase::ReclaimWorkers ( )
virtual
void PThreadPoolBase::SetMaxUnits ( unsigned  count)
inline

References m_maxWorkUnitCount.

void PThreadPoolBase::SetMaxWorkers ( unsigned  count)
void PThreadPoolBase::Shutdown ( )

Referenced by ~PThreadPoolBase().

virtual void PThreadPoolBase::StopWorker ( WorkerThreadBase worker)
protectedvirtual

Member Data Documentation

unsigned PThreadPoolBase::m_maxWorkerCount
protected
unsigned PThreadPoolBase::m_maxWorkUnitCount
protected
PThread::Priority PThreadPoolBase::m_priority
protected
WorkerList_t PThreadPoolBase::m_workers
protected

The documentation for this class was generated from the following file: