Main MRPT website > C++ reference for MRPT 1.5.9
CThreadSafeQueue.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CThreadSafeQueue_H
10 #define CThreadSafeQueue_H
11 
12 #include <mrpt/utils/CMessage.h>
14 #include <queue>
15 
16 namespace mrpt
17 {
18  namespace utils
19  {
20  /** 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*".
21  *
22  * Usage example:
23  *
24  * \code
25  * // Declaration:
26  * CThreadSafeQueue<MyMsgType> tsq;
27  * ...
28  *
29  * // Thread 1: Write
30  * {
31  * MyMsgType *msg = new MyMsgType;
32  * msg->...
33  * tsq.push(msg); // Insert in the queue
34  * }
35  *
36  * // Thread 2: Read
37  * {
38  * MyMsgType *msg = tsq.get();
39  * if (msg)
40  * {
41  * // Process "msg"...
42  * delete msg;
43  * }
44  * }
45  * \endcode
46  *
47  * Note that only dynamically allocated objects can be inserted with \a push() and that freeing that memory
48  * if responsibility of the receiver of this queue as it receives objects with \a get(). However, elements
49  * still in the queue upon destruction will be deleted automatically.
50  *
51  * \sa mrpt::utils::CMessageQueue
52  * \ingroup mrpt_base_grp
53  */
54  template <class T>
56  {
57  protected:
58  std::queue<T*> m_msgs; //!< The queue of messages. Memory is freed at destructor or by clients gathering messages.
59  mrpt::synch::CCriticalSection m_csQueue; //!< The critical section
60  public:
61  /** Default ctor. */
63 
65  {
66  clear();
67  }
68 
69  /** Clear the queue of messages, freeing memory as required. */
70  void clear()
71  {
73  while (!m_msgs.empty())
74  {
75  delete m_msgs.front();
76  m_msgs.pop();
77  }
78  }
79 
80  /** 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.
81  */
82  inline void push( T *msg )
83  {
85  m_msgs.push( msg );
86  }
87 
88  /** Retrieve the next message in the queue, or NULL if there is no message.
89  * The user MUST call "delete" with the returned object after use.
90  */
91  inline T *get( )
92  {
94  if (m_msgs.empty())
95  return NULL;
96  else
97  {
98  T *ret = m_msgs.front();
99  m_msgs.pop();
100  return ret;
101  }
102  }
103 
104  /** Skip all old messages in the queue and directly return the last one (the most recent, at the bottom of the queue), or NULL if there is no message.
105  * \note The memory of all skipped messages is freed with "delete".
106  * \note The user MUST call "delete" with the returned object after use.
107  */
109  {
111  if (m_msgs.empty())
112  return NULL;
113  else
114  {
115  for (;;)
116  {
117  T *ret = m_msgs.front();
118  m_msgs.pop();
119  if (m_msgs.empty()) return ret;
120  else delete ret;
121  }
122  }
123  }
124 
125  /** Return true if there are no messages. */
126  bool empty() const
127  {
129  return m_msgs.empty();
130  }
131 
132  /** Return the number of queued messages. */
133  size_t size() const
134  {
136  return m_msgs.size();
137  }
138 
139  }; // End of class def.
140 
141  } // End of namespace
142 } // end of namespace
143 #endif
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 NULL if there is no message.
This class provides simple critical sections functionality.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
mrpt::synch::CCriticalSection m_csQueue
The critical section.
std::queue< T * > m_msgs
The queue of messages. Memory is freed at destructor or by clients gathering messages.
void push(T *msg)
Insert a new message in the queue - The object must be created with "new", and do not delete is after...
size_t size() const
Return the number of queued messages.
A thread-safe template queue for object passing between threads; for a template argument of T...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void clear()
Clear the queue of messages, freeing memory as required.
bool empty() const
Return true if there are no messages.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020