Template Class LockFreeQueue

Class Documentation

template<typename ElementType, uint64_t Capacity>
class LockFreeQueue

implements a lock free queue (i.e. container with FIFO order) of elements of type T with a fixed Capacity

Public Types

using element_t = ElementType

Public Functions

LockFreeQueue() noexcept

creates and initalizes an empty LockFreeQueue

~LockFreeQueue() noexcept = default
LockFreeQueue(const LockFreeQueue&) = delete
LockFreeQueue(LockFreeQueue&&) = delete
LockFreeQueue &operator=(const LockFreeQueue&) = delete
LockFreeQueue &operator=(LockFreeQueue&&) = delete
constexpr uint64_t capacity() const noexcept

returns the capacity of the queue

Note

threadsafe, lockfree

bool tryPush(ElementType &&value) noexcept

tries to insert value in FIFO order, moves the value internally

Note

threadsafe, lockfree

Parameters:

value – to be inserted

Returns:

true if insertion was successful (i.e. queue was not full during push), false otherwise

bool tryPush(const ElementType &value) noexcept

tries to insert value in FIFO order, copies the value internally

Note

threadsafe, lockfree

Parameters:

value – to be inserted

Returns:

true if insertion was successful (i.e. queue was not full during push), false otherwise

iox::cxx::optional<ElementType> push(const ElementType &value) noexcept

inserts value in FIFO order, always succeeds by removing the oldest value when the queue is detected to be full (overflow)

Note

threadsafe, lockfree

Parameters:

value – to be inserted is copied into the queue

Returns:

removed value if an overflow occured, empty optional otherwise

iox::cxx::optional<ElementType> push(ElementType &&value) noexcept

inserts value in FIFO order, always succeeds by removing the oldest value when the queue is detected to be full (overflow)

Note

threadsafe, lockfree

Parameters:

value – to be inserted is moved into the queue if possible

Returns:

removed value if an overflow occured, empty optional otherwise

iox::cxx::optional<ElementType> pop() noexcept

tries to remove value in FIFO order

Note

threadsafe, lockfree

Returns:

value if removal was successful, empty optional otherwise

bool empty() const noexcept

check whether the queue is empty

Note

that if the queue is used concurrently it might not be empty anymore after the call (but it was at some point during the call)

Note

threadsafe, lockfree

Returns:

true iff the queue is empty

uint64_t size() const noexcept

get the number of stored elements in the queue

Note

that this will not be perfectly in sync with the actual number of contained elements during concurrent operation but will always be at most capacity

Note

threadsafe, lockfree

Returns:

number of stored elements in the queue

Protected Types

using Queue = IndexQueue<Capacity>
using BufferIndex = typename Queue::value_t

Protected Functions

template<typename T>
void writeBufferAt(const BufferIndex&, T&&) noexcept
template<typename T>
iox::cxx::optional<ElementType> pushImpl(T &&value) noexcept
cxx::optional<ElementType> readBufferAt(const BufferIndex&) noexcept

Protected Attributes

Queue m_freeIndices
Queue m_usedIndices
Buffer<ElementType, Capacity, BufferIndex> m_buffer
std::atomic<uint64_t> m_size = {0u}