Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions

bdlmt::FixedThreadPool Class Reference

#include <bdlmt_fixedthreadpool.h>

List of all members.

Public Types

enum  { e_SUCCESS = Queue::e_SUCCESS, e_FULL = Queue::e_FULL, e_DISABLED = Queue::e_DISABLED, e_FAILED = Queue::e_FAILED }
enum  {
  e_STOP, e_RUN, e_SUSPEND, e_DRAIN,
  BCEP_STOP = e_STOP, BCEP_RUN = e_RUN, BCEP_SUSPEND = e_SUSPEND, BCEP_DRAIN = e_DRAIN,
  TP_STOP = e_STOP, TP_RUN = e_RUN, TP_SUSPEND = e_SUSPEND, TP_DRAIN = e_DRAIN
}
typedef bsl::function< void()> Job
typedef bdlcc::BoundedQueue< JobQueue

Public Member Functions

 FixedThreadPool (int numThreads, int maxNumPendingJobs, bslma::Allocator *basicAllocator=0)
 FixedThreadPool (const bslmt::ThreadAttributes &threadAttributes, int numThreads, int maxNumPendingJobs, bslma::Allocator *basicAllocator=0)
 ~FixedThreadPool ()
void disable ()
void enable ()
int enqueueJob (const Job &functor)
int enqueueJob (bslmf::MovableRef< Job > functor)
int enqueueJob (FixedThreadPoolJobFunc function, void *userData)
int tryEnqueueJob (const Job &functor)
int tryEnqueueJob (bslmf::MovableRef< Job > functor)
int tryEnqueueJob (FixedThreadPoolJobFunc function, void *userData)
void drain ()
void shutdown ()
int start ()
void stop ()
bool isEnabled () const
bool isStarted () const
int numActiveThreads () const
int numPendingJobs () const
int numThreads () const
int numThreadsStarted () const
int queueCapacity () const

Detailed Description

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

See Component bdlmt_fixedthreadpool


Member Typedef Documentation


Member Enumeration Documentation

anonymous enum
Enumerator:
e_SUCCESS 
e_FULL 
e_DISABLED 
e_FAILED 
anonymous enum
Enumerator:
e_STOP 
e_RUN 
e_SUSPEND 
e_DRAIN 
BCEP_STOP 
BCEP_RUN 
BCEP_SUSPEND 
BCEP_DRAIN 
TP_STOP 
TP_RUN 
TP_SUSPEND 
TP_DRAIN 

Constructor & Destructor Documentation

