A thread-safe queue implementation. More...
#include <Sync_Queue.hpp>
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_Queue & | operator= (const Sync_Queue &)=delete |
Assignment operator for Sync_Queue. | |
std::optional< value_type > | get () |
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_type & | back () |
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. | |
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.
using Ragot::Sync_Queue< T >::value_type = T |
|
default |
|
inline |
Destroy the Sync_Queue object.
Closes the queue and releases any resources held by it.
|
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.
|
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.
|
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.
|
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.
|
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.
ARGUMENTS | The types of arguments used to construct the element. |
arguments | The arguments used to construct the element. |
|
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.
|
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.
|
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.
|
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.
value | The value to be pushed into the queue. |
|
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.
value | The value to be pushed into the queue. |
|
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.
|
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.
other | The other Sync_Queue to swap with. |
|
private |
Flag to indicate whether the queue is closed, preventing further pushes and allowing threads to exit gracefully when the queue is empty.
|
private |
Condition variable to notify waiting threads when elements are available in the queue or when the queue is closed.
|
mutableprivate |
Mutex to protect access to the queue, ensuring that only one thread can modify the queue at a time.
|
private |
The underlying queue to store elements, used for thread-safe operations.