Taskolib  1.3.3
Public Types | Public Member Functions | Private Attributes | List of all members
task::LockedQueue< MessageT > Class Template Reference

Detailed Description

template<typename MessageT>
class task::LockedQueue< MessageT >

A thread-safe locking message queue.

Messages are added to the end of the queue with push() and taken from the start of the queue with pop(). These calls block if the queue is full or if no messages are available, respectively. try_push() and try_pop() are non-blocking alternatives.

// Set up queue with capacity for 10 integers
LockedQueue<int> queue{ 10 };
// Start sender thread, pushing 100 integers into the queue
std::thread sender([&queue]()
{
for (int i = 1; i <= 100; ++i)
queue.push(i);
});
// Pull all 100 integers out of the queue in the main thread
for (int i = 1; i <= 100; ++i)
{
int val = queue.pop();
assert(val == i);
}
// Join the sender thread
sender.join();

#include <LockedQueue.h>

Inheritance diagram for task::LockedQueue< MessageT >:
Inheritance graph

Public Types

using MessageType = MessageT
 
using message_type = MessageT
 
using SizeType = std::uint32_t
 
using size_type = SizeType
 

Public Member Functions

 LockedQueue (SizeType capacity)
 Construct a queue that is able to hold a given maximum number of entries. More...
 
SizeType capacity () const
 Return the maximal number of entries in the queue. More...
 
bool empty () const
 Determine whether the queue is empty. More...
 
MessageType pop ()
 Remove a message from the front of the queue and return it. More...
 
MessageType back () const
 Fetch the last message pushed to the queue and returns a copy of it. More...
 
template<typename MsgT , std::enable_if_t< std::is_convertible_v< MsgT, MessageType >, bool > = true>
void push (MsgT &&msg)
 Insert a message at the end of the queue. More...
 
SizeType size () const
 Return the number of messages in the queue. More...
 
gul14::optional< MessageTypetry_pop ()
 Remove a message from the front of the queue and return it. More...
 
template<typename MsgT , std::enable_if_t< std::is_convertible_v< MsgT, MessageType >, bool > = true>
bool try_push (MsgT &&msg)
 Try to insert a message at the end of the queue. More...
 

Private Attributes

std::mutex mutex_
 Mutex protecting all member variables. More...
 
std::condition_variable cv_message_available_
 Condition variable, triggered when at least one message has been added to the queue. More...
 
std::condition_variable cv_slot_available_
 Condition variable, triggered when at least one slot in the queue has been freed. More...
 
gul14::SlidingBuffer< MessageTypequeue_
 

Member Typedef Documentation

◆ message_type

template<typename MessageT >
using task::LockedQueue< MessageT >::message_type = MessageT

◆ MessageType

template<typename MessageT >
using task::LockedQueue< MessageT >::MessageType = MessageT

◆ size_type

template<typename MessageT >
using task::LockedQueue< MessageT >::size_type = SizeType

◆ SizeType

template<typename MessageT >
using task::LockedQueue< MessageT >::SizeType = std::uint32_t

Constructor & Destructor Documentation

◆ LockedQueue()

template<typename MessageT >
task::LockedQueue< MessageT >::LockedQueue ( SizeType  capacity)
inline

Construct a queue that is able to hold a given maximum number of entries.

Member Function Documentation

◆ back()

template<typename MessageT >
MessageType task::LockedQueue< MessageT >::back ( ) const
inline

Fetch the last message pushed to the queue and returns a copy of it.

It will not be removed from the queue.

This call blocks until a message is available.

References task::LockedQueue< MessageT >::cv_message_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ capacity()

template<typename MessageT >
SizeType task::LockedQueue< MessageT >::capacity ( ) const
inline

Return the maximal number of entries in the queue.

References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ empty()

template<typename MessageT >
bool task::LockedQueue< MessageT >::empty ( ) const
inline

Determine whether the queue is empty.

References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ pop()

template<typename MessageT >
MessageType task::LockedQueue< MessageT >::pop ( )
inline

Remove a message from the front of the queue and return it.

This call blocks until a message is available.

See also
try_pop()

References task::LockedQueue< MessageT >::cv_message_available_, task::LockedQueue< MessageT >::cv_slot_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ push()

template<typename MessageT >
template<typename MsgT , std::enable_if_t< std::is_convertible_v< MsgT, MessageType >, bool > = true>
void task::LockedQueue< MessageT >::push ( MsgT &&  msg)
inline

Insert a message at the end of the queue.

This call blocks until the queue has a free slot for the message.

References task::LockedQueue< MessageT >::cv_message_available_, task::LockedQueue< MessageT >::cv_slot_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ size()

template<typename MessageT >
SizeType task::LockedQueue< MessageT >::size ( ) const
inline

Return the number of messages in the queue.

References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ try_pop()

template<typename MessageT >
gul14::optional<MessageType> task::LockedQueue< MessageT >::try_pop ( )
inline

Remove a message from the front of the queue and return it.

This call blocks until a message is available.

See also
try_pop()

References task::LockedQueue< MessageT >::cv_slot_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

◆ try_push()

template<typename MessageT >
template<typename MsgT , std::enable_if_t< std::is_convertible_v< MsgT, MessageType >, bool > = true>
bool task::LockedQueue< MessageT >::try_push ( MsgT &&  msg)
inline

Try to insert a message at the end of the queue.

This call returns true if the message was successfully enqueued or false if the queue temporarily had no space to store the message.

Messages given as an rvalue are only moved from if they can actually be inserted into the queue.

References task::LockedQueue< MessageT >::cv_message_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.

Member Data Documentation

◆ cv_message_available_

template<typename MessageT >
std::condition_variable task::LockedQueue< MessageT >::cv_message_available_
mutableprivate

Condition variable, triggered when at least one message has been added to the queue.

Referenced by task::LockedQueue< MessageT >::back(), task::LockedQueue< MessageT >::pop(), task::LockedQueue< MessageT >::push(), and task::LockedQueue< MessageT >::try_push().

◆ cv_slot_available_

template<typename MessageT >
std::condition_variable task::LockedQueue< MessageT >::cv_slot_available_
mutableprivate

Condition variable, triggered when at least one slot in the queue has been freed.

Referenced by task::LockedQueue< MessageT >::pop(), task::LockedQueue< MessageT >::push(), and task::LockedQueue< MessageT >::try_pop().

◆ mutex_

template<typename MessageT >
std::mutex task::LockedQueue< MessageT >::mutex_
mutableprivate

◆ queue_

template<typename MessageT >
gul14::SlidingBuffer<MessageType> task::LockedQueue< MessageT >::queue_
private

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