Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
mrpt::synch::CCriticalSectionLocker::m_cs
const CCriticalSection * m_cs
Definition: CCriticalSection.h:78
mrpt::synch::CCriticalSection::getName
std::string getName() const
Returns the name used in the constructor.
Definition: CCriticalSection.h:57
mrpt::synch::CCriticalSectionLocker
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
Definition: CCriticalSection.h:75
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:16
mrpt::synch::CCriticalSection::m_data
mrpt::utils::CReferencedMemBlock m_data
The OS-dependent descriptors.
Definition: CCriticalSection.h:34
CReferencedMemBlock.h
mrpt::synch::CCriticalSection
This class provides simple critical sections functionality.
Definition: CCriticalSection.h:31
mrpt::utils::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
mrpt::synch::CCriticalSectionLocker::CCriticalSectionLocker
CCriticalSectionLocker(const CCriticalSectionLocker &o)
Definition: CCriticalSection.h:86
mrpt::synch::CCriticalSection::m_name
std::string m_name
Definition: CCriticalSection.h:36
mrpt::utils::CReferencedMemBlock
Represents a memory block (via "void*") that can be shared between several objects through copy opera...
Definition: CReferencedMemBlock.h:27
mrpt::synch::CCriticalSection::m_debugOut
utils::CStream * m_debugOut
If set to a non-NULL value, debug messages regarding the calling threads IDs will be output.
Definition: CCriticalSection.h:61



Page generated by Doxygen 1.8.16 for MRPT 1.4.0 SVN: at Mon Oct 14 23:11:08 UTC 2019