Automatically lock the referenced mutex on constructor and unlock mutex on destructor.
Example:
...
int myMethod()
{
if(cond1)
{
return 1;
}
else if(cond2)
{
return 2;
}
return 0;
}
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()
{
if(sm.lockTry() == 0)
{
if(cond1)
{
return;
}
...
}
}
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:
- See also
- UMutex
Definition at line 193 of file UMutex.h.