BDE 4.14.0 Production release
|
Provide a thread-aware single producer queue of values.
TYPE
This component defines a type, bdlcc::SingleProducerQueue
, that provides an efficient, thread-aware queue of values assuming a single producer (the use of pushBack
and tryPushBack
is done by one thread or a group of threads using external synchronization). The behavior of the methods pushBack
and tryPushBack
is undefined unless the use is by a single producer. This class is ideal for synchronization and communication between threads in a producer-consumer model when there is only one producer thread.
The queue provides pushBack
and popFront
methods for pushing data into the queue and popping data from the queue. The queue will allocate memory as necessary to accommodate pushBack
invocations (pushBack
will never block and is provided for consistency with other containers). When the queue is empty, the popFront
methods block until data appears in the queue. Non-blocking methods tryPushBack
and tryPopFront
are also provided. The tryPopFront
method fails immediately, returning a non-zero value, if the queue is empty.
The queue may be placed into a "enqueue disabled" state using the disablePushBack
method. When disabled, pushBack
and tryPushBack
fail immediately and return an error code. The queue may be restored to normal operation with the enablePushBack
method.
The queue may be placed into a "dequeue disabled" state using the disablePopFront
method. When dequeue disabled, popFront
and tryPopFront
fail immediately and return an error code. Any threads blocked in popFront
when the queue is dequeue disabled return from popFront
immediately and return an error code.
bdlcc::SingleProducerQueue
is a template that is parameterized on the type of element contained within the queue. The supplied template argument, TYPE
, must provide both a default constructor and a copy constructor, as well as an assignment operator. If the default constructor accepts a bslma::Allocator *
, TYPE
must declare the uses bslma::Allocator
trait (see bslma_usesbslmaallocator ) so that the allocator of the queue is propagated to the elements contained in the queue.
A bdlcc::SingleProducerQueue
is exception neutral, and all of the methods of bdlcc::SingleProducerQueue
provide the basic exception safety guarantee (see bsldoc_glossary ).
Move-only types are supported by bdlcc::SingleProducerQueue
on C++11 platforms only (where BSLMF_MOVABLEREF_USES_RVALUE_REFERENCES
is defined), and are not supported on C++03 platforms. Unfortunately, in C++03, there are user types where a bslmf::MovableRef
will not safely degrade to a lvalue reference when a move constructor is not available (types providing a constructor template taking any type), so bslmf::MovableRefUtil::move
cannot be used directly on a user supplied template type. See internal bug report 99039150 for more information.
This section illustrates intended use of this component.
In the following example a bdlcc::SingleProducerQueue
is used to communicate between a single "producer" thread and multiple "consumer" threads. The "producer" will push work requests onto the queue, and each "consumer" will iteratively take a work request from the queue and service the request. This example shows a partial, simplified implementation of the bdlmt::FixedThreadPool
class. See component bdlmt_fixedthreadpool for more information.
First, we define a utility classes that handles a simple "work item":
Next, we provide a simple function to service an individual work item. The details are unimportant for this example:
Then, we define a myConsumer
function that will pop elements off the queue and process them. Note that the call to queue->popFront(&item)
will block until there is an element available on the queue. This function will be executed in multiple threads, so that each thread waits in queue->popFront(&item)
, and bdlcc::SingleProducerQueue
guarantees that each thread gets a unique element from the queue:
Finally, we define a myProducer
function that serves multiple roles: it creates the bdlcc::SingleProducerQueue
, starts the consumer threads, and then produces and enqueues work items. When work requests are exhausted, this function enqueues one e_STOP
item for each consumer queue. This e_STOP
item indicates to the consumer thread to terminate its thread-handling function.
Note that, although the producer cannot control which thread pop
s a particular work item, it can rely on the knowledge that each consumer thread will read a single e_STOP
item and then terminate.