Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions

bdlmt::MultipriorityThreadPool Class Reference

#include <bdlmt_multiprioritythreadpool.h>

List of all members.

Public Types

enum  { k_MAX_NUM_PRIORITIES = sizeof(int) * CHAR_BIT, BCEP_MAX_NUM_PRIORITIES = k_MAX_NUM_PRIORITIES, MAX_NUM_PRIORITIES = k_MAX_NUM_PRIORITIES }
typedef bsl::function< void()> ThreadFunctor

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (MultipriorityThreadPool, bslma::UsesBslmaAllocator)
 MultipriorityThreadPool (int numThreads, int numPriorities, bslma::Allocator *basicAllocator=0)
 MultipriorityThreadPool (int numThreads, int numPriorities, const bslmt::ThreadAttributes &threadAttributes, bslma::Allocator *basicAllocator=0)
 ~MultipriorityThreadPool ()
int enqueueJob (const ThreadFunctor &job, int priority)
int enqueueJob (bslmt_ThreadFunction jobFunction, void *jobData, int priority)
void enableQueue ()
void disableQueue ()
int startThreads ()
void stopThreads ()
void suspendProcessing ()
void resumeProcessing ()
void drainJobs ()
void removeJobs ()
void shutdown ()
bool isEnabled () const
bool isStarted () const
bool isSuspended () const
int numActiveThreads () const
int numPendingJobs () const
int numPriorities () const
int numStartedThreads () const
int numThreads () const

Detailed Description

This class implements a thread-enabled, integrally-prioritized thread-pool mechanism used for concurrently executing multiple user-defined "jobs" supplied as either conventional C-style functions taking an optional void * data argument, or as more flexible functor objects.

See Component bdlmt_multiprioritythreadpool


Member Typedef Documentation

ThreadFunctor is an alias for a function object that is invokable (with no arguments) and returns void; its use is similar to that of a C-style function pointer and optional void * data pointer passed as arguments when creating a thread.


Member Enumeration Documentation

anonymous enum
Enumerator:
k_MAX_NUM_PRIORITIES 

bits per int

BCEP_MAX_NUM_PRIORITIES 
MAX_NUM_PRIORITIES 

Constructor & Destructor Documentation

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

Create a multi-priority thread pool capable of concurrently executing the specified numThreads "jobs" with associated integer priorities in the specified range [0 .. numPriorities - 1], 0 being the most urgent. Optionally specify threadAttributes used to customize each worker thread created by this thread pool, in which case the attribute pertaining to whether the worker threads are created in the detached state is ignored. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. The newly created thread pool will initially be enabled for enqueuing jobs, but with no worker threads created. The behavior is undefined unless 1 <= numThreads and 1 <= numPriorities <= k_MAX_NUM_PRIORITIES.

bdlmt::MultipriorityThreadPool::~MultipriorityThreadPool (  ) 

Remove (cancel) all pending jobs and destroy this multi-priority thread pool. The behavior is undefined unless this thread pool is stopped.


Member Function Documentation

bdlmt::MultipriorityThreadPool::BSLMF_NESTED_TRAIT_DECLARATION ( MultipriorityThreadPool  ,
bslma::UsesBslmaAllocator   
)
int bdlmt::MultipriorityThreadPool::enqueueJob ( const ThreadFunctor job,
int  priority 
)

Add the specified job to the queue of this multi-priority thread pool, assigning it the specified priority. Return 0 if the job was enqueued successfully, and a non-zero value otherwise (implying that the queue was in the disabled state). The behavior is undefined unless 0 <= priority < numPriorities().

int bdlmt::MultipriorityThreadPool::enqueueJob ( bslmt_ThreadFunction  jobFunction,
void *  jobData,
int  priority 
)

Add a job to the queue of this multi-priority thread pool indicated by the specified jobFunction and associated jobData, assigning it the specified priority. Return 0 if the job was enqueued successfully, and a non-zero value otherwise (implying that the queue was in the disabled state). When invoked, jobFunction is passed the void * address jobData as its only argument. The behavior is undefined unless jobFunction is non-null and 0 <= priority < numPriorities(). Note that jobData may be 0 as long as jobFunction supports that value.

void bdlmt::MultipriorityThreadPool::enableQueue (  ) 

Enable the enqueuing of jobs to this multi-priority thread pool. When this thread pool is enabled, the status returned when calling either overloaded enqueueJob method will be 0, indicating that the job was successfully enqueued. Note that calling this method when the queue is already in the enabled state has no effect.

void bdlmt::MultipriorityThreadPool::disableQueue (  ) 

