SpECTRE  v2024.04.12
Parallel::StaticSpscQueue< T, Capacity > Class Template Reference

A static capacity runtime-sized single-producer single-consumer lockfree queue. More...

#include <StaticSpscQueue.hpp>

Public Member Functions

 StaticSpscQueue (const StaticSpscQueue &)=delete
 
StaticSpscQueueoperator= (const StaticSpscQueue &)=delete
 
 StaticSpscQueue (StaticSpscQueue &&)=delete
 
StaticSpscQueueoperator= (StaticSpscQueue &&)=delete
 
template<typename... Args>
void emplace (Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args &&... >)
 Construct a new element at the end of the queue in place. More...
 
template<typename... Args>
bool try_emplace (Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args &&... >)
 Construct a new element at the end of the queue in place. More...
 
void push (const T &v) noexcept(std::is_nothrow_copy_constructible_v< T >)
 Push a new element to the end of the queue. More...
 
template<typename P , Requires< std::is_constructible_v< T, P && > > = nullptr>
void push (P &&v) noexcept(std::is_nothrow_constructible_v< T, P && >)
 Push a new element to the end of the queue. More...
 
bool try_push (const T &v) noexcept(std::is_nothrow_copy_constructible_v< T >)
 Push a new element to the end of the queue. Returns false if the queue is at capacity and does not push the new object, otherwise returns true. More...
 
template<typename P , Requires< std::is_constructible_v< T, P && > > = nullptr>
bool try_push (P &&v) noexcept(std::is_nothrow_constructible_v< T, P && >)
 Push a new element to the end of the queue. Returns false if the queue is at capacity and does not push the new object, otherwise returns true. More...
 
T * front () noexcept
 Returns the first element from the queue. More...
 
void pop ()
 Removes the first element from the queue.
 
size_t size () const noexcept
 Returns the size of the queue at a particular hardware state. More...
 
bool empty () const noexcept
 Returns true if the queue may be empty, otherwise false. More...
 
size_t capacity () const noexcept
 Returns the capacity of the queue.
 

Detailed Description

template<typename T, size_t Capacity>
class Parallel::StaticSpscQueue< T, Capacity >

A static capacity runtime-sized single-producer single-consumer lockfree queue.

As long as only one thread reads and writes simultaneously the queue is threadsafe. Which threads read and write can change throughout program execution, the important thing is that there is no instance during the execution where more than one thread tries to read and where more than one thread tries to write.

Note
This class is intentionally not serializable since handling threadsafety around serialization requires careful thought of the individual circumstances.

Member Function Documentation

◆ emplace()

template<typename T , size_t Capacity>
template<typename... Args>
void Parallel::StaticSpscQueue< T, Capacity >::emplace ( Args &&...  args)
inlinenoexcept

Construct a new element at the end of the queue in place.

Uses placement new for in-place construction.

Warning
This may overwrite existing elements if capacity() is exceeded without warning.

◆ empty()

template<typename T , size_t Capacity>
bool Parallel::StaticSpscQueue< T, Capacity >::empty ( ) const
inlinenoexcept

Returns true if the queue may be empty, otherwise false.

Note that while this can be checked in a threadsafe manner, it is up to the user to guarantee that another thread does not change the queue between when empty() is called and how the result is used.

◆ front()

template<typename T , size_t Capacity>
T * Parallel::StaticSpscQueue< T, Capacity >::front ( )
inlinenoexcept

Returns the first element from the queue.

Note
Returns nullptr if the queue is empty.

◆ push() [1/2]

template<typename T , size_t Capacity>
void Parallel::StaticSpscQueue< T, Capacity >::push ( const T &  v)
inlinenoexcept

Push a new element to the end of the queue.

Uses emplace() internally.

Warning
This may overwrite existing elements if capacity() is exceeded without warning.

◆ push() [2/2]

template<typename T , size_t Capacity>
template<typename P , Requires< std::is_constructible_v< T, P && > > = nullptr>
void Parallel::StaticSpscQueue< T, Capacity >::push ( P &&  v)
inlinenoexcept

Push a new element to the end of the queue.

Uses emplace() internally.

Warning
This may overwrite existing elements if capacity() is exceeded without warning.

◆ size()

template<typename T , size_t Capacity>
size_t Parallel::StaticSpscQueue< T, Capacity >::size ( ) const
inlinenoexcept

Returns the size of the queue at a particular hardware state.

Note that while this can be checked in a threadsafe manner, it is up to the user to guarantee that another thread does not change the queue between when size() is called and how the result is used.

◆ try_emplace()

template<typename T , size_t Capacity>
template<typename... Args>
bool Parallel::StaticSpscQueue< T, Capacity >::try_emplace ( Args &&...  args)
inlinenoexcept

Construct a new element at the end of the queue in place.

Uses placement new for in-place construction.

Returns true if the emplacement succeeded and false if it did not. If it failed then the queue is currently full.

◆ try_push() [1/2]

template<typename T , size_t Capacity>
bool Parallel::StaticSpscQueue< T, Capacity >::try_push ( const T &  v)
inlinenoexcept

Push a new element to the end of the queue. Returns false if the queue is at capacity and does not push the new object, otherwise returns true.

Uses try_emplace() internally.

◆ try_push() [2/2]

template<typename T , size_t Capacity>
template<typename P , Requires< std::is_constructible_v< T, P && > > = nullptr>
bool Parallel::StaticSpscQueue< T, Capacity >::try_push ( P &&  v)
inlinenoexcept

Push a new element to the end of the queue. Returns false if the queue is at capacity and does not push the new object, otherwise returns true.

Uses try_emplace() internally.


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