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::mutex, 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 std::mutex. Because of the nature of a spin-lock, it is very advisable to only use a CYISpinLock for very short periods of time. Avoid using CYISpinLock on single-core systems, as they add worst performance than std::mutex. Used correctly, a CYISpinLock will give you much better performance than a regular std::mutex.
Correct use of a CYISpinLock:
Incorrect use of a CYISpinLock:
#include <thread/YiSpinLock.h>
Public Member Functions | |
CYISpinLock () | |
void | Lock () |
bool | TryLock () |
void | Unlock () |
void | lock () |
bool | try_lock () |
void | unlock () |
CYISpinLock::CYISpinLock | ( | ) |
void CYISpinLock::Lock | ( | ) |
Locks the CYISpinLock.
If another thread has already locked the CYISpinLock, then it will loop (spin) until it is unlocked.
void CYISpinLock::lock | ( | ) |
Locks the CYISpinLock.
If another thread has already locked the CYISpinLock, then it will loop (spin) until it is unlocked.
bool CYISpinLock::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.
bool CYISpinLock::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.
void CYISpinLock::Unlock | ( | ) |
Unlocks the CYISpinLock.
void CYISpinLock::unlock | ( | ) |
Unlocks the CYISpinLock.