# Loki::MutexLocker Class Reference

`#include <LevelMutex.h>`

Collaboration diagram for Loki::MutexLocker:
[legend]

List of all members.

## Public Member Functions

| MutexLocker (volatile LevelMutexInfo &mutex, bool lock=true)
| MutexLocker (volatile LevelMutexInfo &mutex, unsigned int milliSeconds, bool lock=true)
| ~MutexLocker (void)
| Destructs the locker, and determines if it needs to unlock the mutex.
bool | Lock (void)
bool | Unlock (void)
bool | IsLocked (void) const
| Returns true if the mutex is locked by this object.
const volatile LevelMutexInfo & | GetMutex (void) const
| Provides access to mutex controlled by this.

## Detailed Description

You can place an instance of this as a local variable inside a function to lock a single mutex. It will lock the mutex if no error occurs, or throw if one does happen. When the function ends, the destructor will determine if it needs to unlock the mutex. This RAII technique insures the mutex gets unlocked even when exceptions occur.

## Constructor & Destructor Documentation

Loki::MutexLocker::MutexLocker | ( | volatile LevelMutexInfo & | mutex,
|  | bool | lock = `true` |
| ) |  |  | `[explicit]`

Creates an object to lock an unlock a mutex for a function. This will throw if an attempt to lock the mutex fails.

Parameters:

  | mutex | Reference to the mutex.
  | lock | True if function wants to lock the mutex as this gets constructed.

References Loki::LevelMutexInfo::GetLevel(), and Loki::LevelMutexInfo::Lock().

Here is the call graph for this function:

Loki::MutexLocker::MutexLocker | ( | volatile LevelMutexInfo & | mutex,
|  | unsigned int | milliSeconds,
|  | bool | lock = `true` |
| ) |  |  |

Creates an object to lock an unlock a mutex for a function. This waits a specified amount of time for another thread to unlock the mutex if it is locked. This will throw if an attempt to lock the mutex fails.

Parameters:

  | mutex | Reference to the mutex.
  | milliSeconds | Amount of time to wait for another thread to unlock the mutex.
  | lock | True if function wants to lock the mutex as this gets constructed.

References Loki::LevelMutexInfo::GetLevel(), and Loki::LevelMutexInfo::Lock().

Here is the call graph for this function:

## Member Function Documentation

bool Loki::MutexLocker::Lock | ( | void |  | ) |

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

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

References Loki::LevelMutexInfo::Lock().

Here is the call graph for this function:

bool Loki::MutexLocker::Unlock | ( | void |  | ) |

You can call this to unlock a mutex 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::Unlock().

Here is the call graph for this function:

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

- LevelMutex.h
- LevelMutex.cpp