Vault  4.1
Public Member Functions | Protected Attributes
VMutexLocker Class Reference

VMutexLocker is a helper class that you can use to make working with mutexes easier, and more importantly to guarantee proper release behavior of a VMutex even when exceptions are raised. More...

#include <vmutexlocker.h>

Inheritance diagram for VMutexLocker:
VMutexUnlocker

List of all members.

Public Member Functions

 VMutexLocker (VMutex *mutex, const VString &name, bool lockInitially=true)
 Constructs the locker, and if specified, acquires the mutex lock.
virtual ~VMutexLocker ()
 Destructor, unlocks the mutex if this object has acquired it.
virtual void lock ()
 Acquires the mutex lock; if the mutex is currently locked by another thread, this call blocks until the mutex lock can be acquired (if several threads are competing, the order in which they acquire the mutex is not known).
virtual void unlock ()
 Releases the mutex lock; if one or more other threads is waiting on the mutex, one of them will unblock and acquire the mutex lock once this thread releases it.
bool isLocked () const
 Returns true if this object has acquired the lock.
VMutexgetMutex ()
 Returns a pointer to the VMutex object.
void yield ()
 Yields the current thread with the mutex unlocked; that is, this function unlocks the mutex, yields the current thread, and re-locks the mutex before returning.

Protected Attributes

VMutexmMutex
 Pointer to the VMutex object, or NULL.
bool mIsLocked
 True if this object has acquired the lock.
VString mName
 The name of this locker, for diagnostic purposes.

Detailed Description

VMutexLocker is a helper class that you can use to make working with mutexes easier, and more importantly to guarantee proper release behavior of a VMutex even when exceptions are raised.

VMutexLocker lets you avoid ugly code to properly manage the mutex, and instead let C++ auto object destruction do the work for you.

Typically, you need to acquire and release a mutex lock in a function in order to guarantee thread-safety. It is important that the mutex lock gets released when you are done with it. If you throw an exception, this is ugly to properly code, but VMutexLocker makes it trivial.

void doSomethingSafelyToAnObject(MyObject obj)
{
VMutexLocker locker(obj.mMutex);

obj.somethingDangerous(); // might throw an exception!

if (obj.trouble())
    throw VException("Oh no!");
}

In the example above, you are guaranteed that the MyObject's mMutex will be properly unlocked no matter whether you or something you call throws an exception. This is because the locker object is guaranteed to be properly destructed when the function scope exits, and the object's destructor releases the mutex lock.

You can call the lock() method separately if you need to construct the VMutexLocker without locking right away, but lock it later.

You can call the unlock() method separately if you need to unlock the mutex before the VMutexLocker is destructed. Another useful technique to cause the lock to be released earlier than the end of a function is to create a scope specifically to surround the VMutexLocker's existence.

See also:
VMutex

Definition at line 66 of file vmutexlocker.h.


Constructor & Destructor Documentation

VMutexLocker::VMutexLocker ( VMutex mutex,
const VString name,
bool  lockInitially = true 
)

Constructs the locker, and if specified, acquires the mutex lock.

If the mutex is already locked by another thread, this call blocks until it obtains the lock.

You can pass NULL to constructor if you don't want anything to happen; this can useful if, for example, you allow a NULL VMutex pointer to be passed to a routine that needs to lock it if supplied.

Parameters:
mutexthe VMutex to lock, or NULL if no action is wanted
namethe mutex locker name; calling object/function name is a useful string
lockInitiallytrue if the lock should be acquired on construction

Definition at line 17 of file vmutexlocker.cpp.

References lock().


Member Function Documentation

bool VMutexLocker::isLocked ( ) const [inline]

Returns true if this object has acquired the lock.

Returns:
true if this object has acquired the lock

Definition at line 105 of file vmutexlocker.h.

References mIsLocked.

VMutex* VMutexLocker::getMutex ( ) [inline]

Returns a pointer to the VMutex object.

Returns:
a pointer to the VMutex object (may be NULL)

Definition at line 110 of file vmutexlocker.h.

References mMutex.

void VMutexLocker::yield ( )

Yields the current thread with the mutex unlocked; that is, this function unlocks the mutex, yields the current thread, and re-locks the mutex before returning.

This can be used to make a thread that has a lock more "cooperative" with other threads that are competing for the same lock.

Definition at line 53 of file vmutexlocker.cpp.

References isLocked(), lock(), and unlock().


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

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.