CARMA C++
carma::util::ScopedPthreadMutexLockManager Class Reference

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

#include <carma/util/ScopedPthreadMutexLockManager.h>

Public Member Functions

void LockMutex ()
 
 ScopedPthreadMutexLockManager (PthreadMutex &mutex)
 
bool TryLockMutex ()
 
void UnlockMutex ()
 

Detailed Description

A scope class (i.e.

a class that does or manages something for the lifetime of the instance) that makes non-trivial PthreadMutex lock management easier in a C++ world.

If your locking needs are simple then ScopedPthreadMutexLock might be all you need. Lock chaining usage might look something like this:

* static long long gSharedValue1;
* static PthreadMutex gSharedValue1Guard;
*
* static short gSharedValue2;
* static PthreadMutex gSharedValue2Guard;
*
* void
* UpdateSharedValues( const long long newValue1,
* const short newValue2 ) {
* ScopedPthreadMutexLockManager lock1Manager( gSharedValue1Guard );
* ScopedPthreadMutexLockManager lock2Manager( gSharedValue2Guard );
*
* lock1Manager.LockMutex( );
* gSharedValue1 = newValue1;
* lock2Manager.LockMutex( );
* lock1Manager.UnlockMutex( );
* gSharedValue2 = newValue2;
* }
*

Another usage might look something like this:

* static long long gSharedValue;
* static PthreadMutex gSharedValueGuard;
*
* void
* UpdateSharedValueIfConvenient( const long long newValue ) {
* ScopedPthreadMutexLockManager lockManager( gSharedValueGuard );
*
* if ( lockManager.TryLockMutex( ) )
* gSharedValue = newValue;
* }
*

Definition at line 54 of file ScopedPthreadMutexLockManager.h.


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