Main MRPT website > C++ reference for MRPT 1.5.6
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  const unsigned long threadid = mrpt::system::getCurrentThreadId();
42  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Entering Thread ID:%lu\n", m_name.c_str(), threadid);
43 
44  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
45  mut->lock();
46 
47  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Entering DONE Thread ID:%lu\n", m_name.c_str(), threadid);
48 }
49 
51 {
52  const unsigned long threadid = mrpt::system::getCurrentThreadId();
53  if (m_debugOut) m_debugOut->printf("[CCriticalSection:%s] Leaving Thread ID:%lu\n", m_name.c_str(), threadid);
54 
55  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
56  mut->unlock();
57 }
58 
60 {
61  std::mutex *mut = reinterpret_cast<std::mutex*>(m_data);
62  return mut->try_lock();
63 }
64 
65 // CCriticalSectionRecursive
67  m_data( new std::recursive_mutex() )
68 {
69 }
71 {
72  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
73  delete mut;
74  m_data=nullptr;
75 }
76 
78 {
79  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
80  mut->lock();
81 }
82 
84 {
85  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
86  mut->unlock();
87 }
88 
90 {
91  std::recursive_mutex *mut = reinterpret_cast<std::recursive_mutex*>(m_data);
92  return mut->try_lock();
93 }
94 
95 
96 
97 
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.
unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS
Returns the ID of the current thread.
Definition: threads.cpp:211
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.
Definition: atomic_incr.h:29
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.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019