Skip to content

Class loon::SpscQueue

template <typename T, size_t N>

ClassList > loon > SpscQueue

A lock-free single-producer single-consumer (SPSC) queue with fixed capacity. More...

  • #include <spsc.hpp>

Public Functions

Type Name
size_t capacity () const
Returns the maximum number of elements the queue can hold.
bool empty () const
Checks if the queue is empty.
bool full () const
Checks if the queue is full.
bool pop (T & value)
Pops a value from the front of the queue.
bool push (const T & value)
Pushes a value to the back of the queue.

Detailed Description

SpscQueue provides O(1) push and pop operations without locks, suitable for communication between a single producer thread and a single consumer thread. The capacity is fixed at compile time and the queue will reject new elements when full.

Indices are ever-increasing (never explicitly wrapped), relying on well-defined unsigned integer overflow. Buffer access uses modulo N.

Template parameters:

  • T The element type to store.
  • N The maximum number of elements the queue can hold (must be > 0).

** **

loon::SpscQueue<int, 3> queue;
queue.push(42);
int value;
if (queue.pop(value)) {
    // use value
}

Public Functions Documentation

function capacity

Returns the maximum number of elements the queue can hold.

inline size_t loon::SpscQueue::capacity () const


function empty

Checks if the queue is empty.

inline bool loon::SpscQueue::empty () const


function full

Checks if the queue is full.

inline bool loon::SpscQueue::full () const


function pop

Pops a value from the front of the queue.

inline bool loon::SpscQueue::pop (
    T & value
) 

Parameters:

  • value The value popped from the queue (output).

Returns:

true if a value was popped, false if the queue is empty. This method is safe to call from the consumer thread only.


function push

Pushes a value to the back of the queue.

inline bool loon::SpscQueue::push (
    const T & value
) 

Parameters:

  • value The value to push (copied).

Returns:

true if the value was added, false if the queue is full. This method is safe to call from the producer thread only.



The documentation for this class was generated from the following file include/loon/spsc.hpp