Main MRPT website > C++ reference
MRPT logo
CThreadSafeQueue.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CThreadSafeQueue_H
29 #define CThreadSafeQueue_H
30 
31 #include <mrpt/utils/CMessage.h>
33 #include <queue>
34 
35 namespace mrpt
36 {
37  namespace utils
38  {
39  /** 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*".
40  *
41  * Usage example:
42  *
43  * \code
44  * // Declaration:
45  * CThreadSafeQueue<MyMsgType> tsq;
46  * ...
47  *
48  * // Thread 1: Write
49  * {
50  * MyMsgType *msg = new MyMsgType;
51  * msg->...
52  * tsq.push(msg); // Insert in the queue
53  * }
54  *
55  * // Thread 2: Read
56  * {
57  * MyMsgType *msg = tsq.get();
58  * if (msg)
59  * {
60  * // Process "msg"...
61  * delete msg;
62  * }
63  * }
64  * \endcode
65  *
66  * Note that only dynamically allocated objects can be inserted with \a push() and that freeing that memory
67  * if responsibility of the receiver of this queue as it receives objects with \a get(). However, elements
68  * still in the queue upon destruction will be deleted automatically.
69  *
70  * \sa mrpt::utils::CMessageQueue
71  * \ingroup mrpt_base_grp
72  */
73  template <class T>
75  {
76  protected:
77  std::queue<T*> m_msgs; //!< The queue of messages. Memory is freed at destructor or by clients gathering messages.
78  mrpt::synch::CCriticalSection m_csQueue; //!< The critical section
79  public:
80  /** Default ctor. */
82 
84  {
85  clear();
86  }
87 
88  /** Clear the queue of messages, freeing memory as required. */
89  void clear()
90  {
92  while (!m_msgs.empty())
93  {
94  delete m_msgs.front();
95  m_msgs.pop();
96  }
97  }
98 
99  /** 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.
100  */
101  inline void push( T *msg )
102  {
104  m_msgs.push( msg );
105  }
106 
107  /** Retrieve the next message in the queue, or NULL if there is no message.
108  * The user MUST call "delete" with the returned object after use.
109  */
110  inline T *get( )
111  {
113  if (m_msgs.empty())
114  return NULL;
115  else
116  {
117  T *ret = m_msgs.front();
118  m_msgs.pop();
119  return ret;
120  }
121  }
122 
123  /** 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.
124  * \note The memory of all skipped messages is freed with "delete".
125  * \note The user MUST call "delete" with the returned object after use.
126  */
128  {
130  if (m_msgs.empty())
131  return NULL;
132  else
133  {
134  for (;;)
135  {
136  T *ret = m_msgs.front();
137  m_msgs.pop();
138  if (m_msgs.empty()) return ret;
139  else delete ret;
140  }
141  }
142  }
143 
144  /** Return true if there are no messages. */
145  bool empty() const
146  {
148  return m_msgs.empty();
149  }
150 
151  /** Return the number of queued messages. */
152  size_t size() const
153  {
155  return m_msgs.size();
156  }
157 
158  }; // End of class def.
159 
160  } // End of namespace
161 } // end of namespace
162 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013