A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

queue_spsc_atomic

A fixed capacity single-producer, single-consumer queue for multi-threaded and interrupt driven systems.
The queue may be used to transfer data to or from two threads or an ISR.

The queue makes use of etl::atomic_size_t

etl::queue_spsc_atomic<typename T, const size_t SIZE>

Inherits from etl::iqueue_spsc_atomic<T>
etl::iqueue_spsc_atomic may be used as a size independent pointer or reference type for any
etl::queue_spsc_atomic instance of the same implementation.

Member types

value_type      T
size_type       std::size_t
pointer         value_type*
const_pointer   const value_type*
reference       value_type&
const_reference const value_type&

Constructor

queue_spsc_atomic();

Capacity

bool empty() const
Returns true if the size of the queue is zero, otherwise false.
Accurate from the 'pop' thread.
'Not empty' is a guess from the 'push' thread.

bool full() const
Returns true if the size of the queue is SIZE, otherwise false.
Accurate from the 'push' thread.
'Not full' is a guess from the 'pop' thread.

size_t size() const
Returns the size of the queue.
Due to concurrency, this is a guess.

size_t available() const
Returns the remaining available capacity in the queue.
Due to concurrency, this is a guess.

ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the queue.

ETL_CONSTEXPR size_t capacity() const
Returns the maximum possible size of the queue.

Modifiers

bool push(parameter_t value);
Pushes a value to the back of the queue.
Returns true if successful, otherwise false.

bool pop();
Pop a value from the front of the list.
Returns true if successful, otherwise false.

void pop(T& value);
Pop a value from the front of the list and place it in value.
Returns true if successful, otherwise false.

void clear();
Clears the queue to a size of zero.
Must be called from thread that pops the queue or when there is no  possibility of concurrent access.

Notes

Remember that thread context switches may occur between calls to the access protected functions. For example, a call to
empty() may return true, but a subsequent call to pop() may succeed if a context switch occurred between the two and
pushed a new value.

Example


etl::queue_spsc_atomic<char, 10> queue;

int main()
{
  while (true)
  {
    char c;

    if (queue.pop(c))
    {
      Print(c);
    }
  }
}

void Thread()
{
  while (true)
  {
    queue.push('A');
  }
}
queue_spsc_atomic.h