Diligent Engine
 
Loading...
Searching...
No Matches
Diligent::MPSCQueue< T > Class Template Reference

#include <MPSCQueue.hpp>

Public Member Functions

 MPSCQueue ()
 Constructs an empty MPSCQueue.
 
 ~MPSCQueue ()
 
void Enqueue (T value)
 
bool Dequeue (T &result)
 
bool IsEmpty () const
 Checks if the queue is empty.
 
size_t Size () const
 Returns the approximate number of items in the queue.
 

Detailed Description

template<typename T>
class Diligent::MPSCQueue< T >

Multi-Producer Single-Consumer (MPSC) queue.

The queue enables multiple producers to enqueue items concurrently, while a single consumer can dequeue items. Dequeue operations are lock-free.

Template Parameters
TThe type of items stored in the queue. Must be default-constructible, move-constructible, and move-assignable.

Constructor & Destructor Documentation

◆ ~MPSCQueue()

template<typename T>
Diligent::MPSCQueue< T >::~MPSCQueue ( )
inline

Destructs the MPSCQueue and releases all resources.

Warning
Not thread-safe. All producers must be stopped/joined before destruction.

Member Function Documentation

◆ Dequeue()

template<typename T>
bool Diligent::MPSCQueue< T >::Dequeue ( T & result)
inline

Dequeues a value from the queue. The method is thread-safe and can be called in parallel with producers, but only one consumer should call it.

Parameters
resultA reference to store the dequeued value. It will be moved from the queue.
Returns
true if a value was successfully dequeued; false if the queue was empty.

◆ Enqueue()

template<typename T>
void Diligent::MPSCQueue< T >::Enqueue ( T value)
inline

Enqueues a value into the queue. The method is thread-safe and can be called concurrently by multiple producers.

Parameters
valueThe value to enqueue. It will be moved into the queue.

◆ IsEmpty()

template<typename T>
bool Diligent::MPSCQueue< T >::IsEmpty ( ) const
inline

Checks if the queue is empty.

The method is safe to call concurrently with producers, but must only be called by the consumer thread.

Note
In the presence of concurrent producers, this method may temporarily report the queue as empty while an enqueue is still in progress and the new node has not yet been linked into the queue.

◆ Size()

template<typename T>
size_t Diligent::MPSCQueue< T >::Size ( ) const
inline

Returns the approximate number of items in the queue.

Note
This value is not exact in the presence of concurrent producers and consumer. It may temporarily overestimate the number of items due to in-flight enqueue operations, and may disagree with IsEmpty() or a subsequent Dequeue() call. It must not be used to predict whether Dequeue() will succeed.