Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FX::FXThreadPool Class Reference

A Thread Pool manages execution of jobs on a number of worker threads. More...

#include <FXThreadPool.h>

List of all members.

Public Member Functions

 FXThreadPool ()
 FXThreadPool (FXint min, FXint max=32, FXint run=0)
FXbool active ()
FXint getWaitingThreads ()
FXint getRunningThreads ()
void setMinimumThreads (FXint n)
FXint getMinimumThreads ()
void setMaximumThreads (FXint n)
FXint getMaximumThreads ()
FXint start (FXint min=1, FXint max=32, FXint run=0)
FXint wait ()
FXint stop ()
FXWorkerexecute (FXRunnable *job, FXbool block=true)
virtual ~FXThreadPool ()

Friends

class FXWorker


Detailed Description

A Thread Pool manages execution of jobs on a number of worker threads.

For compute-bound tasks, the amount of parallelism in a program is limited by the number of physical processors available; however for I/O-bound tasks, it makes sense to create more threads than the number of physical processors, in order to more fully utilize available processors. The thread pool executes incoming jobs in parallel, assigning each job to the first available thread out of a fixed pool of worker threads. Fluctuations in work-load can be accomodated by creating a few extra worker threads during peak loads, while terminating superfluous worker threads during periods of low activity, thus minimizing resources. In order to prevent falling behind on incoming jobs, the calling thread can be made to block scheduling the next job until a worker thread becomes available to handle it. When the thread pool is deleted, all worker threads are allowed to complete their tasks prior to destroying the thread pool. The jobs which are passed to the thread pool are derived from FXRunnable. In order to perform some useful function, a subclass of FXRunnable should overload the run() function. Any exceptions thrown by this function are caught in FXWorker, thus the worker thread will remain running despite exceptions thrown by the job object.


Constructor & Destructor Documentation

FX::FXThreadPool::FXThreadPool (  ) 

Construct an empty thread pool.

FX::FXThreadPool::FXThreadPool ( FXint  min,
FXint  max = 32,
FXint  run = 0 
)

Construct an thread pool and call start() to initiate the thread pool run.

virtual FX::FXThreadPool::~FXThreadPool (  )  [virtual]

Signal the running workers to terminate, and wait until all jobs have finished.


Member Function Documentation

FXbool FX::FXThreadPool::active (  ) 

Is pool running.

FXint FX::FXThreadPool::getWaitingThreads (  ) 

Return number of waiting threads.

FXint FX::FXThreadPool::getRunningThreads (  ) 

Return number of worker threads.

void FX::FXThreadPool::setMinimumThreads ( FXint  n  ) 

Change minimum number of worker threads.

FXint FX::FXThreadPool::getMinimumThreads (  ) 

Return minimum number of worker threads.

void FX::FXThreadPool::setMaximumThreads ( FXint  n  ) 

Change maximum number of worker threads.

FXint FX::FXThreadPool::getMaximumThreads (  ) 

Return maximum number of worker threads.

FXint FX::FXThreadPool::start ( FXint  min = 1,
FXint  max = 32,
FXint  run = 0 
)

Start the thread pool; the number of workers will vary between min and max, depending on work-load.

A total of run workers will be started immediately; additional workers will be started on as-needed basis. When the number of available workers exceeds min, any additional workers which finish their assigned job will terminate gracefully so as to minimize the number of inactive threads.

FXint FX::FXThreadPool::wait (  ) 

Wait until all jobs currently in progress have been finished.

One should not execute new jobs while waiting.

FXint FX::FXThreadPool::stop (  ) 

Stop pool.

Wait until all workers have terminated gracefully, i.e. until the last job has been completed.

FXWorker* FX::FXThreadPool::execute ( FXRunnable job,
FXbool  block = true 
)

Execute job on the next available worker thread.

If no worker thread is available, check if the maximum number of worker threads has been reached already. If less than the maximum number of workers is active, create a new worker and start it on the job. Otherwise, if the flag block=true, wait until a worker finishes its job and start it on the new job; if flag block=false, do not start the job at this time. Return the worker to whom the job was assigned, or NULL if the job could not be started.


Friends And Related Function Documentation

friend class FXWorker [friend]

Copyright © 1997-2009 Jeroen van der Zijp