All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::Guard Class Reference

A guard class. More...

#include <Radiant/Mutex.hpp>

Inheritance diagram for Radiant::Guard:
Patterns::NotCopyable

Public Member Functions

 Guard (Mutex &mutex)
 Construct guard. More...
 
 ~Guard ()
 Destructor, unlocks the associated mutex.
 

Detailed Description

A guard class.

This class is used to automatically lock and unlock a mutex within some function. This is useful when trying to avoid situations where there are several "return"-statements in a function, and one easily forget to unlock the mutex that one is using.

    int MyClass::doSomething()
    {
Mutex is locked here:
    Guarg g(mutex());
Mutex is freed reliably on each return path
    if(foo())
    return 0;
    if(fee())
    return 1;
    return 2;
    }
    
See Also
ReleaseGuard

Constructor & Destructor Documentation

Radiant::Guard::Guard ( Mutex mutex)
inline

Construct guard.

Locks the given mutex.

Parameters
mutexmutex to guard