Main MRPT website > C++ reference for MRPT 1.9.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>
13 
14 #include <mutex>
15 #include <queue>
16 
17 namespace mrpt
18 {
19 namespace utils
20 {
21 /** A thread-safe template queue for object passing between threads; for a
22  * template argument of T, the objects being passed in the queue are "T*".
23  *
24  * Usage example:
25  *
26  * \code
27  * // Declaration:
28  * CThreadSafeQueue<MyMsgType> tsq;
29  * ...
30  *
31  * // Thread 1: Write
32  * {
33  * MyMsgType *msg = new MyMsgType;
34  * msg->...
35  * tsq.push(msg); // Insert in the queue
36  * }
37  *
38  * // Thread 2: Read
39  * {
40  * MyMsgType *msg = tsq.get();
41  * if (msg)
42  * {
43  * // Process "msg"...
44  * delete msg;
45  * }
46  * }
47  * \endcode
48  *
49  * Note that only dynamically allocated objects can be inserted with \a push()
50  * and that freeing that memory
51  * if responsibility of the receiver of this queue as it receives objects
52  * with \a get(). However, elements
53  * still in the queue upon destruction will be deleted automatically.
54  *
55  * \sa mrpt::utils::CMessageQueue
56  * \ingroup mrpt_base_grp
57  */
58 template <class T>
60 {
61  protected:
62  /** The queue of messages. Memory is freed at destructor or by clients
63  * gathering messages. */
64  std::queue<T*> m_msgs;
65  /** The critical section */
66  mutable std::mutex m_csQueue;
67 
68  public:
69  /** Default ctor. */
71  virtual ~CThreadSafeQueue() { clear(); }
72  /** Clear the queue of messages, freeing memory as required. */
73  void clear()
74  {
75  std::lock_guard<std::mutex> lock(m_csQueue);
76  while (!m_msgs.empty())
77  {
78  delete m_msgs.front();
79  m_msgs.pop();
80  }
81  }
82 
83  /** Insert a new message in the queue - The object must be created with
84  * "new", and do not delete is after calling this, it must be deleted later.
85  */
86  inline void push(T* msg)
87  {
88  std::lock_guard<std::mutex> lock(m_csQueue);
89  m_msgs.push(msg);
90  }
91 
92  /** Retrieve the next message in the queue, or nullptr if there is no
93  * message.
94  * The user MUST call "delete" with the returned object after use.
95  */
96  inline T* get()
97  {
98  std::lock_guard<std::mutex> lock(m_csQueue);
99  if (m_msgs.empty())
100  return nullptr;
101  else
102  {
103  T* ret = m_msgs.front();
104  m_msgs.pop();
105  return ret;
106  }
107  }
108 
109  /** Skip all old messages in the queue and directly return the last one (the
110  * most recent, at the bottom of the queue), or nullptr if there is no
111  * message.
112  * \note The memory of all skipped messages is freed with "delete".
113  * \note The user MUST call "delete" with the returned object after use.
114  */
116  {
117  std::lock_guard<std::mutex> lock(m_csQueue);
118  if (m_msgs.empty())
119  return nullptr;
120  else
121  {
122  for (;;)
123  {
124  T* ret = m_msgs.front();
125  m_msgs.pop();
126  if (m_msgs.empty())
127  return ret;
128  else
129  delete ret;
130  }
131  }
132  }
133 
134  /** Return true if there are no messages. */
135  bool empty() const
136  {
137  std::lock_guard<std::mutex> lock(m_csQueue);
138  return m_msgs.empty();
139  }
140 
141  /** Return the number of queued messages. */
142  size_t size() const
143  {
144  std::lock_guard<std::mutex> lock(m_csQueue);
145  return m_msgs.size();
146  }
147 
148 }; // End of class def.
149 
150 } // End of namespace
151 } // end of namespace
152 #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 nullptr if there is no message.
std::queue< T * > m_msgs
The queue of messages.
std::mutex m_csQueue
The critical section.
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.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019