Main MRPT website > C++ reference for MRPT 1.5.6
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 
13 #include <mrpt/utils/mrpt_macros.h>
14 #include <string>
15 
16 namespace mrpt
17 {
18  namespace utils { class CStream; }
19 
20  /** @defgroup synch_grp Synchronization, multi-threading synch tools
21  * \ingroup mrpt_base_grp */
22 
23 
24  /** This namespace provides multitask, synchronization utilities. \ingroup synch_grp
25  */
26  namespace synch
27  {
29  {
30  public:
31  virtual ~CAbstractMutex();
32  virtual void enter() const =0;
33  virtual void leave() const =0;
34  virtual bool try_enter() const = 0; //!< Returns true if adquired; false otherwise.
35  };
36 
37  /** This class provides simple critical sections functionality.
38  * \sa CCriticalSectionLocker
39  * \ingroup synch_grp
40  */
42  {
43  private:
44  void *m_data; //!< std::mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers
46  public:
47  CCriticalSection(const char *name = NULL); //!< Ctor
48  virtual ~CCriticalSection(); //!< Dtor
49 
50  void enter() const MRPT_OVERRIDE; //!< Enter. \exception If the calling thread already possesses this critical section (it would be a dead-lock).
51  void leave() const MRPT_OVERRIDE; //!< Leave. \exception If the calling thread is not the current owner of the critical section.
52  bool try_enter() const MRPT_OVERRIDE;
53 
54  /** Returns the name used in the constructor. */
55  std::string getName() const { return m_name; }
56 
57  /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output. */
59  };
60 
61  /** Recursive mutex: allow recursive locks by the owner thread.
62  * \sa CCriticalSectionLocker, CCriticalSection
63  * \note [New in MRPT 1.5.0]
64  * \ingroup synch_grp
65  */
67  {
68  private:
69  void *m_data; //!< std::recursive_mutex*. Opaque ptr until MRPT 2.0.0 in which we could expose C++11 to user headers
70  public:
72  virtual ~CCriticalSectionRecursive();
73 
74  void enter() const MRPT_OVERRIDE; //!< Enter. \exception If the calling thread already possesses this critical section (it would be a dead-lock).
75  void leave() const MRPT_OVERRIDE; //!< Leave. \exception If the calling thread is not the current owner of the critical section.
76  bool try_enter() const MRPT_OVERRIDE;
77  };
78 
79  /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
80  * 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.
81  * Example:
82  * \code
83  * { // Code in this scope is protected by critical section
84  * CCriticalSectionLocker myCSLocker( &myCS );
85  * ...
86  * } // End of code protected by critical section
87  * \endcode
88  * \sa CCriticalSection, THREADSAFE_OPERATION
89  */
91  {
92  protected:
93  const CAbstractMutex *m_cs; // it's safe for copy ctor & =op to copy this ptr
94 
95  public:
96  /** Constructor: enters the critical section.
97  * \note [Since MRPT 0.9.6] The pointer can be NULL, in which case no action at all will be taken.
98  */
100 
101  CCriticalSectionLocker(const CCriticalSectionLocker &o) : m_cs(o.m_cs) {}
102 
103  /** Destructor: leaves the critical section. */
105 
106  }; // end of CCriticalSectionLocker
107 
108 
109 
110  /** A macro for protecting a given piece of code with a critical section; for example:
111  * \code
112  * CCriticalSection cs;
113  * MyObject obj;
114  * ...
115  *
116  * THREADSAFE_OPERATION(cs, obj.foo(); )
117  * ...
118  * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); }
119  *
120  * \endcode
121  *
122  * \sa CCriticalSectionLocker, CThreadSafeVariable
123  */
124  #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \
125  { \
126  mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \
127  CODE_TO_EXECUTE \
128  }
129 
130 
131  } // End of namespace
132 } // End of namespace
133 
134 #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:
utils::CStream * m_debugOut
If set to a non-NULL value, debug messages regarding the calling threads IDs will be output...
STL namespace.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
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.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019