template<typename T>
psl::spmc::producer class

SPMC based on Chase-Lev deque.

This class is based on Chase-Lev's deque implementation of a Single Producer Multi Consumer (SPMC). The producer should only be used on a single thread, and the 'psl::spmc::consumer' variant should be used in the consuming threads. This offers a safe API.

member-functions

consumer
public noexcept
size
const public noexcept
ssize
const public noexcept
resize
Resizes the internal buffer to the given size.
public
push
Push the given element onto the end of the deque.
public
pop
Pops an element (if any are left) off the deque from the end.
public noexcept
steal
Pops an element (if any are left) off the deque from the front.
private noexcept

Function documentation

template<typename T>
::psl::spmc::consumer<T> psl::spmc::producer<T>::consumer() noexcept

Returns a consumer that is linked to the current producer, to be used in other threads.

template<typename T>
size_t psl::spmc::producer<T>::size() const noexcept

Returns the current count of all elements in the producer.

template<typename T>
int64_t psl::spmc::producer<T>::ssize() const noexcept

Returns the current count of all elements in the producer.

template<typename T>
void psl::spmc::producer<T>::resize(size_t size)

Brief

Resizes the internal buffer to the given size.

Details

Tries to resize to the given size, it will automatically align itself to the next power of 2 if the value isn't a power of 2 already. The minimum size will be 'at least' equal to, or bigger than, the current size (not capacity) of the internal buffer.

template<typename T>
void psl::spmc::producer<T>::push(T&& value)

Brief

Push the given element onto the end of the deque.

Details

Will push the given element onto the deque if enough space is present. If not enough space is present it will construct a new internal buffer that contains at least enough space. If a previous (unused) buffer is present it will now clean that buffer up.

template<typename T>
std::optional<T> psl::spmc::producer<T>::pop() noexcept

Brief

Pops an element (if any are left) off the deque from the end.

Details

Tries to pop an element off the end of the deque.

template<typename T>
std::optional<T> psl::spmc::producer<T>::steal() private noexcept

Brief

Pops an element (if any are left) off the deque from the front.

Details

To be used by consumer threads, this gives a thread safe way of stealing items from the deque. It can be called by any thread, but is only exposed to the consumer class.