Fixed-capacity FIFO queue passing values from one thread to another without locks.
More...
|
| | 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.
|
| |
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.