Taskolib
1.3.3
|
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.
#include <LockedQueue.h>
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< MessageType > | try_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< MessageType > | queue_ |
using task::LockedQueue< MessageT >::message_type = MessageT |
using task::LockedQueue< MessageT >::MessageType = MessageT |
using task::LockedQueue< MessageT >::size_type = SizeType |
using task::LockedQueue< MessageT >::SizeType = std::uint32_t |
|
inline |
Construct a queue that is able to hold a given maximum number of entries.
|
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_.
|
inline |
Return the maximal number of entries in the queue.
References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.
|
inline |
Determine whether the queue is empty.
References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.
|
inline |
Remove a message from the front of the queue and return it.
This call blocks until a message is available.
References task::LockedQueue< MessageT >::cv_message_available_, task::LockedQueue< MessageT >::cv_slot_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.
|
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_.
|
inline |
Return the number of messages in the queue.
References task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.
|
inline |
Remove a message from the front of the queue and return it.
This call blocks until a message is available.
References task::LockedQueue< MessageT >::cv_slot_available_, task::LockedQueue< MessageT >::mutex_, and task::LockedQueue< MessageT >::queue_.
|
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_.
|
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().
|
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().
|
mutableprivate |
Mutex protecting all member variables.
Referenced by task::LockedQueue< MessageT >::back(), task::LockedQueue< MessageT >::capacity(), task::LockedQueue< MessageT >::empty(), task::LockedQueue< MessageT >::pop(), task::LockedQueue< MessageT >::push(), task::LockedQueue< MessageT >::size(), task::LockedQueue< MessageT >::try_pop(), and task::LockedQueue< MessageT >::try_push().
|
private |
Referenced by task::LockedQueue< MessageT >::back(), task::LockedQueue< MessageT >::capacity(), task::LockedQueue< MessageT >::empty(), task::LockedQueue< MessageT >::pop(), task::LockedQueue< MessageT >::push(), task::LockedQueue< MessageT >::size(), task::LockedQueue< MessageT >::try_pop(), and task::LockedQueue< MessageT >::try_push().