CARMA C++
carma::util::ScopedLockManager< M > Class Template Reference

A scope class (i.e. More...

#include <carma/util/ScopedLockManager.h>

Public Member Functions

void lock ()
 
 ScopedLockManager (M &m)
 
bool tryLock ()
 
void unlock ()
 

Detailed Description

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.


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