MRPT  1.9.9
List of all members | Public Member Functions | Protected Attributes
mrpt::containers::CThreadSafeQueue< T > Class Template Reference

Detailed Description

template<class T>
class mrpt::containers::CThreadSafeQueue< T >

A thread-safe template queue for object passing between threads; for a template argument of T, the objects being passed in the queue are "T*".

Usage example:

// Declaration:
CThreadSafeQueue<MyMsgType> tsq;
...
// Thread 1: Write
{
MyMsgType *msg = new MyMsgType;
msg->...
tsq.push(msg); // Insert in the queue
}
// Thread 2: Read
{
MyMsgType *msg = tsq.get();
if (msg)
{
// Process "msg"...
delete msg;
}
}

Note that only dynamically allocated objects can be inserted with push() and that freeing that memory if responsibility of the receiver of this queue as it receives objects with get(). However, elements still in the queue upon destruction will be deleted automatically.

Definition at line 53 of file CThreadSafeQueue.h.

#include <mrpt/containers/CThreadSafeQueue.h>

Inheritance diagram for mrpt::containers::CThreadSafeQueue< T >:

Public Member Functions

 CThreadSafeQueue ()=default
 Default ctor. More...
 
virtual ~CThreadSafeQueue ()
 
void clear ()
 Clear the queue of messages, freeing memory as required. More...
 
void push (T *msg)
 Insert a new message in the queue - The object must be created with "new", and do not delete is after calling this, it must be deleted later. More...
 
T * get ()
 Retrieve the next message in the queue, or nullptr if there is no message. More...
 
T * get_lastest_purge_old ()
 Skip all old messages in the queue and directly return the last one (the most recent, at the bottom of the queue), or nullptr if there is no message. More...
 
bool empty () const
 Return true if there are no messages. More...
 
size_t size () const
 Return the number of queued messages. More...
 

Protected Attributes

std::queue< T * > m_msgs
 The queue of messages. More...
 
std::mutex m_csQueue
 The critical section. More...
 

Constructor & Destructor Documentation

◆ CThreadSafeQueue()

template<class T>
mrpt::containers::CThreadSafeQueue< T >::CThreadSafeQueue ( )
default

Default ctor.

◆ ~CThreadSafeQueue()

template<class T>
virtual mrpt::containers::CThreadSafeQueue< T >::~CThreadSafeQueue ( )
inlinevirtual

Definition at line 65 of file CThreadSafeQueue.h.

Member Function Documentation

◆ clear()

template<class T>
void mrpt::containers::CThreadSafeQueue< T >::clear ( )
inline

Clear the queue of messages, freeing memory as required.

Definition at line 67 of file CThreadSafeQueue.h.

Referenced by mrpt::containers::CThreadSafeQueue< mrpt::serialization::CMessage >::~CThreadSafeQueue().

Here is the caller graph for this function:

◆ empty()

template<class T>
bool mrpt::containers::CThreadSafeQueue< T >::empty ( ) const
inline

Return true if there are no messages.

Definition at line 129 of file CThreadSafeQueue.h.

◆ get()

template<class T>
T* mrpt::containers::CThreadSafeQueue< T >::get ( )
inline

Retrieve the next message in the queue, or nullptr if there is no message.

The user MUST call "delete" with the returned object after use.

Definition at line 90 of file CThreadSafeQueue.h.

Referenced by mrpt::hmtslam::CHMTSLAM::thread_LSLAM().

Here is the caller graph for this function:

◆ get_lastest_purge_old()

template<class T>
T* mrpt::containers::CThreadSafeQueue< T >::get_lastest_purge_old ( )
inline

Skip all old messages in the queue and directly return the last one (the most recent, at the bottom of the queue), or nullptr if there is no message.

Note
The memory of all skipped messages is freed with "delete".
The user MUST call "delete" with the returned object after use.

Definition at line 109 of file CThreadSafeQueue.h.

◆ push()

template<class T>
void mrpt::containers::CThreadSafeQueue< T >::push ( T *  msg)
inline

Insert a new message in the queue - The object must be created with "new", and do not delete is after calling this, it must be deleted later.

Definition at line 80 of file CThreadSafeQueue.h.

◆ size()

template<class T>
size_t mrpt::containers::CThreadSafeQueue< T >::size ( ) const
inline

Return the number of queued messages.

Definition at line 136 of file CThreadSafeQueue.h.

Member Data Documentation

◆ m_csQueue

template<class T>
std::mutex mrpt::containers::CThreadSafeQueue< T >::m_csQueue
mutableprotected

◆ m_msgs

template<class T>
std::queue<T*> mrpt::containers::CThreadSafeQueue< T >::m_msgs
protected



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 5ec5114f2 Thu Feb 20 07:54:08 2020 +0100 at jue feb 20 08:00:10 CET 2020