Base Framework
Public Types | Public Member Functions | List of all members
AtomicCounter< TYPE > Class Template Reference

Atomic counter. More...

#include <base/concurrency/AtomicCounter.h>

Public Types

enum  { DEFAULT_VALUE = 0, DESTRUCT_VALUE = -1 }
 

Public Member Functions

 AtomicCounter (const TYPE _value=DEFAULT_VALUE) noexcept
 
 AtomicCounter (const AtomicCounter &_value) noexcept
 
AtomicCounteroperator= (const AtomicCounter &_value) noexcept
 
 operator TYPE () const noexcept
 
TYPE operator++ (int) noexcept
 
TYPE operator++ () noexcept
 
TYPE operator-- (int) noexcept
 
TYPE operator-- () noexcept
 
TYPE operator+= (const TYPE _value) noexcept
 
TYPE operator-= (const TYPE _value) noexcept
 
AtomicCounteroperator= (const TYPE _value) noexcept
 
bool operator== (const TYPE _value) const noexcept
 
bool operator!= (const TYPE _value) const noexcept
 
TYPE exchange (const TYPE desired) noexcept
 
bool compareAndExchange (TYPE &expected, const TYPE desired) noexcept
 
bool compareAndExchangeWeak (TYPE &expected, const TYPE desired) noexcept
 

Detailed Description

template<class TYPE>
class AtomicCounter< TYPE >

Atomic counter.

MT-safe counter. Try to avoid atomic load() if result is already available from previous operation. It is recommended to use signed types to make it easier to detect problems around 0.

Make sure to use common alignment for the given type.

See also
SpinLock
Version
1.0

Constructor & Destructor Documentation

◆ AtomicCounter() [1/2]

template<class TYPE >
AtomicCounter< TYPE >::AtomicCounter ( const TYPE  _value = DEFAULT_VALUE)
inlinenoexcept

Initializes the counter with 0.

Parameters
valueThe initial value.

◆ AtomicCounter() [2/2]

template<class TYPE >
AtomicCounter< TYPE >::AtomicCounter ( const AtomicCounter< TYPE > &  _value)
inlinenoexcept

Copy constructor. Avoid this.

Member Function Documentation

◆ compareAndExchange()

template<class TYPE >
bool AtomicCounter< TYPE >::compareAndExchange ( TYPE &  expected,
const TYPE  desired 
)
inlinenoexcept

Strong compare and exchange.

◆ compareAndExchangeWeak()

template<class TYPE >
bool AtomicCounter< TYPE >::compareAndExchangeWeak ( TYPE &  expected,
const TYPE  desired 
)
inlinenoexcept

Weak compare and exchange. Prefer strong if unsure.

◆ exchange()

template<class TYPE >
TYPE AtomicCounter< TYPE >::exchange ( const TYPE  desired)
inlinenoexcept

Exchange the value with the given value.

◆ operator TYPE()

template<class TYPE >
AtomicCounter< TYPE >::operator TYPE ( ) const
inlinenoexcept

Returns the value of the counter.

◆ operator!=()

template<class TYPE >
bool AtomicCounter< TYPE >::operator!= ( const TYPE  _value) const
inlinenoexcept

Returns true if not equal to value.

◆ operator++() [1/2]

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator++ ( )
inlinenoexcept

Pre-increments the counter.

◆ operator++() [2/2]

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator++ ( int  )
inlinenoexcept

Post-increments the counter. Prefer pre-increment.

◆ operator+=()

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator+= ( const TYPE  _value)
inlinenoexcept

Increment counter by value.

◆ operator--() [1/2]

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator-- ( )
inlinenoexcept

Pre-decrements the counter.

◆ operator--() [2/2]

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator-- ( int  )
inlinenoexcept

Post-decrements the counter. Prefer pre-decrement.

◆ operator-=()

template<class TYPE >
TYPE AtomicCounter< TYPE >::operator-= ( const TYPE  _value)
inlinenoexcept

Decrement counter by value.

◆ operator=() [1/2]

template<class TYPE >
AtomicCounter& AtomicCounter< TYPE >::operator= ( const AtomicCounter< TYPE > &  _value)
inlinenoexcept

Assignment. Avoid this.

◆ operator=() [2/2]

template<class TYPE >
AtomicCounter& AtomicCounter< TYPE >::operator= ( const TYPE  _value)
inlinenoexcept

Assign value to counter.

◆ operator==()

template<class TYPE >
bool AtomicCounter< TYPE >::operator== ( const TYPE  _value) const
inlinenoexcept

Returns true if equal to value.