bdlmt::FixedThreadPool::FixedThreadPool ( int  numThreads,
int  maxNumPendingJobs,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified numThreads number of threads and a job queue of capacity sufficient to enqueue the specified maxNumPendingJobs without blocking. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The behavior is undefined unless 1 <= numThreads.

bdlmt::FixedThreadPool::FixedThreadPool ( const bslmt::ThreadAttributes threadAttributes,
int  numThreads,
int  maxNumPendingJobs,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified threadAttributes, numThreads number of threads, and a job queue with capacity sufficient to enqueue the specified maxNumPendingJobs without blocking. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The behavior is undefined unless 1 <= numThreads.

bdlmt::FixedThreadPool::~FixedThreadPool (  ) 

Remove all pending jobs from the queue without executing them, block until all currently running jobs complete, and then destroy this thread pool.


Member Function Documentation

void bdlmt::FixedThreadPool::disable (  ) 

Disable enqueueing into this pool. All subsequent invocations of enqueueJob or tryEnqueueJob will fail immediately. All blocked invocations of enqueueJob will fail immediately. If the pool is already enqueue disabled, this method has no effect. Note that this method has no effect on jobs currently in the pool.

void bdlmt::FixedThreadPool::enable (  ) 

Enable queuing into this pool. If the queue is not enqueue disabled, this call has no effect.

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

Enqueue the specified functor to be executed by the next available thread. Return 0 on success, and a non-zero value otherwise. Specifically, return e_SUCCESS on success, e_DISABLED if !isEnabled(), and e_FAILED if an error occurs. This operation will block if there is not sufficient capacity in the underlying queue until there is free capacity to successfully enqueue this job. Threads blocked (on enqueue methods) due to the underlying queue being full will unblock and return e_DISABLED if disable is invoked (on another thread). The behavior is undefined unless functor is not null.

int bdlmt::FixedThreadPool::enqueueJob ( FixedThreadPoolJobFunc  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 on success, and a non-zero value otherwise. Specifically, return e_SUCCESS on success, e_DISABLED if !isEnabled(), and e_FAILED if an error occurs. This operation will block if there is not sufficient capacity in the underlying queue until there is free capacity to successfully enqueue this job. Threads blocked (on enqueue methods) due to the underlying queue being full will unblock and return e_DISABLED if disable is invoked (on another thread). The behavior is undefined unless function is not null.

int bdlmt::FixedThreadPool::tryEnqueueJob ( const Job functor  ) 
int bdlmt::FixedThreadPool::tryEnqueueJob ( bslmf::MovableRef< Job functor  ) 

Enqueue the specified functor to be executed by the next available thread. Return 0 on success, and a non-zero value otherwise. Specifically, return e_SUCCESS on success, e_DISABLED if !isEnabled(), e_FULL if isEnabled() and the underlying queue was full, and e_FAILED if an error occurs. The behavior is undefined unless functor is not null.

int bdlmt::FixedThreadPool::tryEnqueueJob ( FixedThreadPoolJobFunc  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 on success, and a non-zero value otherwise. Specifically, return e_SUCCESS on success, e_DISABLED if !isEnabled(), e_FULL if isEnabled() and the underlying queue was full, and e_FAILED if an error occurs. The behavior is undefined unless function is not null.

void bdlmt::FixedThreadPool::drain (  ) 

Wait until the underlying queue is empty without disabling this pool (and may thus wait indefinitely), and then wait until all executing jobs complete. If the thread pool was not already started (isStarted() is false), this method has no effect. Note that if any jobs are submitted concurrently with this method, this method may or may not wait until they have also completed.

void bdlmt::FixedThreadPool::shutdown (  ) 

Disable enqueuing jobs on this thread pool, cancel all pending jobs, wait until all active jobs complete, and join all processing threads. If the thread pool was not already started (isStarted() is false), this method has no effect. At the completion of this method, false == isStarted().

int bdlmt::FixedThreadPool::start (  ) 

Spawn threads until there are numThreads() processing threads. On success, enable enqueuing and return 0. Otherwise, join all threads (ensuring false == isStarted()) and return -1. If the thread pool was already started (isStarted() is true), this method has no effect.

void bdlmt::FixedThreadPool::stop (  ) 

Disable enqueuing jobs on this thread pool, wait until all active and pending jobs complete, and join all processing threads. If the thread pool was not already started (isStarted() is false), this method has no effect. At the completion of this method, false == isStarted().

bool bdlmt::FixedThreadPool::isEnabled (  )  const

Return true if enqueuing jobs is enabled on this thread pool, and false otherwise.

bool bdlmt::FixedThreadPool::isStarted (  )  const

Return true if numThreads() are started on this threadpool and false otherwise (indicating that 0 threads are started on this thread pool.)

int bdlmt::FixedThreadPool::numActiveThreads (  )  const

Return a snapshot of the number of threads that are currently processing a job for this threadpool.

int bdlmt::FixedThreadPool::numPendingJobs (  )  const

Return a snapshot of the number of jobs currently enqueued to be processed by thread pool.

int bdlmt::FixedThreadPool::numThreads (  )  const

Return the number of threads passed to this thread pool at construction.

int bdlmt::FixedThreadPool::numThreadsStarted (  )  const

Return a snapshot of the number of threads currently started by this thread pool.

int bdlmt::FixedThreadPool::queueCapacity (  )  const

Return the capacity of the queue used to enqueue jobs by this thread pool.


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