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>

Inheritance diagram for FX::FXThreadPool:
FX::FXRunnable

List of all members.

Public Member Functions

 FXThreadPool (FXuint sz=256)
 Construct an empty thread pool, with given job queue size.
FXbool setSize (FXuint sz)
 Change job queue size, return true if success.
FXuint getSize () const
 Return job queue size.
FXbool active () const
 Is pool running.
FXuint getWaitingThreads () const
 Return number of waiting threads.
FXuint getRunningThreads () const
 Return number of worker threads.
void setMinimumThreads (FXuint n)
 Change minimum number of worker threads.
FXuint getMinimumThreads () const
 Return minimum number of worker threads.
void setMaximumThreads (FXuint n)
 Change maximum number of worker threads.
FXuint getMaximumThreads () const
 Return maximum number of worker threads.
void setExpiration (FXTime ns=forever)
 Change expiration time.
FXTime getExpiration () const
 Get expiration time.
FXuint start (FXuint min=1, FXuint max=32, FXuint cnt=0)
 Start the thread pool; the number of workers will vary between min and max, depending on work-load.
FXbool wait ()
 Wait until all jobs currently in the queue are finished.
void stop ()
 Stop pool.
FXbool execute (FXRunnable *job, FXTime blocking=forever)
 Execute a job on the thread pool by entering it into the job queue.
virtual FXint run ()
 Called by a (worker) thread to process jobs from the job queue.
virtual ~FXThreadPool ()
 Signal the running workers to terminate, and wait until all jobs have finished.

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, minimizing system 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.


Member Function Documentation

FXbool FX::FXThreadPool::execute ( FXRunnable job,
FXTime  blocking = forever 
)

Execute a job on the thread pool by entering it into the job queue.

If no worker threads are available to process the job and the maximum number of threads has not yet been exceeded, a new worker thread will be started. Otherwise, wait for a timeout interval given by blocking for a spot in the job queue to become available. If a spot becomes available in the job queue within the timeout interval, add the job to the queue and return true. Return false if the thread pool wasn't running yet, if a new worker thread could not be created when desired, or the job could not be added within the given time interval.

virtual FXint FX::FXThreadPool::run ( ) [virtual]

Called by a (worker) thread to process jobs from the job queue.

Returns when the thread pool is stopped, the number of threads exceeds the minimum number of threads, or no new jobs arrive within a set amount of time. Returns true if the pool is still running.

Implements FX::FXRunnable.

FXuint FX::FXThreadPool::start ( FXuint  min = 1,
FXuint  max = 32,
FXuint  cnt = 0 
)

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

A total of cnt workers will be started immediately; additional workers may 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. Returns the number of workers successfully started.

void FX::FXThreadPool::stop ( )

Stop pool.

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

FXbool FX::FXThreadPool::wait ( )

Wait until all jobs currently in the queue are finished.

Returns true if all worker threads have become idle, i.e. the queue is empty. If the thread pool has been stopped, it returns false.


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

Copyright © 1997-2011 Jeroen van der Zijp