Main MRPT website > C++ reference
MRPT logo

CThreadSafeQueue.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CThreadSafeQueue_H
00029 #define  CThreadSafeQueue_H
00030 
00031 #include <mrpt/utils/CMessage.h>
00032 #include <mrpt/synch/CCriticalSection.h>
00033 #include <queue>
00034 
00035 namespace mrpt
00036 {
00037         namespace utils
00038         {
00039                 /** A thread-safe template queue for object passing between threads, with objects being passed being "T*".
00040                   * \sa mrpt::utils::CMessageQueue
00041                   */
00042                 template <class T>
00043                 class CThreadSafeQueue
00044                 {
00045                 protected:
00046                         std::queue<T*> m_msgs; //!< The queue of messages. Memory is freed at destructor or by clients gathering messages.
00047                         mrpt::synch::CCriticalSection                   m_csQueue; //!< The critical section
00048                 public:
00049                         CThreadSafeQueue()
00050                         {
00051                         }
00052                         
00053                         virtual ~CThreadSafeQueue()
00054                         {
00055                                 clear();
00056                         }
00057 
00058                         void clear() //!< Clear the queue of messages, freeing memory as required.
00059                         {
00060                                 mrpt::synch::CCriticalSectionLocker locker( &m_csQueue );
00061                                 while (!m_msgs.empty())
00062                                 {
00063                                         delete m_msgs.front();
00064                                         m_msgs.pop();
00065                                 }               
00066                         }
00067 
00068                         void push( T *msg )  //!< 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.
00069                         {
00070                                 mrpt::synch::CCriticalSectionLocker locker( &m_csQueue );
00071                                 m_msgs.push( msg );
00072                         }
00073 
00074                         /** Retrieve the next message in the queue, or NULL if there is no message.
00075                           *  The user MUST call "delete" with the returned object after use.
00076                           */
00077                         T *get( )
00078                         {
00079                                 mrpt::synch::CCriticalSectionLocker locker( &m_csQueue );
00080                                 if (m_msgs.empty())
00081                                         return NULL;
00082                                 else
00083                                 {
00084                                         T *ret = m_msgs.front();
00085                                         m_msgs.pop();
00086                                         return ret;
00087                                 }
00088                         }
00089 
00090                         bool empty() const  //!< Return true if there are no messages.
00091                         {
00092                                 mrpt::synch::CCriticalSectionLocker locker( &m_csQueue );
00093                                 return m_msgs.empty();          
00094                         }
00095 
00096                 }; // End of class def.
00097 
00098         } // End of namespace
00099 } // end of namespace
00100 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011