Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MFXWorkerThread::Pool Class Reference

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

#include <MFXWorkerThread.h>

Collaboration diagram for MFXWorkerThread::Pool:
[legend]

Public Member Functions

void add (Task *const t, int index=-1)
 Gives a number to the given task and assigns it to the worker with the given index. If the index is negative, assign to the next (round robin) one.
 
void addFinished (std::list< Task * > &tasks)
 Adds the given tasks to the list of finished tasks.
 
void addWorker (MFXWorkerThread *const w)
 Adds the given thread to the pool.
 
void clear ()
 Stops and deletes all worker threads.
 
const std::vector< MFXWorkerThread * > & getWorkers ()
 
bool isFull () const
 Checks whether there are currently more pending tasks than threads.
 
void lock ()
 locks the pool mutex
 
 Pool (int numThreads=0)
 Constructor.
 
void setException (ProcessError &e)
 
int size () const
 Returns the number of threads in the pool.
 
void unlock ()
 unlocks the pool mutex
 
void waitAll (const bool deleteFinished=true)
 waits for all tasks to be finished
 
virtual ~Pool ()
 Destructor.
 

Private Attributes

FXCondition myCondition
 the semaphore to wait on for finishing all tasks
 
ProcessErrormyException
 the exception from a child thread
 
std::list< Task * > myFinishedTasks
 list of finished tasks
 
FXMutex myMutex
 the internal mutex for the task list
 
FXMutex myPoolMutex
 the pool mutex for external sync
 
int myRunningIndex
 the running index for the next task
 
std::vector< MFXWorkerThread * > myWorkers
 the current worker threads
 

Detailed Description

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

Definition at line 86 of file MFXWorkerThread.h.

Constructor & Destructor Documentation

◆ Pool()

MFXWorkerThread::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 94 of file MFXWorkerThread.h.

◆ ~Pool()

virtual MFXWorkerThread::Pool::~Pool ( )
inlinevirtual

Destructor.

Stopping and deleting all workers by calling clear.

Definition at line 118 of file MFXWorkerThread.h.

References clear().

Member Function Documentation

◆ add()

void MFXWorkerThread::Pool::add ( Task *const  t,
int  index = -1 
)
inline

Gives a number to the given task and assigns it to the worker with the given index. If the index is negative, assign to the next (round robin) one.

Parameters
[in]tthe task to add
[in]indexindex of the worker thread to use or -1 for an arbitrary one

Definition at line 145 of file MFXWorkerThread.h.

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

Referenced by LandmarkLookupTable< E, V, M >::LandmarkLookupTable(), and MSRoutingEngine::reroute().

Here is the caller graph for this function:

◆ addFinished()

void MFXWorkerThread::Pool::addFinished ( std::list< Task * > &  tasks)
inline

Adds the given tasks to the list of finished tasks.

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

Parameters
[in]tasksthe tasks to add

Definition at line 167 of file MFXWorkerThread.h.

References myCondition, myFinishedTasks, and myMutex.

Referenced by MFXWorkerThread::run().

Here is the caller graph for this function:

◆ addWorker()

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

Adds the given thread to the pool.

Parameters
[in]wthe thread to add

Definition at line 135 of file MFXWorkerThread.h.

References myWorkers.

Referenced by MFXWorkerThread::MFXWorkerThread().

Here is the caller graph for this function:

◆ clear()

void MFXWorkerThread::Pool::clear ( )
inline

Stops and deletes all worker threads.

Definition at line 124 of file MFXWorkerThread.h.

References myWorkers.

Referenced by ~Pool().

Here is the caller graph for this function:

◆ getWorkers()

const std::vector< MFXWorkerThread * > & MFXWorkerThread::Pool::getWorkers ( )
inline

Definition at line 257 of file MFXWorkerThread.h.

References myWorkers.

Referenced by MSRoutingEngine::getIntermodalRouterTT(), MSRoutingEngine::getRouterTT(), and MSRoutingEngine::initRouter().

Here is the caller graph for this function:

◆ isFull()

bool MFXWorkerThread::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 235 of file MFXWorkerThread.h.

References myFinishedTasks, myRunningIndex, and size().

◆ lock()

void MFXWorkerThread::Pool::lock ( )
inline

locks the pool mutex

Definition at line 248 of file MFXWorkerThread.h.

References myPoolMutex.

◆ setException()

void MFXWorkerThread::Pool::setException ( ProcessError e)
inline

Definition at line 174 of file MFXWorkerThread.h.

References myException, and myMutex.

Referenced by MFXWorkerThread::run().

Here is the caller graph for this function:

◆ size()

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

Returns the number of threads in the pool.

Returns
the number of threads

Definition at line 243 of file MFXWorkerThread.h.

References myWorkers.

Referenced by MSRoutingEngine::getIntermodalRouterTT(), MSRoutingEngine::getRouterTT(), MSRoutingEngine::initRouter(), isFull(), LandmarkLookupTable< E, V, M >::LandmarkLookupTable(), MSRoutingEngine::reroute(), and MSRoutingEngine::reroute().

Here is the caller graph for this function:

◆ unlock()

void MFXWorkerThread::Pool::unlock ( )
inline

unlocks the pool mutex

Definition at line 253 of file MFXWorkerThread.h.

References myPoolMutex.

◆ waitAll()

void MFXWorkerThread::Pool::waitAll ( const bool  deleteFinished = true)
inline

waits for all tasks to be finished

Definition at line 183 of file MFXWorkerThread.h.

References MAX2(), MIN2(), myCondition, myException, myFinishedTasks, myMutex, myRunningIndex, myWorkers, toString(), WORKLOAD_INTERVAL, and WRITE_MESSAGE.

Referenced by LandmarkLookupTable< E, V, M >::LandmarkLookupTable().

Here is the caller graph for this function:

Field Documentation

◆ myCondition

FXCondition MFXWorkerThread::Pool::myCondition
private

the semaphore to wait on for finishing all tasks

Definition at line 268 of file MFXWorkerThread.h.

Referenced by addFinished(), and waitAll().

◆ myException

ProcessError* MFXWorkerThread::Pool::myException
private

the exception from a child thread

Definition at line 274 of file MFXWorkerThread.h.

Referenced by setException(), and waitAll().

◆ myFinishedTasks

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

list of finished tasks

Definition at line 270 of file MFXWorkerThread.h.

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

◆ myMutex

FXMutex MFXWorkerThread::Pool::myMutex
private

the internal mutex for the task list

Definition at line 264 of file MFXWorkerThread.h.

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

◆ myPoolMutex

FXMutex MFXWorkerThread::Pool::myPoolMutex
private

the pool mutex for external sync

Definition at line 266 of file MFXWorkerThread.h.

Referenced by lock(), and unlock().

◆ myRunningIndex

int MFXWorkerThread::Pool::myRunningIndex
private

the running index for the next task

Definition at line 272 of file MFXWorkerThread.h.

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

◆ myWorkers

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

the current worker threads

Definition at line 262 of file MFXWorkerThread.h.

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


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