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

A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. More...

#include <carma/util/PthreadMutex.h>

Public Member Functions

const ::pthread_mutex_t & InternalPthreadMutex () const
 Obtain a reference to the internal ::pthread_mutex_t. More...
 
::pthread_mutex_t & InternalPthreadMutex ()
 Obtain a reference to the internal ::pthread_mutex_t. More...
 
void Lock ()
 Obtain a lock on the instance for the caller's thread. More...
 
 PthreadMutex ()
 Construct a mutex with the CARMA defaults. More...
 
 PthreadMutex (int type)
 Construct a mutex with the given type. More...
 
 PthreadMutex (const PthreadMutexAttr &attr)
 Construct a mutex with the given attributes. More...
 
 PthreadMutex (const ::pthread_mutexattr_t &attr)
 Construct a mutex with the given attributes. More...
 
bool TryLock ()
 Obtain a lock on the instance for the caller's thread and return true if it is possible to do so immediately without blocking and return false if obtaining a lock for the caller's thread would require blocking. More...
 
void Unlock ()
 Release a lock that is held on the instance by the caller's thread. More...
 
int UnlockNoThrow ()
 
virtual ~PthreadMutex ()
 Destruct a mutex. More...
 

Detailed Description

A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world.

If you have no idea what a mutex is then here is the general idea: A mutex is an token/object that only one thread can being holding a lock on at a time. It is used to serialize manipulation/access to some shared resource from multiple threads. Hence, methods of PthreadMutex are safe to call concurrently on a single instance. They wouldn't be terribly useful if this wasn't the case.

Definition at line 41 of file PthreadMutex.h.

Constructor & Destructor Documentation

carma::util::PthreadMutex::PthreadMutex ( )
explicit

Construct a mutex with the CARMA defaults.

Any internal errors will throw an exception.

Postcondition
A lock is not held on the instance by any thread.
carma::util::PthreadMutex::PthreadMutex ( int  type)
explicit

Construct a mutex with the given type.

Any internal errors will throw an exception.

Postcondition
A lock is not held on the instance by any thread.
carma::util::PthreadMutex::PthreadMutex ( const PthreadMutexAttr attr)
explicit

Construct a mutex with the given attributes.

Any internal errors will throw an exception.

Postcondition
A lock is not held on the instance by any thread.
carma::util::PthreadMutex::PthreadMutex ( const ::pthread_mutexattr_t &  attr)
explicit

Construct a mutex with the given attributes.

Any internal errors will throw an exception.

Postcondition
A lock is not held on the instance by any thread.
virtual carma::util::PthreadMutex::~PthreadMutex ( )
virtual

Destruct a mutex.

No exceptions will be thrown and hence internal errors will be ignored as far as clients are concerned (though they may be logged).

Precondition
A lock is not held on the instance by any thread.

Member Function Documentation

const ::pthread_mutex_t & carma::util::PthreadMutex::InternalPthreadMutex ( ) const

Obtain a reference to the internal ::pthread_mutex_t.

Warning
Use this with care. Certainly do not go off and call something like ::pthread_mutex_destroy on the return value. If you don't know what you are doing with POSIX mutexes then think twice before using this method.
Returns
a reference to the internal ::pthread_mutex_t

Definition at line 261 of file PthreadMutex.h.

pthread_mutex_t & carma::util::PthreadMutex::InternalPthreadMutex ( )

Obtain a reference to the internal ::pthread_mutex_t.

Warning
Use this with care. Certainly do not go off and call something like ::pthread_mutex_destroy on the return value. If you don't know what you are doing with POSIX mutexes then think twice before using this method.
Returns
a reference to the internal ::pthread_mutex_t

Definition at line 268 of file PthreadMutex.h.

void carma::util::PthreadMutex::Lock ( )

Obtain a lock on the instance for the caller's thread.

May possibly block or deadlock for an indeterminate amount of time along the way. Detected errors will throw an exception. The behavior if a lock on the instance is already held by the caller's thread at the time of the call is defined (or undefined) by POSIX according to the type of mutex that was constructed.

Precondition
A lock is not held on the instance by the caller's thread.
Postcondition
Either a lock is held on the instance by the caller's thread or an exception was thrown (in which case a lock is not held on the instance by the caller's thread if it was not held by the caller's thread at the time of the call).

Definition at line 233 of file PthreadMutex.h.

bool carma::util::PthreadMutex::TryLock ( )

Obtain a lock on the instance for the caller's thread and return true if it is possible to do so immediately without blocking and return false if obtaining a lock for the caller's thread would require blocking.

Detected errors will throw an exception. The behavior is undefined if a lock on the instance is already held by the caller's thread at the time of the call.

Returns
true if a lock was obtained and false if it was not.
Precondition
A lock is not held on the instance by the caller's thread.
Postcondition
One of the following states: a lock is held on the instance by the caller's thread and true was returned, a lock is not held on the instance by the caller's thread and false was returned, or an exception was thrown (in which case a lock is not held on the on the instance by the caller's thread if it was not held by the caller's thread at the time of the call).

Definition at line 240 of file PthreadMutex.h.

void carma::util::PthreadMutex::Unlock ( )

Release a lock that is held on the instance by the caller's thread.

Detected errors will throw an exception. The behavior is undefined if a lock is not held on the instance by the caller's thread at the time of the call.

Precondition
A lock is held on the instance by the caller's thread.
Postcondition
Either a lock is not held on the instance by the caller's thread or an exception was thrown (in which case the caller's lock holding state is undefined).

Definition at line 247 of file PthreadMutex.h.


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