Quick Links: |
Provides thread pools and event schedulers. More...
Components | |
Component bdlmt_eventscheduler | |
Provide a thread-safe recurring and one-time event scheduler. | |
Component bdlmt_fixedthreadpool | |
Provide portable implementation for a fixed-size pool of threads. | |
Component bdlmt_multiprioritythreadpool | |
Provide a mechanism to parallelize a prioritized sequence of jobs. | |
Component bdlmt_multiqueuethreadpool | |
Provide a pool of queues, each processed serially by a thread pool. | |
Component bdlmt_signaler | |
Provide an implementation of a managed signals and slots system. | |
Component bdlmt_threadmultiplexor | |
Provide a mechanism for partitioning a collection of threads. | |
Component bdlmt_threadpool | |
Provide portable implementation for a dynamic pool of threads. | |
Component bdlmt_throttle | |
Provide mechanism for limiting the rate at which actions may occur. | |
Component bdlmt_timereventscheduler | |
Provide a thread-safe recurring and non-recurring event scheduler. |
bdlmt
("Basic Development Library Multi Thread") package provides components for creating and managing thread pools, and components for scheduling (time-based) events. bdlmt_threadpool
component allows clients to configure the pool so that it grows and shrinks according to user demand, manage thread availability, and schedule client "jobs" to be run independently as threads in the pool become available. It does this by placing client requests on an internal job queue, and controlling multiple threads as they remove jobs from the queue and execute them. bdlmt
package currently has 9 components having 2 levels of physical dependency. The list below shows the hierarchical ordering of the components. The order of components within each level is not architecturally significant, just alphabetical. 2. bdlmt_multiqueuethreadpool bdlmt_threadmultiplexor 1. bdlmt_eventscheduler bdlmt_fixedthreadpool bdlmt_multiprioritythreadpool bdlmt_signaler bdlmt_threadpool bdlmt_throttle bdlmt_timereventscheduler
bdlmt_eventscheduler
: bdlmt_fixedthreadpool
: bdlmt_multiprioritythreadpool
: bdlmt_multiqueuethreadpool
: bdlmt_signaler
: bdlmt_threadmultiplexor
: bdlmt_threadpool
: bdlmt_throttle
: bdlmt_timereventscheduler
: bdlmt_MultipriorityThreadPool
. The plan is for other threadpools to move to this model at a later date. void
: extern "C" void job(void *); // Idiomatic C-style function signature
void
. +-------------------------------------------------------------------------+ | ThreadPool *Control* Methods | | | | Front Operations Middle Operations Back Operations | | ---------------- ----------------- --------------- | | int startThreads() void removeJobs() void enableQueue() | | void stopThreads() void drainJobs() void disableQueue() | | int resumeProcessing() int enqueueJob(func,arg) | | int suspendProcessing() int enqueueJob(job) | | | +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+ | +--<--+--<--+--<--+--<--+--<--+----------------+ | | | | | | | | | | | Front <==| Job | Job | Job | Job | Job | |==< Back | | | | | | | | | | | +--<--+--<--+--<--+--<--+--<--+----------------+ | | | | ,----------------. ,-----------------. | | ( N Worker Threads ) ( Thread Attributes ) | | `----------------' `-----------------' | +-------------------------------------------------------------------------+ Figure 1: Illustration of Generalized Thread Pool
bdlcc
) queue -- e.g., (limited-capacity) FixedQueue
, (heap-based) PriorityQueue
, and (array-based) MultipriorityQueue
. Nonetheless, each of the threadpool objects in bdlmt
should provide a suite of input and control operations that are consistent in both name and behavior across the bdlmt
package. void drainJobs()
, the basic functionality of which is to block
the caller until all of the pending jobs complete (i.e., the queue is empty and all worker threads are idle). Should drainJobs()
also leave the queue in the disabled state? Even if that is a common usage pattern, it is often useful to start with simple, orthogonal behaviors, and if needed, define more complex behaviors in terms of them. drainJobs()
and removeJobs()
If we invoke removeJobs()
, then all currently pending (i.e., not started) work items will be removed (i.e., canceled). During this process, clients attempting to add work items [Back] will block, but their eventual success or failure, (which is based solely on whether the queue is enabled or disabled) is not affected. Note that jobs that are already in progress [Front] are also unaffected. Similarly, invoking our orthogonal drainJobs()
method will block enqueuing clients until all pending jobs have completed, but will not affect the enabledness of the thread pool [Back], nor the processing of work items [Front]. startThreads()
method attempts to create these threads (unless they are already created). The startThreads()
method returns 0 if all of these threads are started, and a non-zero value otherwise (in which case none of the worker threads are started). Redundant calls to startThreads()
do nothing and return zero. Invoking stopThreads()
destroys each worker thread (after it completes any current job). Note that the current contents of the queue [Middle], and the ability to enqueue new jobs [Back] are not affected. suspend
and disable
features, so it is imperative that newly created threadpools be both non-suspended and enabled so users can remain blissfully ignorant of them. It is also important the first usage examples, if not all of them, omit use of these features to minimize learning time for the typical user. void shutdown()
. This method is best described as a composition of the simple, orthogonal functions described above. In order to shut down a thread pool, we need to first disable the enqueuing of any additional jobs, then remove all of the pending work items, and finally stop all of the active threads: void shutdown()
{
disableQueue();
removeJobs();
stopThreads();
}
SIGBUS SIGFPE SIGILL SIGSEGV SIGSYS SIGABRT SIGTRAP SIGIOT