Template Class TSQueue

Class Documentation

template<typename T>
class TSQueue

This class implements a simplified thread-safe queue allowing concurrent access from multiple threads. This follows the implementation of threadsafe_queue from C++: Concurrency in Action Ch 6 by A. Williams.

Public Functions

TSQueue() = default
inline void push(T data)

Push data to the queue. Thread-safe.

Parameters

data

inline void wait_and_pop(T &data)

Pop element from queue. Thread-safe (blocking).

Parameters

data – Data reference to overwrite with value.

inline std::shared_ptr<T> wait_and_pop()

Pop element from queue. Thread-safe (blocking).

Returns

std::shared_ptr<T> Shared-pointer to popped data.

inline bool try_pop(T &data)

Pop element from queue if available. Thread-safe.

Parameters

data – Data reference to overwrite.

Returns

true Element successfully popped.

Returns

false Element failed to pop.

inline std::shared_ptr<T> try_pop()

Pop element from queue. Thread safe.

Returns

std::shared_ptr<T>

inline bool empty() const

Check if queue is empty.

Returns

true

Returns

false

inline void clear()

Clear the queue of all items using a mutual exclusion lock to handle multiple threads.