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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020