TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::SafeQueue< T > Class Template Reference

A thread-safe queue implementation for storing and retrieving elements of type T. More...

#include <tec_queue.hpp>

Public Member Functions

 SafeQueue (void)
 Constructs an empty thread-safe queue.
 
 ~SafeQueue (void)=default
 Destructor for the SafeQueue.
 
void enqueue (T &&t)
 Adds an element to the back of the queue.
 
T dequeue (void)
 Retrieves and removes the front element from the queue.
 
std::size_t size () const
 Returns the current number of elements in the queue.
 

Detailed Description

template<class T>
class tec::SafeQueue< T >

A thread-safe queue implementation for storing and retrieving elements of type T.

This class provides a thread-safe queue using a mutex and condition variable to ensure safe access and modification in a multi-threaded environment. It supports enqueueing, dequeueing, and size querying operations.

Template Parameters
TThe type of elements stored in the queue.

Constructor & Destructor Documentation

◆ SafeQueue()

template<class T >
tec::SafeQueue< T >::SafeQueue ( void  )
inline

Constructs an empty thread-safe queue.

Initializes an empty queue with a mutex and condition variable for thread-safe operations.

◆ ~SafeQueue()

template<class T >
tec::SafeQueue< T >::~SafeQueue ( void  )
default

Destructor for the SafeQueue.

Cleans up the queue, mutex, and condition variable. No additional cleanup is required as the standard library handles resource management.

Member Function Documentation

◆ dequeue()

template<class T >
T tec::SafeQueue< T >::dequeue ( void  )
inline

Retrieves and removes the front element from the queue.

If the queue is empty, the calling thread waits until an element becomes available. The operation is thread-safe.

Returns
The front element of the queue.

◆ enqueue()

template<class T >
void tec::SafeQueue< T >::enqueue ( T &&  t)
inline

Adds an element to the back of the queue.

Moves the provided element into the queue in a thread-safe manner and notifies one waiting thread that a new element is available.

Parameters
tThe element to add to the queue (moved into the queue).

◆ size()

template<class T >
std::size_t tec::SafeQueue< T >::size ( ) const
inline

Returns the current number of elements in the queue.

Provides a thread-safe way to query the queue's size.

Returns
The number of elements in the queue.

The documentation for this class was generated from the following file: