Unigine::ScopedLock Class
Header: | #include <UnigineThread.h> |
This class implements a writer lock operating on a simple mutex.
Scoped locks work by locking a mutex when they are constructed, and unlocking it when they are destructed. The C++ rules guarantee that when the control flow leaves a scope (even via an exception), objects local to the scope being exited are destructed correctly. This means using a scoped lock instead of manually calling lock() and unlock() makes it impossible to accidentally not unlock the mutex, e.g. when an exception is thrown in the middle of the code between lock() and unlock().
Usage Example#
Here is a small code-snippet illustrating the scoped lock usage:
// declaring a mutex
Mutex shared_mutex_obj;
// ...
void do_something()
{
// at this point in the ScopedLock constructor the mutex is locked,
// and a reference to it is kept in the 'lock' object for future use
ScopedLock lock(shared_mutex_obj);
//here goes the critical section code
// ...
}//<-- at this point the 'lock' object goes out of scope
// which means that the destructor of ScopedLock will run.
// inside the destructor, the mutex is unlocked.
ScopedLock Class
Members
static ScopedLockPtr create ( Mutex & m ) #
Constructs a scoped lock for the specified synchronization object (simple mutex).Arguments
Last update:
2021-12-13
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)