MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin::containers::fifos::SPSC< T > Class Template Referencefinal

A thread-safe, realtime-safe single producer single consumer fifo. More...

#include <marvin_FIFO.h>

Public Member Functions

 SPSC (size_t maxSize)
 
std::optional< T > tryPop () noexcept
 
void tryPush (T &&x) noexcept
 
void emptyQueue () noexcept
 

Detailed Description

template<typename T>
requires std::is_copy_constructible_v<T> && std::is_move_constructible_v<T> && std::is_default_constructible_v<T>
class marvin::containers::fifos::SPSC< T >

A thread-safe, realtime-safe single producer single consumer fifo.

A wrapper around cameron314's readerwriterqueue.
Suitable for passing data between two threads. If the queue is full, pushing will have no effect, and if the queue is empty, popping will return a std::nullopt.
T must be default-constructible, copy constructible and move constructible.
To empty the queue in a single loop:

class SomeClass {
public:
void emptyQueue() {
while(std::optional<int> current = m_fifo.tryPop()) {
std::cout << "Dequeued " << *current << "\n"; // Or do something meaningful with the data here...
}
}
private:
marvin::utils::fifos::SPSC<int> m_fifo;
};
void emptyQueue() noexcept
Definition marvin_FIFO.h:73

Constructor & Destructor Documentation

◆ SPSC()

template<typename T>
marvin::containers::fifos::SPSC< T >::SPSC ( size_t maxSize)
inlineexplicit
Parameters
maxSizeThe capacity of the fifo.

Member Function Documentation

◆ emptyQueue()

template<typename T>
void marvin::containers::fifos::SPSC< T >::emptyQueue ( )
inlinenoexcept

Removes all queued elements, discarding their values.

◆ tryPop()

template<typename T>
std::optional< T > marvin::containers::fifos::SPSC< T >::tryPop ( )
inlinenodiscardnoexcept

Tries to pop an element from the front of the queue.

Returns
std::nullopt if the queue is empty, the value at the front of the queue otherwise.

◆ tryPush()

template<typename T>
void marvin::containers::fifos::SPSC< T >::tryPush ( T && x)
inlinenoexcept

Tries to emplace an element into the back of the queue. If the queue is full, has no effect. Never allocates.

Parameters
xThe value to push into the queue.

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