OGRE-Next  4.0.0unstable
Object-Oriented Graphics Rendering Engine
Ogre::LightweightMutex Class Reference

A lightweight mutex is a synchronization mechanism, very similar to a regular mutex. More...

#include <OgreLightweightMutex.h>

Public Member Functions

 LightweightMutex ()
 
 ~LightweightMutex ()
 
void lock ()
 Acquires the exclusive lock. More...
 
bool tryLock ()
 Tries to aquire the lock and returns immediately. More...
 
void unlock ()
 Releases the lock aquired through either. More...
 

Detailed Description

A lightweight mutex is a synchronization mechanism, very similar to a regular mutex.

Regular mutexes are well known to be expensive because they need to enter & leave kernel mode.

Lightweight mutexes usually make use of atomic instructions (in x86 they're referred to as instructions with the "lock" prefix) to avoid entering kernel mode when no other thread has aquired the lock.
This comes with a couple caveats that we don't care for the applications we need, but is important to keep them in mind: 1) LightweightMutexes can't be shared among processes 2) Priority inversion is not a concern 3) Recursive locks aren't supported (same thread calling lock() twice can deadlock) Note that some LightweightMutex implementations may offer this functionality, but we don't guarantee them in all platforms/architectures. It is possible to write a lightweight mutex that supports recursive locks, but that requires a call to GetCurrentThreadId (in Windows), which as much as saying just use a regular mutex.
Windows users are familiar with the concept of LightweightMutexes because there is already an implementation provided: CRITICAL_SECTION. We go further by reinventing the wheel and writting it ourselves. If you ever wondered how a CRITICAL_SECTION works, now you know.
Interesting reads: http://preshing.com/20111124/always-use-a-lightweight-mutex http://preshing.com/20120226/roll-your-own-lightweight-mutex http://preshing.com/20120305/implementing-a-recursive-mutex

Constructor & Destructor Documentation

◆ LightweightMutex()

Ogre::LightweightMutex::LightweightMutex ( )

◆ ~LightweightMutex()

Ogre::LightweightMutex::~LightweightMutex ( )

Member Function Documentation

◆ lock()

void Ogre::LightweightMutex::lock ( )

Acquires the exclusive lock.

Waits if necessary until another thread releases the lock. Recursive locking is not guaranteed (do not call this function twice from the same thread)

◆ tryLock()

bool Ogre::LightweightMutex::tryLock ( )

Tries to aquire the lock and returns immediately.

On failure returns false, true on success

◆ unlock()

void Ogre::LightweightMutex::unlock ( )

Releases the lock aquired through either.

See also
lock or
tryLock

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