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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST