Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions | Friends

bdlmt::ThreadPool Class Reference

#include <bdlmt_threadpool.h>

List of all members.

Public Types

typedef bsl::function< void()> Job

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (ThreadPool, bslma::UsesBslmaAllocator)
 ThreadPool (const bslmt::ThreadAttributes &threadAttributes, int minThreads, int maxThreads, int maxIdleTime, bslma::Allocator *basicAllocator=0)
 ThreadPool (const bslmt::ThreadAttributes &threadAttributes, int minThreads, int maxThreads, bsls::TimeInterval maxIdleTime, bslma::Allocator *basicAllocator=0)
 ~ThreadPool ()
void drain ()
int enqueueJob (const Job &functor)
int enqueueJob (bslmf::MovableRef< Job > functor)
int enqueueJob (ThreadPoolJobFunc function, void *userData)
double resetPercentBusy ()
void shutdown ()
int start ()
void stop ()
int enabled () const
int maxThreads () const
int maxIdleTime () const
bsls::TimeInterval maxIdleTimeInterval () const
int minThreads () const
int numActiveThreads () const
int numPendingJobs () const
int numWaitingThreads () const
double percentBusy () const
int threadFailures () const

Friends

void * ThreadPoolEntry (void *)

Detailed Description

This class implements a thread pool used for concurrently executing multiple user-defined functions ("jobs").

See Component bdlmt_threadpool


Member Typedef Documentation


Constructor & Destructor Documentation

bdlmt::ThreadPool::ThreadPool ( const bslmt::ThreadAttributes threadAttributes,
int  minThreads,
int  maxThreads,
int  maxIdleTime,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified threadAttributes, the specified minThreads minimum number of threads, the specified maxThreads maximum number of threads, and the specified maxIdleTime idle time (in milliseconds) after which a thread may be considered for destruction. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The behavior is undefined unless 0 <= minThreads, minThreads <= maxThreads, and 0 <= maxIdleTime.

bdlmt::ThreadPool::ThreadPool ( const bslmt::ThreadAttributes threadAttributes,
int  minThreads,
int  maxThreads,
bsls::TimeInterval  maxIdleTime,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified threadAttributes, the specified minThreads minimum number of threads, the specified maxThreads maximum number of threads, and the specified maxIdleTime idle time after which a thread may be considered for destruction. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The behavior is undefined unless 0 <= minThreads, minThreads <= maxThreads, 0 <= maxIdleTime, and the maxIdleTime has a value less than or equal to INT_MAX milliseconds.

bdlmt::ThreadPool::~ThreadPool (  ) 

Call shutdown() and destroy this thread pool.


Member Function Documentation

bdlmt::ThreadPool::BSLMF_NESTED_TRAIT_DECLARATION ( ThreadPool  ,
bslma::UsesBslmaAllocator   
)
void bdlmt::ThreadPool::drain (  ) 

Disable queuing on this thread pool and wait until all pending jobs complete. Use start to re-enable queuing.

int bdlmt::ThreadPool::enqueueJob ( const Job functor  ) 
int bdlmt::ThreadPool::enqueueJob ( bslmf::MovableRef< Job functor  ) 

Enqueue the specified functor to be executed by the next available thread. Return 0 if enqueued successfully, and a non-zero value if queuing is currently disabled. The behavior is undefined unless functor is not "unset". See bsl::function for more information on functors.

int bdlmt::ThreadPool::enqueueJob ( ThreadPoolJobFunc  function,
void *  userData 
)

Enqueue the specified function to be executed by the next available thread. The specified userData pointer will be passed to the function by the processing thread. Return 0 if enqueued successfully, and a non-zero value if queuing is currently disabled.

double bdlmt::ThreadPool::resetPercentBusy (  ) 

Atomically report the percentage of wall time spent by each thread of this thread pool executing jobs since the last reset time, and set the reset time to now. The creation of the thread pool is considered a first reset time. This value is calculated as

                   sum(jobExecutionTime)       100%
          P_busy = --------------------   x ----------
                    timeSinceLastReset      maxThreads

Note that this percentage reflects the wall time spent per thread, and not CPU time per thread, or not even CPU time per processor. Also note that there is no guarantee that all threads are processed concurrently (e.g., the number of threads could be larger than the number of processors).

void bdlmt::ThreadPool::shutdown (  ) 

Disable queuing on this thread pool, cancel all queued jobs, and shut down all processing threads (after all active jobs complete).

int bdlmt::ThreadPool::start (  ) 

Enable queuing on this thread pool and spawn minThreads() processing threads. Return 0 on success, and a non-zero value otherwise. If minThreads() threads were not successfully started, all threads are stopped.

void bdlmt::ThreadPool::stop (  ) 

Disable queuing on this thread pool and wait until all pending jobs complete, then shut down all processing threads.

int bdlmt::ThreadPool::enabled (  )  const

Return the state (enabled or not) of the thread pool.

int bdlmt::ThreadPool::maxThreads (  )  const

Return the maximum number of threads that are allowed to be running at given time.

int bdlmt::ThreadPool::maxIdleTime (  )  const

Return the amount of time (in milliseconds) a thread remains idle before being shut down when there are more than min threads started.

bsls::TimeInterval bdlmt::ThreadPool::maxIdleTimeInterval (  )  const

Return the amount of time a thread remains idle before being shut down when there are more than min threads started.

int bdlmt::ThreadPool::minThreads (  )  const

Return the minimum number of threads that must be started at any given time.

int bdlmt::ThreadPool::numActiveThreads (  )  const

Return the number of threads that are currently processing a job.

int bdlmt::ThreadPool::numPendingJobs (  )  const

Return the number of jobs that are currently queued, but not yet being processed.

int bdlmt::ThreadPool::numWaitingThreads (  )  const

Return the number of threads that are currently waiting for a job.

double bdlmt::ThreadPool::percentBusy (  )  const

Return the percentage of wall time spent by each thread of this thread pool executing jobs since the last reset time. The creation of the thread pool is considered a first reset time. This value is calculated as

                   sum(jobExecutionTime)       100%
          P_busy = --------------------   x ----------
                    timeSinceLastReset      maxThreads

Note that this percentage reflects the wall time spent per thread, and not CPU time per thread, or not even CPU time per processor. Also note that there is no guarantee that all threads are processed concurrently (e.g., the number of threads could be larger than the number of processors).

int bdlmt::ThreadPool::threadFailures (  )  const

Return the number of times that thread creation failed.


Friends And Related Function Documentation

void* ThreadPoolEntry ( void *   )  [friend]

Entry point for processing threads.


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