You.i Engine
CYIRecursiveSpinLock Class Reference

Detailed Description

Provides access serialization between threads, where a thread trying to acquire the lock waits in a loop repeatedly checking until the lock becomes available.

Similarly to the std::recursive_mutex, the CYIRecursiveSpinLock will achieve serialized access to a shared resource. Instead of asking the operating system to de-schedule a thread on a locked resource and then re-schedule the thread upon unlocking the shared resource, a CYIRecursiveSpinLock keeps the thread alive, thus eliminating the thread re-scheduling overhead that is typically observed with std::recursive_mutex. Because of the nature of a spin-lock, it is very advisable to only use a CYIRecursiveSpinLock for very short periods of time. Avoid using CYIRecursiveSpinLock on single-core systems, as they incur additional performance overhead when compared to std::recursive_mutex. Used correctly, a CYIRecursiveSpinLock will give you much better performance than a regular std::recursive_mutex.

Unlike CYISpinLock, instances of this class can be locked multiple times by the same thread.

Correct use of a CYIRecursiveSpinLock:

int32_t m_intValue = 6;
float m_floatValue = 3.5f;
void GoodExample()
{
m_spinLock.Lock();
m_intValue *= 5;
m_floatValue /= 4.f;
m_spinLock.Unlock();
}

Incorrect use of a CYIRecursiveSpinLock:

int m_array[ARRAY_UNKNOWN_SIZE];
void BadExample()
{
m_spinLock.Lock();
for (int32_t i = 0; i < ARRAY_UNKNOWN_SIZE; ++i) // That could be too long for a CYIRecursiveSpinLock. You should consider using std::recursive_mutex instead
{
m_array[i] *= 3;
}
m_spinLock.Unlock();
}
See also
CYISpinLock

#include <thread/YiRecursiveSpinLock.h>

Public Member Functions

 CYIRecursiveSpinLock ()
 
void Lock ()
 
bool TryLock ()
 
void Unlock ()
 
void lock ()
 
bool try_lock ()
 
void unlock ()
 

Constructor & Destructor Documentation

◆ CYIRecursiveSpinLock()

CYIRecursiveSpinLock::CYIRecursiveSpinLock ( )

Member Function Documentation

◆ Lock()

void CYIRecursiveSpinLock::Lock ( )

Locks the CYISpinLock.

If another thread has already locked the CYISpinLock, then it will loop (spin) until it is unlocked.

◆ lock()

void CYIRecursiveSpinLock::lock ( )

Locks the CYISpinLock.

If another thread has already locked the CYISpinLock, then it will loop (spin) until it is unlocked.

◆ try_lock()

bool CYIRecursiveSpinLock::try_lock ( )

Attempts to lock the CYISpinLock.

If another thread has already locked the CYISpinLock, this function will return false. Otherwise if the lock was successfully acquired, it will return true.

◆ TryLock()

bool CYIRecursiveSpinLock::TryLock ( )

Attempts to lock the CYISpinLock.

If another thread has already locked the CYISpinLock, this function will return false. Otherwise if the lock was successfully acquired, it will return true.

◆ Unlock()

void CYIRecursiveSpinLock::Unlock ( )

Unlocks the CYISpinLock.

◆ unlock()

void CYIRecursiveSpinLock::unlock ( )

Unlocks the CYISpinLock.


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