3.3.2.29. Template Class ConcurrentQueue

3.3.2.29.1. Class Documentation

template <typename T>
class ConcurrentQueue

Concurrent queue.

ConcurrentQueue is a basic thread safe implementation of a queue. ConcurrentQueue has a limited size. Push and Pop methods can be used in a multi-threaded context.

This implementation is inspired by https://juanchopanzacpp.wordpress.com/2013/02/26/concurrent-queue-c11

Warning
Push and Pop methods may block.

Public Functions

ConcurrentQueue(std::size_t max_queue_size = 10)

Instanciate a concurrent queue with a maximum size.

The queue is active after instantiation

Parameters
  • max_queue_size: limit the queue size

~ConcurrentQueue()
ConcurrentQueue(const ConcurrentQueue&)
ConcurrentQueue &operator=(const ConcurrentQueue&)
ConcurrentQueue(ConcurrentQueue&&)
ConcurrentQueue &operator=(ConcurrentQueue&&)
void Push(T &&element, std::error_code &ec)

Push an element in the queue.

Warning
This method may block until there is an available spot
Warning
If the queue is not active, the element will be dropped and ec is set
Parameters
  • element: to push to the queue
  • ec: return status

T Pop(std::error_code &ec)

Pop an element from the queue.

Return
An available element from the queue
Warning
This method may block until there is an element to pop
Warning
If no element is available and the queue is not active, this method returns a default constructed element and ec is set
Parameters
  • ec: return status

std::size_t Size()

Get queue size.

Return
Queue size

bool Empty()

Queue is empty.

Return
true if queue is empty

bool CanPop()

Elements can be popped from the queue.

Return
boolean

void Activate()

Activate the queue.

The queue will be able to receive new elements

void Deactivate()

Deactivate the queue.

The queue will not be able to receive new elements

void DeactivateAndClear()

Deactivate the queue and clear its content.

The queue will not be able to receive new elements and its content will be erased

bool IsActive()

Can the queue be filled with new elements.

Return
true if the queue is still active