You.i Engine
CYISpinLock 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 CYIMutex, the CYISpinLock 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 CYISpinLock keeps the thread alive, thus eliminating the thread re-scheduling overhead that is typically observed with CYIMutex. Because of the nature of a spin-lock, it is very advisable to only use a CYISpinLock for very short period of time. Avoid using CYISpinLock on single-core systems, as they add worst performance than CYIMutex. Used correctly, a CYISpinLock will give you much better performance than a regular CYIMutex.

Correct use of a CYISpinLock:

CYISpinLock m_spinLock;
int32_t m_nValue = 6;
float m_fValue = 3.5f;
void GoodExample1()
{
m_spinLock.Lock();
m_nValue *= 5;
m_fValue /= 4.f;
m_spinLock.Unlock();
}
void GoodExample2()
{
m_spinLock.Lock();
m_nValue *= 3;
m_fValue /= 2.f;
m_spinLock.Unlock();
}

Incorrect use of a CYISpinLock:

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

#include <thread/YiSpinLock.h>

Public Member Functions

 CYISpinLock ()
 
bool Lock ()
 
bool TryLock ()
 
bool Unlock ()
 

Constructor & Destructor Documentation

CYISpinLock::CYISpinLock ( )
inline

Member Function Documentation

bool CYISpinLock::Lock ( )
inline

Locks the CYISpinLock.

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

bool CYISpinLock::TryLock ( )
inline

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.

bool CYISpinLock::Unlock ( )
inline

Unlocks the CYISpinLock.


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