Main MRPT website > C++ reference
MRPT logo
CCriticalSection.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 mrpt_synch_criticalsection_H
29 #define mrpt_synch_criticalsection_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/utils/CStream.h>
34 
35 /*---------------------------------------------------------------
36  Class
37  ---------------------------------------------------------------*/
38 namespace mrpt
39 {
40  /** @defgroup synch_grp Synchronization, multi-threading synch tools
41  * \ingroup mrpt_base_grp */
42 
43 
44  /** This namespace provides multitask, synchronization utilities. \ingroup synch_grp
45  */
46  namespace synch
47  {
48  /** This class provides simple critical sections functionality.
49  * \sa CCriticalSectionLocker
50  * \ingroup synch_grp
51  */
53  {
54  private:
55  utils::CReferencedMemBlock m_data; //!< The OS-dependent descriptors
56 
57  std::string m_name;
58  public:
59  /** Constructor
60  */
61  CCriticalSection(const char *name = NULL);
62 
63  /** Destructor
64  */
66 
67  /** Enter.
68  * \exception If the calling thread already possesses this critical section (it would be a dead-lock).
69  */
70  void enter() const;
71 
72  /** Leave
73  * \exception If the calling thread is not the current owener of the critical section.
74  */
75  void leave() const;
76 
77  /** Returns the name used in the constructor. */
78  std::string getName() const { return m_name; }
79 
80  /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output.
81  */
83  };
84 
85  /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
86  * It is a better idea to always use CCriticalSectionLocker, since it is more secure in the case of possible exceptions, many different exit points from a function, etc.. : it will always release the critical section at the destructor.
87  * Example:
88  * \code
89  * { // Code in this scope is protected by critical section
90  * CCriticalSectionLocker myCSLocker( &myCS );
91  * ...
92  * } // End of code protected by critical section
93  * \endcode
94  * \sa CCriticalSection, THREADSAFE_OPERATION
95  */
97  {
98  protected:
100 
101  public:
102  /** Constructor: enters the critical section.
103  * \note [Since MRPT 0.9.6] The pointer can be NULL, in which case no action at all will be taken.
104  */
106 
108  {
109  }
110 
112  {
113  m_cs = o.m_cs;
114  return *this;
115  }
116 
117  /** Destructor: leaves the critical section.
118  */
120 
121  }; // end of CCriticalSectionLocker
122 
123 
124 
125  /** A macro for protecting a given piece of code with a critical section; for example:
126  * \code
127  * CCriticalSection cs;
128  * MyObject obj;
129  * ...
130  *
131  * THREADSAFE_OPERATION(cs, obj.foo(); )
132  * ...
133  * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); }
134  *
135  * \endcode
136  *
137  * \sa CCriticalSectionLocker, CThreadSafeVariable
138  */
139  #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \
140  { \
141  mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \
142  CODE_TO_EXECUTE \
143  }
144 
145 
146  } // End of namespace
147 } // End of namespace
148 
149 #endif



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