GClasses
GClasses::GWorkerThread Class Referenceabstract

Detailed Description

An abstract class for performing jobs. The idea is that you should be able to write the code to perform the jobs, then use it in either a serial or parallel manner. The class you write, that inherits from this one, will typically have additional constructor parameters that pass in any values or data necessary to define the jobs.

#include <GThread.h>

Public Member Functions

 GWorkerThread (GMasterThread &master)
 
virtual ~GWorkerThread ()
 
virtual void doJob (size_t jobId)=0
 This method should be implemented to perform the job indicated by the specified id. (The job ids range from 0 to jobCount-1.) The implementing class should be designed such that jobId is sufficient for it to obtain any information that it needs to do the job. Also, this method is also reponsible to report the results in a thread-safe manner. Here is an example of how to take a lock in order to do something critical: GSpinLockHolder lockHolder(m_master.getLock(), "MyWorkerThread::doJob"); (Note that this assumes the master will never be deleted while a worker is doing a job.) More...
 
void pump ()
 This method is called by the master thread. Users should not need to call it. It pulls jobs from the master thread (by calling nextJob()) and does them as long as m_keepAlive is true. If the master returns INVALID_INDEX for the job id, it goes to sleep for a short time. (Each time it receives INVALID_INDEX, it becomes more bored, and so sleeps a little longer, up to a maximum nap of 100ms. When it receives a job, the boredom counter resets to 0.) When m_keepAlive is set to false, this method will delete this object and exit. More...
 

Public Attributes

size_t m_boredom
 
volatile bool m_keepAlive
 
GMasterThreadm_master
 

Constructor & Destructor Documentation

GClasses::GWorkerThread::GWorkerThread ( GMasterThread master)
inline
virtual GClasses::GWorkerThread::~GWorkerThread ( )
inlinevirtual

Member Function Documentation

virtual void GClasses::GWorkerThread::doJob ( size_t  jobId)
pure virtual

This method should be implemented to perform the job indicated by the specified id. (The job ids range from 0 to jobCount-1.) The implementing class should be designed such that jobId is sufficient for it to obtain any information that it needs to do the job. Also, this method is also reponsible to report the results in a thread-safe manner. Here is an example of how to take a lock in order to do something critical: GSpinLockHolder lockHolder(m_master.getLock(), "MyWorkerThread::doJob"); (Note that this assumes the master will never be deleted while a worker is doing a job.)

void GClasses::GWorkerThread::pump ( )

This method is called by the master thread. Users should not need to call it. It pulls jobs from the master thread (by calling nextJob()) and does them as long as m_keepAlive is true. If the master returns INVALID_INDEX for the job id, it goes to sleep for a short time. (Each time it receives INVALID_INDEX, it becomes more bored, and so sleeps a little longer, up to a maximum nap of 100ms. When it receives a job, the boredom counter resets to 0.) When m_keepAlive is set to false, this method will delete this object and exit.

Member Data Documentation

size_t GClasses::GWorkerThread::m_boredom
volatile bool GClasses::GWorkerThread::m_keepAlive
GMasterThread& GClasses::GWorkerThread::m_master