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:
Incorrect use of a CYISpinLock:
#include <thread/YiSpinLock.h>
Public Member Functions | |
| CYISpinLock () | |
| bool | Lock () |
| bool | TryLock () |
| bool | Unlock () |
|
inline |
|
inline |
Locks the CYISpinLock.
If another thread has already locked the CYISpinLock, then it will loop (spin) until it is unlocked.
|
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.
|
inline |
Unlocks the CYISpinLock.