userver: concurrent::MpscQueue< T > Class Template Reference
Loading...
Searching...
No Matches
concurrent::MpscQueue< T > Class Template Referencefinal

#include <userver/concurrent/mpsc_queue.hpp>

Detailed Description

template<typename T>
class concurrent::MpscQueue< T >

Multiple producer, single consumer queue

Example usage:

static constexpr std::chrono::milliseconds kTimeout{10};
auto producer = queue->GetProducer();
auto consumer = queue->GetConsumer();
auto producer_task = utils::Async("producer", [&] {
// ...
if (!producer.Push(1, engine::Deadline::FromDuration(kTimeout))) {
// The reader is dead
}
});
auto consumer_task = utils::Async("consumer", [&] {
for (;;) {
// ...
int item{};
if (consumer.Pop(item, engine::Deadline::FromDuration(kTimeout))) {
// processing the queue element
ASSERT_EQ(item, 1);
} else {
// the queue is empty and there are no more live producers
return;
}
}
});
producer_task.Get();
consumer_task.Get();
See also
Synchronization Primitives

Definition at line 44 of file mpsc_queue.hpp.

+ Inheritance diagram for concurrent::MpscQueue< T >:

Public Types

using ValueType = T
 
using Producer = concurrent::Producer<MpscQueue, ProducerToken, EmplaceEnabler>
 
using Consumer = concurrent::Consumer<MpscQueue, ConsumerToken, EmplaceEnabler>
 
using MultiProducer = concurrent::Producer<MpscQueue, impl::NoToken, EmplaceEnabler>
 

Public Member Functions

Producer GetProducer ()
 
MultiProducer GetMultiProducer ()
 
Consumer GetConsumer ()
 
void SetSoftMaxSize (size_t size)
 Sets the limit on the queue size, pushes over this limit will block.
 
size_t GetSoftMaxSize () const
 Gets the limit on the queue size.
 
size_t GetSizeApproximate () const
 Gets the approximate size of queue.
 

Static Public Member Functions

static std::shared_ptr< MpscQueueCreate (std::size_t max_size=kUnbounded)
 Create a new queue.
 

Static Public Attributes

static constexpr std::size_t kUnbounded = std::numeric_limits<std::size_t>::max()
 

Member Typedef Documentation

◆ Consumer

template<typename T >
using concurrent::MpscQueue< T >::Consumer = concurrent::Consumer<MpscQueue, ConsumerToken, EmplaceEnabler>

Definition at line 64 of file mpsc_queue.hpp.

◆ MultiProducer

template<typename T >
using concurrent::MpscQueue< T >::MultiProducer = concurrent::Producer<MpscQueue, impl::NoToken, EmplaceEnabler>

Definition at line 65 of file mpsc_queue.hpp.

◆ Producer

template<typename T >
using concurrent::MpscQueue< T >::Producer = concurrent::Producer<MpscQueue, ProducerToken, EmplaceEnabler>

Definition at line 63 of file mpsc_queue.hpp.

◆ ValueType

template<typename T >
using concurrent::MpscQueue< T >::ValueType = T

Definition at line 61 of file mpsc_queue.hpp.

Member Function Documentation

◆ Create()

template<typename T >
static std::shared_ptr< MpscQueue > concurrent::MpscQueue< T >::Create ( std::size_t max_size = kUnbounded)
inlinestatic

Create a new queue.

Definition at line 80 of file mpsc_queue.hpp.

◆ GetConsumer()

template<typename T >
MpscQueue< T >::Consumer concurrent::MpscQueue< T >::GetConsumer ( )

Get a Consumer which makes it possible to read items from the queue. Can be called only once. You may not use the Consumer simultaneously from multiple coroutines/threads.

Note
Consumer may outlive the queue and producers.

Definition at line 169 of file mpsc_queue.hpp.

◆ GetMultiProducer()

template<typename T >
MpscQueue< T >::MultiProducer concurrent::MpscQueue< T >::GetMultiProducer ( )

Get a MultiProducer which makes it possible to push items into the queue. Can be called multiple times. The resulting MultiProducer is thread-safe, so it can be used simultaneously from multiple coroutines/threads.

Note
MultiProducer may outlive the queue and the Consumer.

Definition at line 162 of file mpsc_queue.hpp.

◆ GetProducer()

template<typename T >
MpscQueue< T >::Producer concurrent::MpscQueue< T >::GetProducer ( )

Get a Producer which makes it possible to push items into the queue. Can be called multiple times. The resulting Producer is not thread-safe, so you have to use multiple Producers of the same queue to simultaneously write from multiple coroutines/threads.

Note
Producer may outlive the queue and the Consumer.

Definition at line 154 of file mpsc_queue.hpp.

◆ GetSizeApproximate()

template<typename T >
size_t concurrent::MpscQueue< T >::GetSizeApproximate ( ) const

Gets the approximate size of queue.

Definition at line 186 of file mpsc_queue.hpp.

◆ GetSoftMaxSize()

template<typename T >
size_t concurrent::MpscQueue< T >::GetSoftMaxSize ( ) const

Gets the limit on the queue size.

Definition at line 181 of file mpsc_queue.hpp.

◆ SetSoftMaxSize()

template<typename T >
void concurrent::MpscQueue< T >::SetSoftMaxSize ( size_t size)

Sets the limit on the queue size, pushes over this limit will block.

Note
This is a soft limit and may be slightly overrun under load.

Definition at line 176 of file mpsc_queue.hpp.

Friends And Related Symbol Documentation

◆ Consumer< MpscQueue, ConsumerToken, EmplaceEnabler >

template<typename T >
friend class Consumer< MpscQueue, ConsumerToken, EmplaceEnabler >
friend

Definition at line 45 of file mpsc_queue.hpp.

◆ Producer< MpscQueue, ProducerToken, EmplaceEnabler >

template<typename T >
friend class Producer< MpscQueue, ProducerToken, EmplaceEnabler >
friend

Definition at line 45 of file mpsc_queue.hpp.

Member Data Documentation

◆ kUnbounded

template<typename T >
constexpr std::size_t concurrent::MpscQueue< T >::kUnbounded = std::numeric_limits<std::size_t>::max()
staticconstexpr

Definition at line 59 of file mpsc_queue.hpp.


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