cds  2.3.2
cds::sync::spin_lock< Backoff > Class Template Reference

Spin lock. More...

#include <cds/sync/spinlock.h>

Public Types

typedef Backoff backoff_strategy
 back-off strategy type
 

Public Member Functions

 spin_lock () noexcept
 Construct free (unlocked) spin-lock.
 
 spin_lock (bool bLocked) noexcept
 Construct spin-lock in specified state. More...
 
 spin_lock (const spin_lock< Backoff > &) noexcept
 Dummy copy constructor. More...
 
 ~spin_lock ()
 Destructor. On debug time it checks whether spin-lock is free.
 
bool is_locked () const noexcept
 Check if the spin is locked.
 
bool try_lock () noexcept
 Try to lock the object. More...
 
bool try_lock (unsigned int nTryCount) noexcept(noexcept(backoff_strategy()()))
 Try to lock the object, repeat nTryCount times if failed. More...
 
void lock () noexcept(noexcept(backoff_strategy()()))
 Lock the spin-lock. Waits infinitely while spin-lock is locked. Debug version: deadlock may be detected.
 
void unlock () noexcept
 Unlock the spin-lock. Debug version: deadlock may be detected.
 

Private Attributes

atomics::atomic< bool > m_spin
 Spin.
 

Detailed Description

template<typename Backoff>
class cds::sync::spin_lock< Backoff >

Spin lock.

Simple and light-weight spin-lock critical section It is useful to gain access to small (short-timed) code

Algorithm:

TATAS (test-and-test-and-lock) [1984] L. Rudolph, Z. Segall. Dynamic Decentralized Cache Schemes for MIMD Parallel Processors.

No serialization performed - any of waiting threads may owns the spin-lock. This spin-lock is NOT recursive: the thread owned the lock cannot call lock() method without deadlock. The method unlock() can call any thread

DEBUG version: The spinlock stores owner thead id. Assertion is raised when:

  • double lock attempt encountered by same thread (deadlock)
  • unlock by another thread

If spin-lock is locked the Backoff algorithm is called. Predefined backoff::LockDefault class yields current thread and repeats lock attempts later

Template parameters:

  • Backoff - backoff strategy. Used when spin lock is locked

Constructor & Destructor Documentation

◆ spin_lock() [1/2]

template<typename Backoff >
cds::sync::spin_lock< Backoff >::spin_lock ( bool  bLocked)
inlineexplicitnoexcept

Construct spin-lock in specified state.

In debug mode: if bLocked = true then spin-lock is made owned by current thread

◆ spin_lock() [2/2]

template<typename Backoff >
cds::sync::spin_lock< Backoff >::spin_lock ( const spin_lock< Backoff > &  )
inlinenoexcept

Dummy copy constructor.

The ctor initializes the spin to free (unlocked) state like the default ctor.

Member Function Documentation

◆ try_lock() [1/2]

template<typename Backoff >
bool cds::sync::spin_lock< Backoff >::try_lock ( )
inlinenoexcept

Try to lock the object.

Returns true if locking is succeeded otherwise (if the spin is already locked) returns false

Debug version: deadlock can be detected

◆ try_lock() [2/2]

template<typename Backoff >
bool cds::sync::spin_lock< Backoff >::try_lock ( unsigned int  nTryCount)
inlinenoexcept

Try to lock the object, repeat nTryCount times if failed.

Returns true if locking is succeeded otherwise (if the spin is already locked) returns false


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

cds 2.3.2 Developed by Maxim Khizhinsky aka khizmax and other contributors 2007 - 2017
Autogenerated Sun Dec 31 2017 12:10:54 by Doxygen 1.8.13