CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::SpinLock Class Reference

Spin-lock synchronization primitive. More...

#include <spin_lock.h>

Public Member Functions

 SpinLock () noexcept
 
 SpinLock (const SpinLock &)=delete
 
 SpinLock (SpinLock &&)=delete
 
 ~SpinLock ()=default
 
SpinLockoperator= (const SpinLock &)=delete
 
SpinLockoperator= (SpinLock &&)=delete
 
bool IsLocked () noexcept
 Is already locked? More...
 
bool TryLock () noexcept
 Try to acquire spin-lock without block. More...
 
bool TryLockSpin (int64_t spin) noexcept
 Try to acquire spin-lock for the given spin count. More...
 
bool TryLockFor (const Timespan &timespan) noexcept
 Try to acquire spin-lock for the given timespan. More...
 
bool TryLockUntil (const UtcTimestamp &timestamp) noexcept
 Try to acquire spin-lock until the given timestamp. More...
 
void Lock () noexcept
 Acquire spin-lock with block. More...
 
void Unlock () noexcept
 Release spin-lock. More...
 

Detailed Description

Spin-lock synchronization primitive.

The purpose of a spin lock is to prevent multiple threads from concurrently accessing a shared data structure. In contrast to a mutex, threads will busy-wait and waste CPU cycles instead of yielding the CPU to another thread. Do not use spinlocks unless you are certain that you understand the consequences!

Thread-safe.

https://en.wikipedia.org/wiki/Spinlock

Examples
threads_spin_lock.cpp.

Definition at line 29 of file spin_lock.h.

Constructor & Destructor Documentation

◆ SpinLock() [1/3]

CppCommon::SpinLock::SpinLock ( )
inlinenoexcept

Definition at line 32 of file spin_lock.h.

◆ SpinLock() [2/3]

CppCommon::SpinLock::SpinLock ( const SpinLock )
delete

◆ SpinLock() [3/3]

CppCommon::SpinLock::SpinLock ( SpinLock &&  )
delete

◆ ~SpinLock()

CppCommon::SpinLock::~SpinLock ( )
default

Member Function Documentation

◆ IsLocked()

bool CppCommon::SpinLock::IsLocked ( )
inlinenoexcept

Is already locked?

Will not block.

Returns
'true' if the spin-lock is already locked, 'false' if the spin-lock is released

Definition at line 11 of file spin_lock.inl.

◆ Lock()

void CppCommon::SpinLock::Lock ( )
inlinenoexcept

Acquire spin-lock with block.

Will block in a spin loop.

Definition at line 50 of file spin_lock.inl.

◆ operator=() [1/2]

SpinLock& CppCommon::SpinLock::operator= ( const SpinLock )
delete

◆ operator=() [2/2]

SpinLock& CppCommon::SpinLock::operator= ( SpinLock &&  )
delete

◆ TryLock()

bool CppCommon::SpinLock::TryLock ( )
inlinenoexcept

Try to acquire spin-lock without block.

Will not block.

Returns
'true' if the spin-lock was successfully acquired, 'false' if the spin-lock is busy

Definition at line 16 of file spin_lock.inl.

◆ TryLockFor()

bool CppCommon::SpinLock::TryLockFor ( const Timespan timespan)
inlinenoexcept

Try to acquire spin-lock for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the spin-lock
Returns
'true' if the spin-lock was successfully acquired, 'false' if the spin-lock is busy

Definition at line 34 of file spin_lock.inl.

◆ TryLockSpin()

bool CppCommon::SpinLock::TryLockSpin ( int64_t  spin)
inlinenoexcept

Try to acquire spin-lock for the given spin count.

Will block for the given spin count in the worst case.

Parameters
spin- Spin count
Returns
'true' if the spin-lock was successfully acquired, 'false' if the spin-lock is busy

Definition at line 21 of file spin_lock.inl.

◆ TryLockUntil()

bool CppCommon::SpinLock::TryLockUntil ( const UtcTimestamp timestamp)
inlinenoexcept

Try to acquire spin-lock until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the spin-lock
Returns
'true' if the spin-lock was successfully acquired, 'false' if the spin-lock is busy

Definition at line 80 of file spin_lock.h.

◆ Unlock()

void CppCommon::SpinLock::Unlock ( )
inlinenoexcept

Release spin-lock.

Will not block.

Definition at line 55 of file spin_lock.inl.


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