Main MRPT website > C++ reference
MRPT logo
CThreadSafeVariable.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
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  mrpt_synch_threadsafevar_H
00029 #define  mrpt_synch_threadsafevar_H
00030 
00031 #include <mrpt/synch/CCriticalSection.h>
00032 
00033 namespace mrpt
00034 {
00035 namespace synch
00036 {
00037 
00038         /** A template for created thread-safe variables with an internal critical section controlled each read or write.
00039           * Example:
00040           *  \code
00041           *    CThreadSafeVariable<double>   var1;
00042           *        ...
00043           *    var.set(2.3);    // Sets the value
00044           *    double x = var.get();  // Reads the variable
00045           *    ...
00046           *    double foo = var;  // Also reads the variable
00047           *    var = 2.3;       // ERROR: Not allowed, use ".set()" instead.
00048           *  \endcode
00049           *
00050           * \sa CCriticalSection
00051           * \ingroup synch_grp
00052           */
00053         template <typename T>
00054         class CThreadSafeVariable
00055         {
00056         private:
00057                 CCriticalSection  m_cs;
00058                 T       m_val;
00059         public:
00060                 CThreadSafeVariable() : m_cs(), m_val()  {  }
00061                 CThreadSafeVariable(const T& init_val) : m_cs(), m_val(init_val)  {  }
00062 
00063                 virtual ~CThreadSafeVariable() { }
00064 
00065                 /** Return a copy of the hold variable */
00066                 T get() const
00067                 {
00068                         T ret;
00069                         {
00070                                 CCriticalSectionLocker l(&m_cs);
00071                                 ret = m_val;
00072                         }
00073                         return ret;
00074                 }
00075 
00076                 /** Return a copy of the hold variable */
00077                 void get(T &out_val) const
00078                 {
00079                         CCriticalSectionLocker l(&m_cs);
00080                         out_val = m_val;
00081                 }
00082 
00083                 /** Return a copy of the hold variable */
00084                 operator T(void) const
00085                 {
00086                         CCriticalSectionLocker l(&m_cs);
00087                         return m_val;
00088                 }
00089 
00090                 /** Return a copy of the hold variable */
00091                 void set(const T &new_val)
00092                 {
00093                         CCriticalSectionLocker l(&m_cs);
00094                         m_val = new_val;
00095                 }
00096 
00097                 /** Swap the current value of the hold variable and the passed one, as one atomic operation. */
00098                 void swap(T &in_out_var)
00099                 {
00100                         CCriticalSectionLocker l(&m_cs);
00101                         std::swap(in_out_var,m_val);
00102                 }
00103         };
00104 
00105 } // End of namespace
00106 } // End of namespace
00107 
00108 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011