This class is a simple wrapper around C++ standard library type std::recursive_mutex
. Thus, it is used to implement mutual exclusive access to resources by protecting critical code sections from being executed in parallel in concurrent threads.
When a pair of AcquireRecursive and ReleaseRecursive invocations is performed within the same code block, then it is recommended to use a stack instantiation of class OwnerRecursive to acquire and release objects of this class. Such a use is highly simplified with macros ALIB_LOCK_RECURSIVE and ALIB_LOCK_RECURSIVE_WITH.
Nested acquisitions are supported with this type. An instance of this class is released when an equal number of invocations to AcquireRecursive and ReleaseRecursive have been performed.
This class has slightly reduced performance in comparison to non-recursive type Lock. Not only for this reason, non-nested locking is the preferred technique. But there are situations where nested locks are just unavoidable.
std::recursive_mutex
, with debug-compilations class std::recursive_timed_mutex
is wrapped.Definition at line 48 of file recursivelock.inl.
Public Field Index: | |
DbgLockAsserter | Dbg |
The debug tool instance. | |
Public Method Index: | |
~RecursiveLock () override | |
Destructor. With debug-compilations, asserts that this lock is not acquired. | |
ALIB_DLL void | AcquireRecursive (ALIB_DBG_TAKE_CI) |
virtual ALIB_DLL bool | DCSIsAcquired () const override |
virtual ALIB_DLL bool | DCSIsSharedAcquired () const override |
ALIB_DLL void | ReleaseRecursive (ALIB_DBG_TAKE_CI) |
ALIB_DLL bool | TryAcquire (ALIB_DBG_TAKE_CI) |
![]() | |
virtual | ~AssociatedLock () |
Virtual Destructor. | |
Protected Field Index: | |
std::recursive_mutex | mutex |
DbgLockAsserter alib::threads::RecursiveLock::Dbg |
The debug tool instance.
Definition at line 68 of file recursivelock.inl.
|
protected |
The internal object to lock on.
std::recursive_timed_mutex
. Definition at line 61 of file recursivelock.inl.
|
inlineoverride |
Destructor. With debug-compilations, asserts that this lock is not acquired.
Definition at line 74 of file recursivelock.inl.
void alib::threads::RecursiveLock::AcquireRecursive | ( | ALIB_DBG_TAKE_CI | ) |
Thread which invokes this method gets registered as the current owner of this object, until the same thread releases the ownership invoking ReleaseRecursive. In the case that this object is already owned by another thread, the invoking thread is suspended until ownership can be gained. Multiple (nested) calls to this method are counted and the object is only released when the same number of Release() calls have been made.
Definition at line 211 of file locks.cpp.
|
overridevirtual |
true
if the lock is acquired (in non-shared mode), false
otherwise. Implements alib::lang::DbgCriticalSections::AssociatedLock.
|
overridevirtual |
true
if the lock is shared-acquired (by at least any thread). Otherwise, returns false
. Implements alib::lang::DbgCriticalSections::AssociatedLock.
void alib::threads::RecursiveLock::ReleaseRecursive | ( | ALIB_DBG_TAKE_CI | ) |
Releases ownership of this object. If AcquireRecursive was called multiple times before, the same number of calls to this method has to be performed to release ownership.
|
nodiscard |
Tries to acquire this lock. Successful calls to this method are counted, as if AcquireRecursive was called and an according invocation of ReleaseRecursive has to be performed.
true
if the lock was not acquired by a different thread and thus, this call was successful. false
otherwise.