template<typename M>
class carma::util::ScopedLockManager< M >
A scope class (i.e.
a class that does or manages something for the lifetime of the instance) that makes non-trivial mutex lock management easier in a C++ world.
- Note
- Please note that use of this template requires that the expressions
* lockMutex( m );
* tryLockMutex( m );
* unlockMutex( m );
* unlockMutexNoThrow( m );
*
are valid and will lock, try to lock non-blocking, and unlock a mutex object if m is of type M &. In particular, to use the types ScopedLock< PthreadMutex > and/or ScopedLock< pthread_mutex_t > you will need to include the file "carma/util/PthreadMutex.h" to get prototypes for the appropriate functions.
Lock chaining usage might look something like this:
* #include "MutexType.h"
*
* static long long gSharedValue1;
* static MutexType gSharedValue1Guard;
*
* static short gSharedValue2;
* static MutexType gSharedValue2Guard;
*
* void
* UpdateSharedValues( const long long newValue1,
* const short newValue2 ) {
* ScopedLockManager< MutexType > lock1Manager( gSharedValue1Guard );
* ScopedLockManager< MutexType > lock2Manager( gSharedValue2Guard );
*
* lock1Manager.lock( );
* gSharedValue1 = newValue1;
* lock2Manager.lock( );
* lock1Manager.unlock( );
* gSharedValue2 = newValue2;
* }
*
Another usage might look something like this:
* #include "MutexType.h"
*
* static long long gSharedValue;
* static MutexType gSharedValueGuard;
*
* void
* UpdateSharedValueIfConvenient( const long long newValue ) {
* ScopedLockManager< MutexType > lockManager( gSharedValueGuard );
*
* if ( lockManager.tryLock( ) )
* gSharedValue = newValue;
* }
*
If your locking needs are simple then ScopedLock might be all you need.
Definition at line 72 of file ScopedLockManager.h.