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 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 */ 00052 template <typename T> 00053 class CThreadSafeVariable 00054 { 00055 private: 00056 CCriticalSection m_cs; 00057 T m_val; 00058 public: 00059 CThreadSafeVariable() : m_cs(), m_val() { } 00060 CThreadSafeVariable(const T& init_val) : m_cs(), m_val(init_val) { } 00061 00062 virtual ~CThreadSafeVariable() { } 00063 00064 /** Return a copy of the hold variable */ 00065 T get() const 00066 { 00067 T ret; 00068 { 00069 CCriticalSectionLocker l(&m_cs); 00070 ret = m_val; 00071 } 00072 return ret; 00073 } 00074 00075 /** Return a copy of the hold variable */ 00076 void get(T &out_val) const 00077 { 00078 CCriticalSectionLocker l(&m_cs); 00079 out_val = m_val; 00080 } 00081 00082 /** Return a copy of the hold variable */ 00083 operator T(void) const 00084 { 00085 CCriticalSectionLocker l(&m_cs); 00086 return m_val; 00087 } 00088 00089 /** Return a copy of the hold variable */ 00090 void set(const T &new_val) 00091 { 00092 CCriticalSectionLocker l(&m_cs); 00093 m_val = new_val; 00094 } 00095 00096 /** Swap the current value of the hold variable and the passed one, as one atomic operation. */ 00097 void swap(T &in_out_var) 00098 { 00099 CCriticalSectionLocker l(&m_cs); 00100 std::swap(in_out_var,m_val); 00101 } 00102 }; 00103 00104 } // End of namespace 00105 } // End of namespace 00106 00107 #endif
Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011 |