Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 
12 #include <mrpt/utils/mrpt_macros.h>
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  {
28  {
29  public:
30  virtual ~CAbstractMutex();
31  virtual void enter() const =0;
32  virtual void leave() const =0;
33  virtual bool try_enter() const = 0; //!< Returns true if adquired; false otherwise.
34  };
35 
36  /** This class provides simple critical sections functionality.
37  * \sa CCriticalSectionLocker
38  * \ingroup synch_grp
39  */
41  {
42  private:
43  void *m_data; //!< std::mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers
45  public:
46  CCriticalSection(const char *name = NULL); //!< Ctor
47  virtual ~CCriticalSection(); //!< Dtor
48 
49  void enter() const MRPT_OVERRIDE; //!< Enter. \exception If the calling thread already possesses this critical section (it would be a dead-lock).
50  void leave() const MRPT_OVERRIDE; //!< Leave. \exception If the calling thread is not the current owner of the critical section.
51  bool try_enter() const MRPT_OVERRIDE;
52 
53  /** Returns the name used in the constructor. */
54  std::string getName() const;
55 
56  /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output. */
57  utils::CStream *m_debugOut;
58  };
59 
60  /** Recursive mutex: allow recursive locks by the owner thread.
61  * \sa CCriticalSectionLocker, CCriticalSection
62  * \note [New in MRPT 1.5.0]
63  * \ingroup synch_grp
64  */
66  {
67  private:
68  void *m_data; //!< std::recursive_mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers
69  public:
71  virtual ~CCriticalSectionRecursive();
72 
73  void enter() const MRPT_OVERRIDE; //!< Enter. \exception If the calling thread already possesses this critical section (it would be a dead-lock).
74  void leave() const MRPT_OVERRIDE; //!< Leave. \exception If the calling thread is not the current owner of the critical section.
75  bool try_enter() const MRPT_OVERRIDE;
76  };
77 
78  /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
79  * 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.
80  * Example:
81  * \code
82  * { // Code in this scope is protected by critical section
83  * CCriticalSectionLocker myCSLocker( &myCS );
84  * ...
85  * } // End of code protected by critical section
86  * \endcode
87  * \sa CCriticalSection, THREADSAFE_OPERATION
88  */
90  {
91  protected:
92  const CAbstractMutex *m_cs; // it's safe for copy ctor & =op to copy this ptr
93 
94  public:
95  /** Constructor: enters the critical section.
96  * \note [Since MRPT 0.9.6] The pointer can be NULL, in which case no action at all will be taken.
97  */
99 
100  CCriticalSectionLocker(const CCriticalSectionLocker &o) : m_cs(o.m_cs) {}
101 
102  /** Destructor: leaves the critical section. */
104 
105  }; // end of CCriticalSectionLocker
106 
107 
108 
109  /** A macro for protecting a given piece of code with a critical section; for example:
110  * \code
111  * CCriticalSection cs;
112  * MyObject obj;
113  * ...
114  *
115  * THREADSAFE_OPERATION(cs, obj.foo(); )
116  * ...
117  * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); }
118  *
119  * \endcode
120  *
121  * \sa CCriticalSectionLocker, CThreadSafeVariable
122  */
123  #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \
124  { \
125  mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \
126  CODE_TO_EXECUTE \
127  }
128 
129 
130  } // End of namespace
131 } // End of namespace
132 
133 #endif
Recursive mutex: allow recursive locks by the owner thread.
This class provides simple critical sections functionality.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
CCriticalSectionLocker(const CCriticalSectionLocker &o)
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
STL namespace.
GLsizei const GLchar ** string
Definition: glext.h:3919
void * m_data
std::mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void * m_data
std::recursive_mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers ...
GLuint const GLchar * name
Definition: glext.h:3891



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019