GClasses
GClasses::GMasterThread Class Reference

Detailed Description

Manages a pool of GWorkerThread objects. To use this class, first call addWorker one or more times. Then, call doJobs.

#include <GThread.h>

Public Member Functions

 GMasterThread ()
 
 ~GMasterThread ()
 Note that all worker threads reference the master, so it is not okay to delete the master while a worker is still doing a job. More...
 
void addWorker (GWorkerThread *pWorker)
 Adds a worker to the pool. Takes ownership of the worker object. More...
 
void doJobs (size_t jobCount)
 Perform some jobs. The job ids will range from 0 to jobCount-1. If no workers have been added, throws an exception. If only one worker has been added, that worker performs all of the jobs in the same thread as the master (so no new threads are spawned). If two or more workers have been added to the pool, the jobs will be performed by those workers in separate threads. This method does not return until all the jobs are done. More...
 
GSpinLockgetLock ()
 Returns a pointer to the master lock. (If there is only one worker, then there are no worker threads, and this method will return NULL. Note that GSpinLockHolder checks for NULL, so it provides a good way to take the lock.) More...
 
size_t nextJob (GWorkerThread *pWorkerWhoWantsAJob)
 This method is called by worker threads to obtain the next available job. Calling it is a contract to complete the job. Returns INVALID_INDEX if there are no more jobs to do. In that case, the worker thread should terminate. (This method is already synchronized, so you do not need to take a lock before calling it.) More...
 

Static Protected Member Functions

static unsigned int spawnWorker (void *pWorkerObject)
 This is a helper function that spawns a worker thread. More...
 

Protected Attributes

volatile size_t m_activeWorkers
 
size_t m_job
 
size_t m_jobCount
 
GSpinLockm_pMasterLock
 
std::vector< GWorkerThread * > m_workers
 

Constructor & Destructor Documentation

GClasses::GMasterThread::GMasterThread ( )
GClasses::GMasterThread::~GMasterThread ( )

Note that all worker threads reference the master, so it is not okay to delete the master while a worker is still doing a job.

Member Function Documentation

void GClasses::GMasterThread::addWorker ( GWorkerThread pWorker)

Adds a worker to the pool. Takes ownership of the worker object.

void GClasses::GMasterThread::doJobs ( size_t  jobCount)

Perform some jobs. The job ids will range from 0 to jobCount-1. If no workers have been added, throws an exception. If only one worker has been added, that worker performs all of the jobs in the same thread as the master (so no new threads are spawned). If two or more workers have been added to the pool, the jobs will be performed by those workers in separate threads. This method does not return until all the jobs are done.

GSpinLock* GClasses::GMasterThread::getLock ( )
inline

Returns a pointer to the master lock. (If there is only one worker, then there are no worker threads, and this method will return NULL. Note that GSpinLockHolder checks for NULL, so it provides a good way to take the lock.)

size_t GClasses::GMasterThread::nextJob ( GWorkerThread pWorkerWhoWantsAJob)

This method is called by worker threads to obtain the next available job. Calling it is a contract to complete the job. Returns INVALID_INDEX if there are no more jobs to do. In that case, the worker thread should terminate. (This method is already synchronized, so you do not need to take a lock before calling it.)

static unsigned int GClasses::GMasterThread::spawnWorker ( void *  pWorkerObject)
staticprotected

This is a helper function that spawns a worker thread.

Member Data Documentation

volatile size_t GClasses::GMasterThread::m_activeWorkers
protected
size_t GClasses::GMasterThread::m_job
protected
size_t GClasses::GMasterThread::m_jobCount
protected
GSpinLock* GClasses::GMasterThread::m_pMasterLock
protected
std::vector<GWorkerThread*> GClasses::GMasterThread::m_workers
protected