Main Page   Modules   Namespace List   Class Hierarchy   Data Structures   File List   Namespace Members   Data Fields   Globals   Related Pages  

SctService::WorkerGroup< T > Class Template Reference

Control part of the observer/worker pattern. More...

#include <WorkerGroup.h>

Inheritance diagram for SctService::WorkerGroup< T >:

noncopyable SctAnalysis::AnalysisWorkerGroup SctArchivingService::ArchivingWorkerGroup SctFitter::FitterWorkerGroup

Queue @{ the queue itself contains pointers to objects to be

worked on.

std::queue< DataTypem_queue
boost::thread_group m_thread_group
 boost thread group from which threads can be created

volatile bool m_run
 if false, threads are to stop.

volatile bool m_pause
 does not execute work() if true

volatile unsigned m_sleeptime
 time to sleep if queue is empty

boost::mutex m_queueAccess
 queueLock

boost::mutex m_vectorAccess
 vectorLock

class Worker

Public Types

typedef T DataType

Public Member Functions

void go (unsigned nWorker=1)
 Start this worker group.
Parameters:
the number of worker threads to be started.


unsigned queueSize ()
 get the number of objects in the queue

unsigned nWorkers ()
 get the number of workers in the map

unsigned busy ()
 get the number of threads which are busy.

void stop ()
 Raise flag to stop all worker threads.

void setPaused (bool paused=true)
void join ()
 Call this to make sure that Worker threads continue to completion.

unsigned paused ()
 get the number of paused threads

void setSleepTime (unsigned msec)
 Set sleep time for all workers, if no items are on the queue.

unsigned getSleepTime ()
 Get the sleep time.

DataType pop ()
void push (DataType ob)
 Push an object onto the back of the queue.

virtual ~WorkerGroup ()
 destructor

 WorkerGroup ()
 Constructor.


Protected Types

typedef std::vector< Worker * > WorkerStore
typedef WorkerStore::iterator Iterator

Protected Member Functions

virtual void threadMain (Worker &worker) throw ()
 The threadMain method.

virtual bool popAndWork (Worker &worker)
 If the queue length != 0, removes an element from the queue and class work.

virtual void waitForData ()
 Called if there is no data on the queue.

virtual void work (DataType data)=0 throw ()
 To be overriden! Should actually do something with the data!

void wait (int msec)
 Causes this thread to sleep for msec milliseconds.

void addWorker (Worker &worker)
 threadMain should call this with its parameter.


Protected Attributes

Worker thread information @{
This map allows each thread to access different particular information from its own Worker.

WorkerStore m_workers

Detailed Description

template<class T>
class SctService::WorkerGroup< T >

Control part of the observer/worker pattern.

Contains the queue to which objects of type T are pushed. Also useful functions to control and query the worker threads. The template class ``T'' is the data/instructions which the workers at to work on. Derive this class to implement/modify the behaviour of the worker threads. At the least, the work method needs an implementation. T must be DefaultConstructible For a simple example, see the test/MyWorkerGroup.h file

Definition at line 23 of file WorkerGroup.h.


Member Typedef Documentation

template<class T>
typedef T SctService::WorkerGroup< T >::DataType
 

Definition at line 25 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::pop(), and SctService::WorkerGroup< T >::popAndWork().

template<class T>
typedef WorkerStore::iterator SctService::WorkerGroup< T >::Iterator [protected]
 

Definition at line 128 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::busy(), and SctService::WorkerGroup< T >::paused().

template<class T>
typedef std::vector<Worker*> SctService::WorkerGroup< T >::WorkerStore [protected]
 

Definition at line 127 of file WorkerGroup.h.


Constructor & Destructor Documentation

template<class T>
virtual SctService::WorkerGroup< T >::~WorkerGroup   [inline, virtual]
 

destructor

Definition at line 81 of file WorkerGroup.h.

template<class T>
SctService::WorkerGroup< T >::WorkerGroup   [inline]
 

Constructor.

Definition at line 84 of file WorkerGroup.h.


Member Function Documentation

template<class T>
void SctService::WorkerGroup< T >::addWorker Worker   worker [protected]
 

threadMain should call this with its parameter.

Definition at line 186 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_workers.

template<class W>
unsigned SctService::WorkerGroup< W >::busy  
 

get the number of threads which are busy.

Note:
for information only, not to be used in thread-control.

Definition at line 263 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::Iterator, and SctService::WorkerGroup< T >::m_workers.

template<class T>
unsigned SctService::WorkerGroup< T >::getSleepTime   [inline]
 

Get the sleep time.

Definition at line 69 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::waitForData().

template<class T>
void SctService::WorkerGroup< T >::go unsigned    nWorker = 1
 

Start this worker group.

Parameters:
the number of worker threads to be started.

Definition at line 158 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_thread_group, and SctService::WorkerGroup< T >::Worker.

Referenced by SctArchivingService::ArchivingService::run(), and SctAnalysis::AnalysisService::run().

template<class W>
void SctService::WorkerGroup< W >::join  
 

Call this to make sure that Worker threads continue to completion.

Definition at line 244 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_thread_group.

Referenced by SctService::WorkerGroup< T >::stop().

template<class W>
unsigned SctService::WorkerGroup< W >::nWorkers  
 

get the number of workers in the map

Definition at line 233 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_workers.

template<class W>
unsigned SctService::WorkerGroup< W >::paused  
 

get the number of paused threads

Definition at line 254 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::Iterator, and SctService::WorkerGroup< T >::m_workers.

template<class T>
T SctService::WorkerGroup< T >::pop  
 

Definition at line 199 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::DataType, and SctService::WorkerGroup< T >::m_queue.

template<class W>
bool SctService::WorkerGroup< W >::popAndWork Worker   worker [protected, virtual]
 

If the queue length != 0, removes an element from the queue and class work.

Returns:
false if the queue length is 0.

Definition at line 212 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::DataType, SctService::WorkerGroup< T >::Worker::m_busy, SctService::WorkerGroup< T >::m_queue, and SctService::WorkerGroup< T >::work().

template<class T>
void SctService::WorkerGroup< T >::push DataType    ob
 

Push an object onto the back of the queue.

Definition at line 192 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_queue.

Referenced by SctArchivingService::ArchivingService::archive(), SctFitter::Fitter::doFits(), and SctAnalysis::AnalysisService::scanResultCallback().

template<class W>
unsigned SctService::WorkerGroup< W >::queueSize  
 

get the number of objects in the queue

Note:
for information only, not to be used in thread-control.

Definition at line 228 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_queue.

template<class W>
void SctService::WorkerGroup< W >::setPaused bool    paused = true
 

Definition at line 249 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_pause.

template<class W>
void SctService::WorkerGroup< W >::setSleepTime unsigned    msec
 

Set sleep time for all workers, if no items are on the queue.

Parameters:
sleep time in msec

Definition at line 272 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_sleeptime.

template<class W>
void SctService::WorkerGroup< W >::stop  
 

Raise flag to stop all worker threads.

Definition at line 238 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::join(), and SctService::WorkerGroup< T >::m_run.

template<class T>
void SctService::WorkerGroup< T >::threadMain Worker   worker throw () [protected, virtual]
 

The threadMain method.

Can be overriden if sub-classes want to completly change the behaviour! Current implementation checks various flags for pausing, stopping etc, then calls popAndWork. If popAndWork returns false, then it calls waitForData

Definition at line 166 of file WorkerGroup.h.

template<class W>
void SctService::WorkerGroup< W >::wait int    msec [protected]
 

Causes this thread to sleep for msec milliseconds.

Definition at line 277 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::waitForData().

template<class W>
void SctService::WorkerGroup< W >::waitForData   [protected, virtual]
 

Called if there is no data on the queue.

Calls wait in default implementation.

Definition at line 285 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::getSleepTime(), and SctService::WorkerGroup< T >::wait().

template<class T>
virtual void SctService::WorkerGroup< T >::work DataType    data throw () [protected, pure virtual]
 

To be overriden! Should actually do something with the data!

Referenced by SctService::WorkerGroup< T >::popAndWork().


Friends And Related Function Documentation

template<class T>
friend class Worker [friend]
 

Definition at line 153 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::go().


Field Documentation

template<class T>
volatile bool SctService::WorkerGroup< T >::m_pause [protected]
 

does not execute work() if true

Definition at line 148 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::setPaused(), and SctService::WorkerGroup< DataType >::WorkerGroup().

template<class T>
std::queue<DataType> SctService::WorkerGroup< T >::m_queue [protected]
 

Definition at line 141 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::pop(), SctService::WorkerGroup< T >::popAndWork(), SctService::WorkerGroup< T >::push(), and SctService::WorkerGroup< T >::queueSize().

template<class T>
boost::mutex SctService::WorkerGroup< T >::m_queueAccess [protected]
 

queueLock

Definition at line 151 of file WorkerGroup.h.

template<class T>
volatile bool SctService::WorkerGroup< T >::m_run [protected]
 

if false, threads are to stop.

Definition at line 147 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::stop(), and SctService::WorkerGroup< DataType >::WorkerGroup().

template<class T>
volatile unsigned SctService::WorkerGroup< T >::m_sleeptime [protected]
 

time to sleep if queue is empty

Definition at line 149 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< DataType >::getSleepTime(), SctService::WorkerGroup< T >::setSleepTime(), and SctService::WorkerGroup< DataType >::WorkerGroup().

template<class T>
boost::thread_group SctService::WorkerGroup< T >::m_thread_group [protected]
 

boost thread group from which threads can be created

Definition at line 145 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::go(), and SctService::WorkerGroup< T >::join().

template<class T>
boost::mutex SctService::WorkerGroup< T >::m_vectorAccess [protected]
 

vectorLock

Definition at line 152 of file WorkerGroup.h.

template<class T>
WorkerStore SctService::WorkerGroup< T >::m_workers [protected]
 

Definition at line 135 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::addWorker(), SctService::WorkerGroup< T >::busy(), SctService::WorkerGroup< T >::nWorkers(), and SctService::WorkerGroup< T >::paused().


The documentation for this class was generated from the following file:
Generated on Mon Dec 15 19:38:10 2003 for SCT DAQ/DCS Software by doxygen1.3-rc3