BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlmt::FixedThreadPool Class Reference

#include <bdlmt_fixedthreadpool.h>

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
}
 
typedef bsl::function< void()> Job
 
typedef bdlcc::BoundedQueue< JobQueue
 

Public Member Functions

 FixedThreadPool (int numThreads, int maxNumPendingJobs, bslma::Allocator *basicAllocator=0)
 
 FixedThreadPool (int numThreads, int maxNumPendingJobs, const bsl::string_view &threadPoolName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
 
 FixedThreadPool (const bslmt::ThreadAttributes &threadAttributes, int numThreads, int maxNumPendingJobs, bslma::Allocator *basicAllocator=0)
 
 FixedThreadPool (const bslmt::ThreadAttributes &threadAttributes, int numThreads, int maxNumPendingJobs, const bsl::string_view &threadPoolName, bdlm::MetricsRegistry *metricsRegistry, 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 bdlmt_fixedthreadpool

Member Typedef Documentation

◆ Job

◆ Queue

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
e_SUCCESS 
e_FULL 
e_DISABLED 
e_FAILED 

◆ anonymous enum

anonymous enum
Enumerator
e_STOP 
e_RUN 
e_SUSPEND 
e_DRAIN 
BCEP_STOP 
BCEP_RUN 
BCEP_SUSPEND 
BCEP_DRAIN 

Constructor & Destructor Documentation

◆ FixedThreadPool() [1/4]

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 name used for created threads is "bdl.FixedPool". The behavior is undefined unless 1 <= numThreads.

◆ FixedThreadPool() [2/4]

bdlmt::FixedThreadPool::FixedThreadPool ( int  numThreads,
int  maxNumPendingJobs,
const bsl::string_view threadPoolName,
bdlm::MetricsRegistry metricsRegistry,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified numThreads number of threads, a job queue of capacity sufficient to enqueue the specified maxNumPendingJobs without blocking, the specified threadPoolName to be used to identify this thread pool, and the specified metricsRegistry to be used for reporting metrics. If metricsRegistry is 0, bdlm::MetricsRegistry::singleton() is used. Optionally specify a'basicAllocator' used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The name used for created threads is threadPoolName if not empty, otherwise "bdl.FixedPool". The behavior is undefined unless 1 <= numThreads.

◆ FixedThreadPool() [3/4]

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 name used for created threads is threadAttributes.threadName() if not empty, otherwise "bdl.FixedPool". The behavior is undefined unless 1 <= numThreads.

◆ FixedThreadPool() [4/4]

bdlmt::FixedThreadPool::FixedThreadPool ( const bslmt::ThreadAttributes threadAttributes,
int  numThreads,
int  maxNumPendingJobs,
const bsl::string_view threadPoolName,
bdlm::MetricsRegistry metricsRegistry,
bslma::Allocator basicAllocator = 0 
)

Construct a thread pool with the specified threadAttributes, numThreads number of threads, a job queue with capacity sufficient to enqueue the specified maxNumPendingJobs without blocking, the specified threadPoolName to be used to identify this thread pool, and the specified metricsRegistry to be used for reporting metrics. If metricsRegistry is 0, bdlm::MetricsRegistry::singleton() is used. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The name used for created threads is threadAttributes.threadName() if not empty, otherwise threadPoolName if not empty, otherwise "bdl.FixedPool". The behavior is undefined unless 1 <= numThreads.

◆ ~FixedThreadPool()

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

◆ disable()

void bdlmt::FixedThreadPool::disable ( )
inline

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.

◆ drain()

void bdlmt::FixedThreadPool::drain ( )
inline

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.

◆ enable()

void bdlmt::FixedThreadPool::enable ( )
inline

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

◆ enqueueJob() [1/3]

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

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.

◆ enqueueJob() [2/3]

int bdlmt::FixedThreadPool::enqueueJob ( const Job functor)
inline

◆ enqueueJob() [3/3]

int bdlmt::FixedThreadPool::enqueueJob ( FixedThreadPoolJobFunc  function,
void *  userData 
)
inline

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.

◆ isEnabled()

bool bdlmt::FixedThreadPool::isEnabled ( ) const
inline

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

◆ isStarted()

bool bdlmt::FixedThreadPool::isStarted ( ) const
inline

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

◆ numActiveThreads()

int bdlmt::FixedThreadPool::numActiveThreads ( ) const
inline

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

◆ numPendingJobs()

int bdlmt::FixedThreadPool::numPendingJobs ( ) const
inline

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

◆ numThreads()

int bdlmt::FixedThreadPool::numThreads ( ) const
inline

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

◆ numThreadsStarted()

int bdlmt::FixedThreadPool::numThreadsStarted ( ) const
inline

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

◆ queueCapacity()

int bdlmt::FixedThreadPool::queueCapacity ( ) const
inline

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

◆ shutdown()

void bdlmt::FixedThreadPool::shutdown ( )
inline

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().

◆ start()

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.

◆ stop()

void bdlmt::FixedThreadPool::stop ( )
inline

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().

◆ tryEnqueueJob() [1/3]

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

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.

◆ tryEnqueueJob() [2/3]

int bdlmt::FixedThreadPool::tryEnqueueJob ( const Job functor)
inline

◆ tryEnqueueJob() [3/3]

int bdlmt::FixedThreadPool::tryEnqueueJob ( FixedThreadPoolJobFunc  function,
void *  userData 
)
inline

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.


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