SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FXWorkerThread::Pool Class Reference

A pool of worker threads which distributes the tasks and collects the results. More...

#include <FXWorkerThread.h>

Public Member Functions

void add (Task *const t)
 Gives a number to the given task and assigns it to a randomly chosen worker. More...
 
void addFinished (Task *const t)
 Adds the given task to the list of finished tasks and assigns it to a randomly chosen worker. More...
 
void addWorker (FXWorkerThread *const w)
 Adds the given thread to the pool. More...
 
void clear ()
 Stops and deletes all worker threads. More...
 
bool isFull () const
 Checks whether there are currently more pending tasks than threads. More...
 
void lock ()
 locks the pool mutex More...
 
 Pool (int numThreads=0)
 Constructor. More...
 
int size () const
 Returns the number of threads in the pool. More...
 
void unlock ()
 unlocks the pool mutex More...
 
void waitAll ()
 waits for all tasks to be finished More...
 
virtual ~Pool ()
 Destructor. More...
 

Private Attributes

FXCondition myCondition
 the semaphore to wait on for finishing all tasks More...
 
std::list< Task * > myFinishedTasks
 list of finished tasks More...
 
FXMutex myMutex
 the internal mutex for the task list More...
 
int myNumFinished
 the number of finished tasks (is reset when the pool runs empty) More...
 
FXMutex myPoolMutex
 the pool mutex for external sync More...
 
int myRunningIndex
 the running index for the next task More...
 
std::vector< FXWorkerThread * > myWorkers
 the current worker threads More...
 

Detailed Description

A pool of worker threads which distributes the tasks and collects the results.

Definition at line 87 of file FXWorkerThread.h.

Constructor & Destructor Documentation

FXWorkerThread::Pool::Pool ( int  numThreads = 0)
inline

Constructor.

May initialize the pool with a given number of workers.

Parameters
[in]numThreadsthe number of threads to create

Definition at line 95 of file FXWorkerThread.h.

References FXWorkerThread::FXWorkerThread().

virtual FXWorkerThread::Pool::~Pool ( )
inlinevirtual

Destructor.

Stopping and deleting all workers by calling clear.

Definition at line 106 of file FXWorkerThread.h.

References clear().

Member Function Documentation

void FXWorkerThread::Pool::add ( Task *const  t)
inline

Gives a number to the given task and assigns it to a randomly chosen worker.

Parameters
[in]tthe task to add

Definition at line 132 of file FXWorkerThread.h.

References myRunningIndex, myWorkers, and FXWorkerThread::Task::setIndex().

void FXWorkerThread::Pool::addFinished ( Task *const  t)
inline

Adds the given task to the list of finished tasks and assigns it to a randomly chosen worker.

Locks the internal mutex and counts the finished tasks. This is to be called by the worker thread only.

Parameters
[in]tthe task to add

Definition at line 143 of file FXWorkerThread.h.

References myCondition, myFinishedTasks, myMutex, and myNumFinished.

Referenced by FXWorkerThread::run().

void FXWorkerThread::Pool::addWorker ( FXWorkerThread *const  w)
inline

Adds the given thread to the pool.

Parameters
[in]wthe thread to add

Definition at line 123 of file FXWorkerThread.h.

References myWorkers.

Referenced by FXWorkerThread::FXWorkerThread().

void FXWorkerThread::Pool::clear ( )
inline

Stops and deletes all worker threads.

Definition at line 112 of file FXWorkerThread.h.

References myWorkers.

Referenced by ~Pool().

bool FXWorkerThread::Pool::isFull ( ) const
inline

Checks whether there are currently more pending tasks than threads.

This is only a rough estimate because the tasks are already assigned and there could be an idle thread even though the number of tasks is large.

Returns
whether there are enough tasks to let all threads work

Definition at line 174 of file FXWorkerThread.h.

References myNumFinished, myRunningIndex, and size().

void FXWorkerThread::Pool::lock ( )
inline

locks the pool mutex

Definition at line 187 of file FXWorkerThread.h.

References myPoolMutex.

int FXWorkerThread::Pool::size ( ) const
inline

Returns the number of threads in the pool.

Returns
the number of threads

Definition at line 182 of file FXWorkerThread.h.

References myWorkers.

Referenced by isFull().

void FXWorkerThread::Pool::unlock ( )
inline

unlocks the pool mutex

Definition at line 192 of file FXWorkerThread.h.

References myPoolMutex.

void FXWorkerThread::Pool::waitAll ( )
inline

waits for all tasks to be finished

Definition at line 152 of file FXWorkerThread.h.

References myCondition, myFinishedTasks, myMutex, myNumFinished, and myRunningIndex.

Field Documentation

FXCondition FXWorkerThread::Pool::myCondition
private

the semaphore to wait on for finishing all tasks

Definition at line 204 of file FXWorkerThread.h.

Referenced by addFinished(), and waitAll().

std::list<Task*> FXWorkerThread::Pool::myFinishedTasks
private

list of finished tasks

Definition at line 206 of file FXWorkerThread.h.

Referenced by addFinished(), and waitAll().

FXMutex FXWorkerThread::Pool::myMutex
private

the internal mutex for the task list

Definition at line 200 of file FXWorkerThread.h.

Referenced by addFinished(), and waitAll().

int FXWorkerThread::Pool::myNumFinished
private

the number of finished tasks (is reset when the pool runs empty)

Definition at line 210 of file FXWorkerThread.h.

Referenced by addFinished(), isFull(), and waitAll().

FXMutex FXWorkerThread::Pool::myPoolMutex
private

the pool mutex for external sync

Definition at line 202 of file FXWorkerThread.h.

Referenced by lock(), and unlock().

int FXWorkerThread::Pool::myRunningIndex
private

the running index for the next task

Definition at line 208 of file FXWorkerThread.h.

Referenced by add(), isFull(), and waitAll().

std::vector<FXWorkerThread*> FXWorkerThread::Pool::myWorkers
private

the current worker threads

Definition at line 198 of file FXWorkerThread.h.

Referenced by add(), addWorker(), clear(), and size().


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