A minimal thread-safe FIFO queue for producer/consumer patterns. Supports blocking pop() and non-blocking try_pop() .

#include “/include/tsqueue.hpp”

Template Paramaters


Public Member Functions

void push(T v);

Enqueue an element and wake one waiting consumer.

T pop();

Block until an element is available; remove and return it.

bool try_pop(T& out);

Non-blocking pop; move front into out if available and return true, else false.


Description

TSQueue<T> provides a simple, efficient, multi-producer / multi-consumer queue.

It uses std::mutex and std::condition_variable to protect a std::queue<T>.

Note: The class does not provide close(); threads blocked in pop() will not exit by themselves. Therefore it is required that threads check their own bool running_ and that they are shut down by setting running_ = false and pushing a dummy value into the queue. (See example below.)