3D Graphics Engine for ESP32
 
Loading...
Searching...
No Matches
Ragot::Sync_Queue< T > Class Template Reference

A thread-safe queue implementation. More...

#include <Sync_Queue.hpp>

Inheritance diagram for Ragot::Sync_Queue< T >:
Inheritance graph
Collaboration diagram for Ragot::Sync_Queue< T >:
Collaboration graph

Public Types

using value_type = T
 

Public Member Functions

 Sync_Queue ()=default
 Construct a new Sync_Queue object.
 
 ~Sync_Queue ()
 Destroy the Sync_Queue object.
 
 Sync_Queue (const Sync_Queue &)=delete
 Construct a new Sync_Queue object.
 
Sync_Queueoperator= (const Sync_Queue &)=delete
 Assignment operator for Sync_Queue.
 
std::optional< value_typeget ()
 Retrieves an element from the queue.
 
void push (const value_type &value)
 Pushes an element into the queue.
 
template<typename ... ARGUMENTS>
void push (ARGUMENTS &&...arguments)
 Pushes an element into the queue.
 
template<typename ... ARGUMENTS>
void emplace (ARGUMENTS &&...arguments)
 Emplaces an element into the queue.
 
value_typeback ()
 Retrieves the front element of the queue without removing it.
 
void close ()
 Closes the queue.
 
void clear ()
 Clears the queue.
 
void swap (Sync_Queue &other)
 Swaps the contents of this queue with another queue.
 
bool empty () const
 Checks if the queue is empty.
 
size_t size () const
 Gets the size of the queue.
 

Private Attributes

std::queue< T > queue
 The underlying queue to store elements, used for thread-safe operations.
 
std::mutex mutex
 Mutex to protect access to the queue, ensuring that only one thread can modify the queue at a time.
 
std::condition_variable condition
 Condition variable to notify waiting threads when elements are available in the queue or when the queue is closed.
 
bool closed = false
 Flag to indicate whether the queue is closed, preventing further pushes and allowing threads to exit gracefully when the queue is empty.
 

Detailed Description

template<typename T>
class Ragot::Sync_Queue< T >

A thread-safe queue implementation.

This class provides a synchronized queue that allows multiple threads to safely push and pop elements. It uses mutexes and condition variables to ensure thread safety.

Member Typedef Documentation

◆ value_type

template<typename T>
using Ragot::Sync_Queue< T >::value_type = T

Constructor & Destructor Documentation

◆ Sync_Queue() [1/2]

template<typename T>
Ragot::Sync_Queue< T >::Sync_Queue ( )
default

Construct a new Sync_Queue object.

Here is the caller graph for this function:

◆ ~Sync_Queue()

template<typename T>
Ragot::Sync_Queue< T >::~Sync_Queue ( )
inline

Destroy the Sync_Queue object.

Closes the queue and releases any resources held by it.

Here is the call graph for this function:

◆ Sync_Queue() [2/2]

template<typename T>
Ragot::Sync_Queue< T >::Sync_Queue ( const Sync_Queue< T > & )
delete

Construct a new Sync_Queue object.

This constructor is deleted to prevent copying of the Sync_Queue object. It ensures that the queue cannot be copied, which is important for thread safety.

Here is the call graph for this function:

Member Function Documentation

◆ back()

template<typename T>
value_type & Ragot::Sync_Queue< T >::back ( )
inline

Retrieves the front element of the queue without removing it.

This method returns a reference to the front element of the queue. It is not thread-safe and should be used with caution.

Returns
value_type& A reference to the front element of the queue.

◆ clear()

template<typename T>
void Ragot::Sync_Queue< T >::clear ( )
inline

Clears the queue.

This method removes all elements from the queue and resets it to an empty state. It is thread-safe and can be called while other threads are accessing the queue.

Here is the call graph for this function:

◆ close()

template<typename T>
void Ragot::Sync_Queue< T >::close ( )
inline

Closes the queue.

This method sets the closed flag to true and notifies all waiting threads. After closing, no more elements can be pushed into the queue.

Here is the caller graph for this function:

◆ emplace()

template<typename T>
template<typename ... ARGUMENTS>
void Ragot::Sync_Queue< T >::emplace ( ARGUMENTS &&... arguments)
inline

Emplaces an element into the queue.

This method constructs an element in place and adds it to the queue if it is not closed. It notifies one waiting thread that an element is available.

Template Parameters
ARGUMENTSThe types of arguments used to construct the element.
Parameters
argumentsThe arguments used to construct the element.

◆ empty()

template<typename T>
bool Ragot::Sync_Queue< T >::empty ( ) const
inline

Checks if the queue is empty.

This method checks if the queue is empty by acquiring a lock on the mutex. It returns true if the queue is empty, false otherwise.

Returns
true if the queue is empty, false otherwise.
Here is the caller graph for this function:

◆ get()

template<typename T>
std::optional< value_type > Ragot::Sync_Queue< T >::get ( )
inline

Retrieves an element from the queue.

This method blocks until an element is available or the queue is closed. If the queue is closed and empty, it returns std::nullopt.

Returns
std::optional<value_type> The retrieved element or std::nullopt if the queue is closed and empty.

◆ operator=()

template<typename T>
Sync_Queue & Ragot::Sync_Queue< T >::operator= ( const Sync_Queue< T > & )
delete

Assignment operator for Sync_Queue.

This assignment operator is deleted to prevent copying of the Sync_Queue object. It ensures that the queue cannot be assigned, which is important for thread safety.

Here is the call graph for this function:

◆ push() [1/2]

template<typename T>
template<typename ... ARGUMENTS>
void Ragot::Sync_Queue< T >::push ( ARGUMENTS &&... arguments)
inline

Pushes an element into the queue.

This method adds an element to the queue if it is not closed. It notifies one waiting thread that an element is available.

Parameters
valueThe value to be pushed into the queue.

◆ push() [2/2]

template<typename T>
void Ragot::Sync_Queue< T >::push ( const value_type & value)
inline

Pushes an element into the queue.

This method adds an element to the queue if it is not closed. It notifies one waiting thread that an element is available.

Parameters
valueThe value to be pushed into the queue.

◆ size()

template<typename T>
size_t Ragot::Sync_Queue< T >::size ( ) const
inline

Gets the size of the queue.

This method returns the number of elements in the queue by acquiring a lock on the mutex. It is thread-safe and can be called while other threads are accessing the queue.

Returns
size_t The number of elements in the queue.

◆ swap()

template<typename T>
void Ragot::Sync_Queue< T >::swap ( Sync_Queue< T > & other)
inline

Swaps the contents of this queue with another queue.

This method swaps the contents of this queue with another Sync_Queue. It locks both mutexes to ensure thread safety during the swap operation.

Parameters
otherThe other Sync_Queue to swap with.
Here is the call graph for this function:

Member Data Documentation

◆ closed

template<typename T>
bool Ragot::Sync_Queue< T >::closed = false
private

Flag to indicate whether the queue is closed, preventing further pushes and allowing threads to exit gracefully when the queue is empty.

◆ condition

template<typename T>
std::condition_variable Ragot::Sync_Queue< T >::condition
private

Condition variable to notify waiting threads when elements are available in the queue or when the queue is closed.

◆ mutex

template<typename T>
std::mutex Ragot::Sync_Queue< T >::mutex
mutableprivate

Mutex to protect access to the queue, ensuring that only one thread can modify the queue at a time.

◆ queue

template<typename T>
std::queue<T> Ragot::Sync_Queue< T >::queue
private

The underlying queue to store elements, used for thread-safe operations.


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