Loki  0.1.7
Loki::MultiMutexLocker Class Reference

#include <LevelMutex.h>

Public Member Functions

 MultiMutexLocker (LevelMutexInfo::MutexContainer &mutexes, bool lock=true)
 
 MultiMutexLocker (LevelMutexInfo::MutexContainer &mutexes, unsigned int milliSeconds, bool lock=true)
 
 ~MultiMutexLocker (void)
 Destructs the locker, and determines if it needs to unlock the mutexes.
 
bool Lock (void)
 
bool Unlock (void)
 
bool IsLocked (void) const
 Returns true if the mutexes are locked by this object.
 
const LevelMutexInfo::MutexContainerGetMutexes (void) const
 Provides access to the collection of mutexes controlled by this.
 

Detailed Description

You can place an instance of this as a local variable inside a function to lock a collection of mutexes. It locks them if no error occurs, or throws an exception if one does happen. When the function ends, the destructor determines if it needs to unlock the mutexes. This RAII technique insures the mutexes get unlocked even when exceptions occur. You will also have to construct a MutexContainer as a local object within the same function.

Constructor & Destructor Documentation

◆ MultiMutexLocker() [1/2]

Loki::MultiMutexLocker::MultiMutexLocker ( LevelMutexInfo::MutexContainer mutexes,
bool  lock = true 
)
explicit

Creates an object to lock and unlock a collection of mutexes for a function. This will throw if an attempt to lock any mutex fails. If an exception occurs, it unlocks mutexes it previously locked.

Parameters
mutexReference to a collection of mutexes.
lockTrue if function wants to lock the mutex as this gets constructed.

References Loki::LevelMutexInfo::MultiLock().

Here is the call graph for this function:

◆ MultiMutexLocker() [2/2]

Loki::MultiMutexLocker::MultiMutexLocker ( LevelMutexInfo::MutexContainer mutexes,
unsigned int  milliSeconds,
bool  lock = true 
)

Creates an object to lock and unlock a collection of mutexes for a function. This waits a specified amount of time for other threads to unlock each mutex that is locked. This will throw if an attempt to lock any mutex fails. If an exception occurs, it unlocks mutexes it previously locked.

Parameters
mutexesReference to a collection of mutexes.
milliSecondsAmount of time to wait for another thread to unlock the mutex.
lockTrue if function wants to lock the mutexes as this gets constructed.

References Loki::LevelMutexInfo::MultiLock().

Here is the call graph for this function:

Member Function Documentation

◆ Lock()

bool Loki::MultiMutexLocker::Lock ( void  )

You can call this to lock (or relock) the mutexes. In theory, you can lock and unlock mutexes several times within a function in order to give other threads access to resources while this function does not need them.

Returns
True if mutex is locked by this, else false if not locked.

References Loki::LevelMutexInfo::MultiLock().

Here is the call graph for this function:

◆ Unlock()

bool Loki::MultiMutexLocker::Unlock ( void  )

You can call this to unlock the mutexes before the destructor does it. By unlocking the mutexes before returning, the function can do other operations without making other threads wait too long.

Returns
True if unlocked by this, else false if not unlocked by this. (Which is not the same as whether the mutex itself is locked or not by another thread.)

References Loki::LevelMutexInfo::MultiUnlock().

Here is the call graph for this function:

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