CXScopeLockLambda

From cxwiki

The CXScopeLockLambda class is intended to be constructed on the stack as a mechanism to take a lock and ensure that it is released again when the CXScopeLock object goes out of scope. This is intended as a way to reduce manual lock management errors.
 
In the context of this object, a "lock" may be any logical construct that needs cleaning up; it need not be a CXMutex or similar mechanism.
// Construct a scoped lock, calling the 'lockFunction' immediately, and calling the 'unlockFunction' when unlocking.
CXScopeLockLambda(const std::function<void(void)>& lockFunction, const std::function<void(void)>& unlockFunction);

// Destruct the scoped lock, calling the 'unlockFunction' if not already unlocked.
~CXScopeLockLambda(void);

// Unlock the scoped lock, calling the 'unlockFunction' if not already unlocked.
void Unlock(void);

Intended Usage

It is intended that the 'lockFunction' will perform some locking action, and that 'unlockFunction' will undo that action. While it is of course possible to have the 'lockFunction' perform a null action, leaving the 'unlockFunction' merely to clean up some unrelated state, this usage is not encouraged.