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 75 of file mpsc_queue.hpp.

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

Public Types

using ValueType = T
 
using Producer
 
using Consumer
 
using MultiProducer
 

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
 

Member Typedef Documentation

◆ Consumer

template<typename T >
using concurrent::MpscQueue< T >::Consumer

◆ MultiProducer

template<typename T >
using concurrent::MpscQueue< T >::MultiProducer

◆ Producer

template<typename T >
using concurrent::MpscQueue< T >::Producer

◆ ValueType

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

Definition at line 93 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 116 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 205 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 198 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 190 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 223 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 218 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 213 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 76 of file mpsc_queue.hpp.

◆ Producer< MpscQueue, ProducerToken, EmplaceEnabler >

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

Definition at line 76 of file mpsc_queue.hpp.

Member Data Documentation

◆ kUnbounded

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

Definition at line 90 of file mpsc_queue.hpp.


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