Disable the enqueuing of jobs to this multi-priority thread pool. When this thread pool is disabled, the status returned when calling either overloaded enqueueJob method will be non-zero, indicating that the job was not enqueued. Note that calling this method when the queue is already in the disabled state has no effect.

int bdlmt::MultipriorityThreadPool::startThreads (  ) 

Create and start numThreads() worker threads in this multi-priority thread pool. Return 0 on success, and a non-zero value with no worker threads and no jobs processed otherwise. This method has no impact on the enabled/disabled or suspended/resumed states of this thread pool. Note that calling this method when this thread pool is already in the started state has no effect. Also note that until this method is called, the thread pool will not process any jobs.

void bdlmt::MultipriorityThreadPool::stopThreads (  ) 

Destroy all worker threads of this multi-priority thread pool after waiting for any active (i.e., already-running) jobs to complete; no new jobs will be allowed to become active. This method has no impact on the enabled/disabled or suspended/resumed states of this thread pool. Note that calling this function when this thread pool is not in the started state has no effect. Also note that calling this method from one of the threads belonging to this thread pool will cause a deadlock.

void bdlmt::MultipriorityThreadPool::suspendProcessing (  ) 

Put this multi-priority thread pool into the suspended state. This method does not suspend any jobs that have begun to execute; such jobs are allowed to complete. No pending jobs, however, will be allowed to begin execution until resumeProcessing() is called. This is orthogonal to start/stop, and enable/disable; no threads are stopped. If this thread pool is started, this call blocks until all threads have finished any jobs they were processing and have gone into suspension. This method has no effect if this thread pool was already in the suspended state. Note that calling this method from one of the threads belonging to this thread pool will cause a deadlock.

void bdlmt::MultipriorityThreadPool::resumeProcessing (  ) 

If this multi-priority thread pool is suspended, resume processing of jobs. This is orthogonal to start/stop, and enable/disable; no threads are started. Note that this method has no effect if this thread pool is not in the suspended state.

void bdlmt::MultipriorityThreadPool::drainJobs (  ) 

Block until all executing jobs and pending jobs enqueued to this multi-priority thread pool complete. This method does not affect the enabled/disabled state of this thread pool. If this thread pool is enabled and jobs are enqueued during draining, this method may return before all enqueued jobs are executed. The behavior is undefined if:

  • this method is called while this thread pool is stopped or suspended, or
  • this method is called concurrently with a call to the removeJobs method, or
  • this method is called by one of the threads belonging to this thread pool.

Note that, in these cases, such undefined behavior may include deadlock.

void bdlmt::MultipriorityThreadPool::removeJobs (  ) 

Remove all pending (i.e., not yet active) jobs from the queue of this multi-priority thread pool. This method does not affect the enabled status of the queue, nor does it affect the started status or any active jobs in this thread pool. The behavior is undefined if this method is called concurrently with the drainJobs method. Note that, in this case, such undefined behavior may include deadlock.

void bdlmt::MultipriorityThreadPool::shutdown (  ) 

Disable the enqueuing of new jobs to this multi-priority thread pool, cancel all pending jobs, and stop all worker threads.

bool bdlmt::MultipriorityThreadPool::isEnabled (  )  const

Return true if the enqueuing of new jobs is enabled for this multi-priority thread pool, and false otherwise.

bool bdlmt::MultipriorityThreadPool::isStarted (  )  const

Return true if all numThreads() worker threads (specified at construction) have been created for this multi-priority thread pool, and false otherwise.

bool bdlmt::MultipriorityThreadPool::isSuspended (  )  const

Return true if the threads of this multi-priority thread pool are currently suspended from processing jobs, and false otherwise.

int bdlmt::MultipriorityThreadPool::numActiveThreads (  )  const

Return a snapshot of the number of threads that are actively processing jobs for this multi-priority thread pool. Note that 0 <= numActiveThreads() <= numThreads() is an invariant of this class.

int bdlmt::MultipriorityThreadPool::numPendingJobs (  )  const

Return a snapshot of the number of jobs currently enqueued to be processed by this multi-priority thread pool, but are not yet running.

int bdlmt::MultipriorityThreadPool::numPriorities (  )  const

Return the fixed number of priorities, specified at construction, that this multi-priority thread pool supports.

int bdlmt::MultipriorityThreadPool::numStartedThreads (  )  const

Return the number of threads that have been started for this multi-priority thread pool. Note that they may be currently suspended.

int bdlmt::MultipriorityThreadPool::numThreads (  )  const

Returns the fixed number of threads, specified at construction, that are started by this multi-priority thread pool.


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