GClasses
|
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... | |
GSpinLock * | getLock () |
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 |
GSpinLock * | m_pMasterLock |
std::vector< GWorkerThread * > | m_workers |
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.
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.
|
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.)
|
staticprotected |
This is a helper function that spawns a worker thread.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |