RTAB-Map 0.23.13
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UScopeMutex Class Reference

#include <UMutex.h>

Public Member Functions

 UScopeMutex (const UMutex &mutex, bool lockNow=true)
 
 UScopeMutex (UMutex *mutex)
 
 ~UScopeMutex ()
 
int lock () &
 
int lockTry () &
 
int unlock () &
 
bool isLocked () const
 

Detailed Description

Automatically lock the referenced mutex on constructor and unlock mutex on destructor.

Example:

UMutex m; // Mutex shared with another thread(s).
...
int myMethod()
{
UScopeMutex sm(m); // automatically lock the mutex m
if(cond1)
{
return 1; // automatically unlock the mutex m
}
else if(cond2)
{
return 2; // automatically unlock the mutex m
}
return 0; // automatically unlock the mutex m
}

The lock can also be deferred, for example to only try locking it. The destructor then unlocks the mutex only if this object locked it:

void callback()
{
UScopeMutex sm(m, false); // not locked yet
if(sm.lockTry() == 0)
{
if(cond1)
{
return; // automatically unlock the mutex m
}
...
}
// the mutex m is unlocked only if lockTry() succeeded
}

The object must be named: a temporary would be destroyed, and the mutex unlocked, at the end of the expression. lock(), lockTry() and unlock() can only be called on a named object, so that if(UScopeMutex(m, false).lockTry() == 0) doesn't compile. In C++17, the object can be scoped to an if statement instead:

if(UScopeMutex sm(m, false); sm.lockTry() == 0)
{
// locked here
} // unlocked here, only if lockTry() succeeded
int lockTry() &
Definition UMutex.h:246
See also
UMutex

Definition at line 193 of file UMutex.h.

Constructor & Destructor Documentation

◆ UScopeMutex() [1/2]

UScopeMutex::UScopeMutex ( const UMutex &  mutex,
bool  lockNow = true 
)
inline
Parameters
mutexthe mutex to lock.
lockNowif true (default), the mutex is locked here. If false, it is not locked until lock() or lockTry() is called.

Definition at line 201 of file UMutex.h.

◆ UScopeMutex() [2/2]

UScopeMutex::UScopeMutex ( UMutex *  mutex)
inline

Definition at line 211 of file UMutex.h.

◆ ~UScopeMutex()

UScopeMutex::~UScopeMutex ( )
inline

Unlock the mutex, only if this object locked it.

Definition at line 220 of file UMutex.h.

Member Function Documentation

◆ lock()

int UScopeMutex::lock ( ) &
inline

Lock the mutex, if this object doesn't hold it already.

Returns
0 on success, an error code otherwise.

Definition at line 229 of file UMutex.h.

◆ lockTry()

int UScopeMutex::lockTry ( ) &
inline

Try locking the mutex, if this object doesn't hold it already.

Returns
0 if the mutex is held by this object, EBUSY (or another error code) otherwise.

Definition at line 246 of file UMutex.h.

◆ unlock()

int UScopeMutex::unlock ( ) &
inline

Unlock the mutex before this object goes out of scope, only if this object locked it.

Returns
0 on success (or if this object didn't hold the mutex), an error code otherwise.

Definition at line 262 of file UMutex.h.

◆ isLocked()

bool UScopeMutex::isLocked ( ) const
inline
Returns
true if this object currently holds the mutex.

Definition at line 275 of file UMutex.h.


The documentation for this class was generated from the following file: