TeiaCareSDK  v0.1.0
TeiaCareSDK is a collection of reusable C++ components
Loading...
Searching...
No Matches
tc::sdk::blocking_queue< T > Class Template Reference

Thread safe, blocking queue. More...

#include <blocking_queue.hpp>

Inheritance diagram for tc::sdk::blocking_queue< T >:
Collaboration diagram for tc::sdk::blocking_queue< T >:

Public Member Functions

 blocking_queue (size_t capacity)
 Constructor.
 
 ~blocking_queue ()=default
 Destructor.
 
void push (const T &item)
 Insert an item into the queue.
 
void push (T &&item)
 Insert an item into the queue.
 
bool try_push (const T &item)
 Try to insert an item into the queue.
 
bool try_push (T &&item)
 Try to insert an item into the queue.
 
pop ()
 Retrieve an item from the queue.
 
std::optional< T > try_pop ()
 Try to retrieve an item from the queue.
 
size_t size () const
 Get the number of items currently in the queue.
 
size_t capacity () const
 Get the maximum number of items that the queue can hold.
 

Detailed Description

template<typename T>
class tc::sdk::blocking_queue< T >

Thread safe, blocking queue.

Template Parameters
TQueue items type

The queue has a fixed capacity (i.e. maximum number of items that can be hold). When the queue is full and a new item is needs to be inserted via blocking_queue::push() the queue blocks until an item is popped. Viceversa, when the queue is empty and an item is required via blocking_queue::pop(), the queue blocks until the first item is pushed.

Examples
example_blocking_queue.cpp, example_geometry_line.cpp, example_geometry_point.cpp, example_geometry_rectangle.cpp, example_geometry_size.cpp, and example_observable.cpp.

Definition at line 38 of file blocking_queue.hpp.

Constructor & Destructor Documentation

◆ blocking_queue()

template<typename T >
tc::sdk::blocking_queue< T >::blocking_queue ( size_t  capacity)
inlineexplicit

Constructor.

Parameters
capacityset the maximum number of items the queue can hold

Creates a tc::sdk::blocking_queue instance.

Definition at line 47 of file blocking_queue.hpp.

◆ ~blocking_queue()

template<typename T >
tc::sdk::blocking_queue< T >::~blocking_queue ( )
default

Destructor.

Destructs this.

Member Function Documentation

◆ push() [1/2]

template<typename T >
void tc::sdk::blocking_queue< T >::push ( const T &  item)
inline

Insert an item into the queue.

Template Parameters
itemThe item to be inserted

If this method is called when the queue is full the calling thread is blocked until an item is popped from the queue.

Definition at line 65 of file blocking_queue.hpp.

◆ push() [2/2]

template<typename T >
void tc::sdk::blocking_queue< T >::push ( T &&  item)
inline

Insert an item into the queue.

Template Parameters
itemThe item to be inserted

If this method is called when the queue is full the calling thread is blocked until an item is popped from the queue. This is an overload of blocking_queue::push(const T&) which emplaces the item instead of using a const ref.

Definition at line 82 of file blocking_queue.hpp.

◆ try_push() [1/2]

template<typename T >
bool tc::sdk::blocking_queue< T >::try_push ( const T &  item)
inline

Try to insert an item into the queue.

Template Parameters
itemThe item to be inserted
Returns
true if the item was inserted

If this method is called when the queue is full the calling thread is not blocked and false is returned. Otherwise the thread is locked until the item is inserted, then true is returned.

Definition at line 100 of file blocking_queue.hpp.

◆ try_push() [2/2]

template<typename T >
bool tc::sdk::blocking_queue< T >::try_push ( T &&  item)
inline

Try to insert an item into the queue.

Template Parameters
itemThe item to be inserted
Returns
true if the item was inserted

If this method is called when the queue is full the calling thread is not blocked and false is returned. Otherwise the thread is locked until the item is inserted, then true is returned. This is an overload of blocking_queue::try_push(const T&) which emplaces the item instead of using a const ref.

Definition at line 121 of file blocking_queue.hpp.

◆ pop()

template<typename T >
T tc::sdk::blocking_queue< T >::pop ( )
inline

Retrieve an item from the queue.

Returns
T value

If this method is called when the queue is full the calling thread is blocked until an item is pushed in the queue.

Examples
example_blocking_queue.cpp.

Definition at line 139 of file blocking_queue.hpp.

◆ try_pop()

template<typename T >
std::optional< T > tc::sdk::blocking_queue< T >::try_pop ( )
inline

Try to retrieve an item from the queue.

Returns
std::optional<T> value

If this method is called when the queue is empty the calling thread is not blocked and std::nullopt is returned. Otherwise the thread is locked until the item is removed, then std::optional<T> is returned.

Definition at line 159 of file blocking_queue.hpp.

◆ size()

template<typename T >
size_t tc::sdk::blocking_queue< T >::size ( ) const
inline

Get the number of items currently in the queue.

Returns
queue size
Examples
example_blocking_queue.cpp.

Definition at line 176 of file blocking_queue.hpp.

◆ capacity()

template<typename T >
size_t tc::sdk::blocking_queue< T >::capacity ( ) const
inline

Get the maximum number of items that the queue can hold.

Returns
queue capacity

Definition at line 186 of file blocking_queue.hpp.


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