Main MRPT website > C++ reference for MRPT 1.3.2
CCriticalSection.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef mrpt_synch_criticalsection_H
10 #define mrpt_synch_criticalsection_H
11 
13 #include <string>
14 
15 namespace mrpt
16 {
17  namespace utils { class CStream; }
18 
19  /** @defgroup synch_grp Synchronization, multi-threading synch tools
20  * \ingroup mrpt_base_grp */
21 
22 
23  /** This namespace provides multitask, synchronization utilities. \ingroup synch_grp
24  */
25  namespace synch
26  {
27  /** This class provides simple critical sections functionality.
28  * \sa CCriticalSectionLocker
29  * \ingroup synch_grp
30  */
32  {
33  private:
34  mrpt::utils::CReferencedMemBlock m_data; //!< The OS-dependent descriptors
35 
36  std::string m_name;
37  public:
38  /** Constructor
39  */
40  CCriticalSection(const char *name = NULL);
41 
42  /** Destructor
43  */
45 
46  /** Enter.
47  * \exception If the calling thread already possesses this critical section (it would be a dead-lock).
48  */
49  void enter() const;
50 
51  /** Leave
52  * \exception If the calling thread is not the current owener of the critical section.
53  */
54  void leave() const;
55 
56  /** Returns the name used in the constructor. */
57  std::string getName() const { return m_name; }
58 
59  /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output.
60  */
62  };
63 
64  /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
65  * 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.
66  * Example:
67  * \code
68  * { // Code in this scope is protected by critical section
69  * CCriticalSectionLocker myCSLocker( &myCS );
70  * ...
71  * } // End of code protected by critical section
72  * \endcode
73  * \sa CCriticalSection, THREADSAFE_OPERATION
74  */
76  {
77  protected:
79 
80  public:
81  /** Constructor: enters the critical section.
82  * \note [Since MRPT 0.9.6] The pointer can be NULL, in which case no action at all will be taken.
83  */
85 
87  {
88  }
89 
91  {
92  m_cs = o.m_cs;
93  return *this;
94  }
95 
96  /** Destructor: leaves the critical section.
97  */
99 
100  }; // end of CCriticalSectionLocker
101 
102 
103 
104  /** A macro for protecting a given piece of code with a critical section; for example:
105  * \code
106  * CCriticalSection cs;
107  * MyObject obj;
108  * ...
109  *
110  * THREADSAFE_OPERATION(cs, obj.foo(); )
111  * ...
112  * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); }
113  *
114  * \endcode
115  *
116  * \sa CCriticalSectionLocker, CThreadSafeVariable
117  */
118  #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \
119  { \
120  mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \
121  CODE_TO_EXECUTE \
122  }
123 
124 
125  } // End of namespace
126 } // End of namespace
127 
128 #endif
This class provides simple critical sections functionality.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
CCriticalSectionLocker(const CCriticalSectionLocker &o)
utils::CStream * m_debugOut
If set to a non-NULL value, debug messages regarding the calling threads IDs will be output...
Represents a memory block (via "void*") that can be shared between several objects through copy opera...
class BASE_IMPEXP CStream
Definition: math_frwds.h:26
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
mrpt::utils::CReferencedMemBlock m_data
The OS-dependent descriptors.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string getName() const
Returns the name used in the constructor.



Page generated by Doxygen 1.8.11 for MRPT 1.3.2 SVN: at Mon May 9 06:50:38 UTC 2016