Main MRPT website > C++ reference for MRPT 1.5.7
CCriticalSection.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/threads.h>
14 #include <mutex>
15 
16 using namespace mrpt::utils;
17 using namespace mrpt::synch;
18 
19 // CAbstractMutex
20 CAbstractMutex::~CAbstractMutex()
21 {
22 }
23 
24 // CCriticalSection
25 CCriticalSection::CCriticalSection(const char *name) :
26  m_data(new std::mutex()),
27  m_name(name ? name : "Unnamed"),
28  m_debugOut(nullptr)
29 {
30 }
31 
33 {
34  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
35  delete mut;
36  m_data = nullptr;
37 }
38 
40 {
41  return m_name;
42 }
43 
45 {
46  const unsigned long threadid = mrpt::system::getCurrentThreadId();
47  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Entering Thread ID:%lu\n", m_name.c_str(), threadid);
48 
49  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
50  mut->lock();
51 
52  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Entering DONE Thread ID:%lu\n", m_name.c_str(), threadid);
53 }
54 
56 {
57  const unsigned long threadid = mrpt::system::getCurrentThreadId();
58  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Leaving Thread ID:%lu\n", m_name.c_str(), threadid);
59 
60  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
61  mut->unlock();
62 }
63 
65 {
66  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
67  return mut->try_lock();
68 }
69 
70 // CCriticalSectionRecursive
72  m_data( new std::recursive_mutex() )
73 {
74 }
76 {
77  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
78  delete mut;
79  m_data=nullptr;
80 }
81 
83 {
84  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
85  mut->lock();
86 }
87 
89 {
90  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
91  mut->unlock();
92 }
93 
95 {
96  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
97  return mut->try_lock();
98 }
99 
100 
101 
102 
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
void enter() const MRPT_OVERRIDE
Enter.
std::string getName() const
Returns the name used in the constructor.
unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS
Returns the ID of the current thread.
Definition: threads.cpp:83
utils::CStream * m_debugOut
If set to a non-NULL value, debug messages regarding the calling threads IDs will be output...
STL namespace.
bool try_enter() const MRPT_OVERRIDE
Returns true if adquired; false otherwise.
void leave() const MRPT_OVERRIDE
Leave.
void enter() const MRPT_OVERRIDE
Enter.
bool try_enter() const MRPT_OVERRIDE
Returns true if adquired; false otherwise.
This namespace provides multitask, synchronization utilities.
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 ...
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
void leave() const MRPT_OVERRIDE
Leave.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:507



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