Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::SpscQueue< T > Class Template Reference

Fixed-capacity FIFO queue passing values from one thread to another without locks. More...

#include <aegis2d/core/spsc_queue.hpp>

Public Member Functions

 SpscQueue (std::size_t capacity)
 Creates an empty queue.
 
 SpscQueue (const SpscQueue &)=delete
 
SpscQueue & operator= (const SpscQueue &)=delete
 
 SpscQueue (SpscQueue &&)=delete
 
SpscQueue & operator= (SpscQueue &&)=delete
 
 ~SpscQueue ()=default
 Destroys the values still queued.
 
template<typename... Args>
bool try_emplace (Args &&... args)
 Builds a value at the back of the queue. Producer only.
 
bool try_push (T &&value)
 Adds a value at the back of the queue. Producer only.
 
bool try_push (const T &value)
 Adds a copy of a value at the back of the queue. Producer only.
 
std::optional< T > try_pop ()
 Takes the value at the front of the queue. Consumer only.
 
template<typename Consume >
std::size_t drain (Consume consume, std::size_t limit=std::numeric_limits< std::size_t >::max())
 Takes values from the front of the queue and hands each to consume. Consumer only.
 
std::size_t capacity () const noexcept
 Largest number of values the queue holds at once.
 
std::size_t size () const noexcept
 Number of values queued, as seen at the time of the call.
 
bool empty () const noexcept
 Tells whether the queue was empty at the time of the call.
 

Detailed Description

template<typename T>
class aegis2d::core::SpscQueue< T >

Fixed-capacity FIFO queue passing values from one thread to another without locks.

One thread, the producer, adds values with try_push() or try_emplace(); one other thread, the consumer, takes them out with try_pop() or drain(). Neither ever waits on the other: the producer is told when the queue is full, the consumer when it is empty. Memory is allocated once, by the constructor; pushing and popping never allocate (the values themselves may).

The network thread and the game thread use two of them, one in each direction, so the simulation never blocks on a socket.

Exactly one thread may push and exactly one thread may pop at a time; size() and empty() may be called from either and are only a snapshot.

Constructor & Destructor Documentation

◆ SpscQueue() [1/3]

template<typename T >
aegis2d::core::SpscQueue< T >::SpscQueue ( std::size_t  capacity)
inlineexplicit

Creates an empty queue.

Parameters
capacityLargest number of values the queue holds at once.
Exceptions
std::invalid_argumentif capacity is 0.

◆ SpscQueue() [2/3]

template<typename T >
aegis2d::core::SpscQueue< T >::SpscQueue ( const SpscQueue< T > &  )
delete

◆ SpscQueue() [3/3]

template<typename T >
aegis2d::core::SpscQueue< T >::SpscQueue ( SpscQueue< T > &&  )
delete

◆ ~SpscQueue()

template<typename T >
aegis2d::core::SpscQueue< T >::~SpscQueue ( )
default

Destroys the values still queued.

Member Function Documentation

◆ capacity()

template<typename T >
std::size_t aegis2d::core::SpscQueue< T >::capacity ( ) const
inlinenoexcept

Largest number of values the queue holds at once.

Returns
The capacity given to the constructor.

◆ drain()

template<typename T >
template<typename Consume >
std::size_t aegis2d::core::SpscQueue< T >::drain ( Consume  consume,
std::size_t  limit = std::numeric_limits<std::size_t>::max() 
)
inline

Takes values from the front of the queue and hands each to consume. Consumer only.

Values pushed while draining may be taken too, up to limit. If consume throws, the value it was given is lost and the others stay queued.

Parameters
consumeCalled with each value, as an rvalue, oldest first.
limitLargest number of values to take, for instance to bound one tick's work.
Returns
Number of values taken.

◆ empty()

template<typename T >
bool aegis2d::core::SpscQueue< T >::empty ( ) const
inlinenoexcept

Tells whether the queue was empty at the time of the call.

Returns
true if nothing was queued.

◆ operator=() [1/2]

template<typename T >
SpscQueue & aegis2d::core::SpscQueue< T >::operator= ( const SpscQueue< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
SpscQueue & aegis2d::core::SpscQueue< T >::operator= ( SpscQueue< T > &&  )
delete

◆ size()

template<typename T >
std::size_t aegis2d::core::SpscQueue< T >::size ( ) const
inlinenoexcept

Number of values queued, as seen at the time of the call.

Returns
A snapshot: the other thread may change it right away.

◆ try_emplace()

template<typename T >
template<typename... Args>
bool aegis2d::core::SpscQueue< T >::try_emplace ( Args &&...  args)
inline

Builds a value at the back of the queue. Producer only.

Parameters
argsArguments of T's constructor.
Returns
false, without building anything, if the queue is full.

◆ try_pop()

template<typename T >
std::optional< T > aegis2d::core::SpscQueue< T >::try_pop ( )
inline

Takes the value at the front of the queue. Consumer only.

Returns
The oldest value, or nothing if the queue is empty.

◆ try_push() [1/2]

template<typename T >
bool aegis2d::core::SpscQueue< T >::try_push ( const T &  value)
inline

Adds a copy of a value at the back of the queue. Producer only.

Parameters
valueValue to copy into the queue.
Returns
false if the queue is full.

◆ try_push() [2/2]

template<typename T >
bool aegis2d::core::SpscQueue< T >::try_push ( T &&  value)
inline

Adds a value at the back of the queue. Producer only.

Parameters
valueValue to move into the queue.
Returns
false if the queue is full; value is then left unchanged.

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