PIDUINO
Loading...
Searching...
No Matches
Piduino::TSQueue< T >

Thread-safe queue implementation. More...

#include <tsqueue.h>

Collaboration diagram for Piduino::TSQueue< T >:

Public Member Functions

void push (T item)
 Pushes an item into the queue.
 
pop ()
 Pops an item from the queue.
 
bool empty () const
 Checks if the queue is empty.
 
size_t size () const
 Gets the size of the queue.
 
void clear ()
 Clears the queue.
 

Private Attributes

std::queue< T > m_queue
 
std::mutex m_mutex
 
std::condition_variable m_cond
 

Detailed Description

template<typename T>
class Piduino::TSQueue< T >

Thread-safe queue implementation.

This class provides a thread-safe queue (TSQueue) for storing elements of type T. It uses a std::queue as the underlying container and synchronizes access using std::mutex and std::condition_variable to ensure safe concurrent access from multiple threads.

Template Parameters
TType of elements stored in the queue.

Definition at line 35 of file tsqueue.h.

Member Function Documentation

◆ clear()

template<typename T >
void Piduino::TSQueue< T >::clear ( )
inline

Clears the queue.

Acquires the lock and clears the queue, removing all items.

Definition at line 125 of file tsqueue.h.

References Piduino::TSQueue< T >::m_mutex, and Piduino::TSQueue< T >::m_queue.

◆ empty()

template<typename T >
bool Piduino::TSQueue< T >::empty ( ) const
inline

Checks if the queue is empty.

Acquires the lock and checks if the queue is empty.

Returns
true if the queue is empty, false otherwise.

Definition at line 95 of file tsqueue.h.

References Piduino::TSQueue< T >::m_mutex, and Piduino::TSQueue< T >::m_queue.

◆ pop()

template<typename T >
T Piduino::TSQueue< T >::pop ( )
inline

Pops an item from the queue.

Waits until the queue is not empty, then retrieves and removes the front item.

Returns
The item removed from the front of the queue.

Definition at line 69 of file tsqueue.h.

References Piduino::TSQueue< T >::m_cond, Piduino::TSQueue< T >::m_mutex, and Piduino::TSQueue< T >::m_queue.

◆ push()

template<typename T >
void Piduino::TSQueue< T >::push ( item)
inline

Pushes an item into the queue.

Acquires the lock, adds the item to the queue, and notifies one waiting thread.

Parameters
itemThe item to be added to the queue.

Definition at line 49 of file tsqueue.h.

References Piduino::TSQueue< T >::m_cond, Piduino::TSQueue< T >::m_mutex, and Piduino::TSQueue< T >::m_queue.

◆ size()

template<typename T >
size_t Piduino::TSQueue< T >::size ( ) const
inline

Gets the size of the queue.

Acquires the lock and returns the number of items in the queue.

Returns
The size of the queue.

Definition at line 111 of file tsqueue.h.

References Piduino::TSQueue< T >::m_mutex, and Piduino::TSQueue< T >::m_queue.

Member Data Documentation

◆ m_cond

template<typename T >
std::condition_variable Piduino::TSQueue< T >::m_cond
private

Definition at line 39 of file tsqueue.h.

Referenced by Piduino::TSQueue< T >::pop(), and Piduino::TSQueue< T >::push().

◆ m_mutex

template<typename T >
std::mutex Piduino::TSQueue< T >::m_mutex
mutableprivate

◆ m_queue

template<typename T >
std::queue<T> Piduino::TSQueue< T >::m_queue
private