Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::Atomic< T > Class Template Reference

A variable with atomic semantics and standalone atomic operations. More...

#include <atomic.h>

+ Collaboration diagram for lunchbox::Atomic< T >:

Public Member Functions

 Atomic (const T v=0)
 Construct a new atomic variable with an initial value. More...
 
 Atomic (const Atomic< T > &v)
 Construct a copy of an atomic variable. More...
 
 operator T (void) const
 
void operator= (const T v)
 Assign a new value. More...
 
void operator= (const Atomic< T > &v)
 Assign a new value. More...
 
operator+= (T v)
 Atomically add a value and return the new value. More...
 
operator-= (T v)
 Atomically substract a value and return the new value. More...
 
operator++ (void)
 Atomically increment by one and return the new value. More...
 
operator-- (void)
 Atomically decrement by one and return the new value. More...
 
operator++ (int)
 Atomically increment by one and return the old value. More...
 
operator-- (int)
 Atomically decrement by one and return the old value. More...
 
bool operator== (const Atomic< T > &rhs) const
 
bool operator!= (const Atomic< T > &rhs) const
 
bool compareAndSwap (const T expected, const T newValue)
 Perform a compare-and-swap atomic operation. More...
 

Static Public Member Functions

static T getAndAdd (T &value, const T increment)
 
static T getAndSub (T &value, const T increment)
 
static T addAndGet (T &value, const T increment)
 
static T subAndGet (T &value, const T increment)
 
static T incAndGet (T &value)
 
static T decAndGet (T &value)
 
static bool compareAndSwap (T *value, const T expected, const T newValue)
 Perform a compare-and-swap atomic operation. More...
 

Detailed Description

template<class T>
class lunchbox::Atomic< T >

A variable with atomic semantics and standalone atomic operations.

Use the C++11 equivalent if you can.

Atomic variables can be modified safely from multiple threads concurrently. They are useful to implement lock-free algorithms.

For implementation reasons, only signed atomic variables are supported, of which int32_t and ssize_t are implemented and typedef'd as a_int32_t and a_ssize_t.

Definition at line 87 of file atomic.h.

Constructor & Destructor Documentation

template<class T>
lunchbox::Atomic< T >::Atomic ( const T  v = 0)
explicit

Construct a new atomic variable with an initial value.

Version
1.0

Definition at line 328 of file atomic.h.

template<class T>
lunchbox::Atomic< T >::Atomic ( const Atomic< T > &  v)

Construct a copy of an atomic variable.

Not thread-safe!

Version
1.0

Definition at line 334 of file atomic.h.

Member Function Documentation

template<class T>
T lunchbox::Atomic< T >::addAndGet ( T &  value,
const T  increment 
)
static
Returns
the new value after adding the given increment.

Definition at line 283 of file atomic.h.

References lunchbox::memoryBarrierAcquire(), and lunchbox::memoryBarrierRelease().

Referenced by lunchbox::Atomic< T >::incAndGet(), and lunchbox::Atomic< T >::operator+=().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
static bool lunchbox::Atomic< T >::compareAndSwap ( T *  value,
const T  expected,
const T  newValue 
)
static

Perform a compare-and-swap atomic operation.

Referenced by lunchbox::Atomic< T >::compareAndSwap(), lunchbox::Atomic< T >::getAndAdd(), and lunchbox::Atomic< T >::getAndSub().

+ Here is the caller graph for this function:

template<class T>
bool lunchbox::Atomic< T >::compareAndSwap ( const T  expected,
const T  newValue 
)

Perform a compare-and-swap atomic operation.

Atomically replaces the value and return true if the value matched the expected.

Returns
true if the new value was set, false otherwise
Version
1.1.2

Definition at line 411 of file atomic.h.

References lunchbox::Atomic< T >::compareAndSwap().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Atomic< T >::decAndGet ( T &  value)
static
Returns
the new value after decrementing the value.

Definition at line 321 of file atomic.h.

References lunchbox::Atomic< T >::subAndGet().

Referenced by lunchbox::Atomic< T >::operator--().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
T lunchbox::Atomic< T >::getAndAdd ( T &  value,
const T  increment 
)
static
Returns
the old value, then add the given increment.

Definition at line 251 of file atomic.h.

References lunchbox::Atomic< T >::compareAndSwap(), lunchbox::memoryBarrierAcquire(), and lunchbox::memoryBarrierRelease().

Referenced by lunchbox::Atomic< T >::operator++().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
T lunchbox::Atomic< T >::getAndSub ( T &  value,
const T  increment 
)
static
Returns
the old value, then substract the increment.

Definition at line 267 of file atomic.h.

References lunchbox::Atomic< T >::compareAndSwap(), lunchbox::memoryBarrierAcquire(), and lunchbox::memoryBarrierRelease().

Referenced by lunchbox::Atomic< T >::operator--().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
T lunchbox::Atomic< T >::incAndGet ( T &  value)
static
Returns
the new value after incrementing the value.

Definition at line 315 of file atomic.h.

References lunchbox::Atomic< T >::addAndGet().

Referenced by lunchbox::Atomic< T >::operator++().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
lunchbox::Atomic< T >::operator T ( void  ) const
Returns
the current value
Version
1.0

Definition at line 340 of file atomic.h.

References lunchbox::memoryBarrierAcquire().

+ Here is the call graph for this function:

template<class T>
bool lunchbox::Atomic< T >::operator!= ( const Atomic< T > &  rhs) const
Returns
true if the variable has not the given value.
Version
1.1.2

Definition at line 404 of file atomic.h.

References lunchbox::memoryBarrier().

+ Here is the call graph for this function:

template<class T >
T lunchbox::Atomic< T >::operator++ ( void  )

Atomically increment by one and return the new value.

Version
1.0

Definition at line 373 of file atomic.h.

References lunchbox::Atomic< T >::incAndGet().

+ Here is the call graph for this function:

template<class T >
T lunchbox::Atomic< T >::operator++ ( int  )

Atomically increment by one and return the old value.

Version
1.0

Definition at line 385 of file atomic.h.

References lunchbox::Atomic< T >::getAndAdd().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Atomic< T >::operator+= ( v)

Atomically add a value and return the new value.

Version
1.0

Definition at line 361 of file atomic.h.

References lunchbox::Atomic< T >::addAndGet().

+ Here is the call graph for this function:

template<class T >
T lunchbox::Atomic< T >::operator-- ( void  )

Atomically decrement by one and return the new value.

Version
1.0

Definition at line 379 of file atomic.h.

References lunchbox::Atomic< T >::decAndGet().

+ Here is the call graph for this function:

template<class T >
T lunchbox::Atomic< T >::operator-- ( int  )

Atomically decrement by one and return the old value.

Version
1.0

Definition at line 391 of file atomic.h.

References lunchbox::Atomic< T >::getAndSub().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Atomic< T >::operator-= ( v)

Atomically substract a value and return the new value.

Version
1.0

Definition at line 367 of file atomic.h.

References lunchbox::Atomic< T >::subAndGet().

+ Here is the call graph for this function:

template<class T>
void lunchbox::Atomic< T >::operator= ( const T  v)

Assign a new value.

Version
1.0

Definition at line 347 of file atomic.h.

References lunchbox::memoryBarrier().

+ Here is the call graph for this function:

template<class T>
void lunchbox::Atomic< T >::operator= ( const Atomic< T > &  v)

Assign a new value.

Not thread-safe!

Version
1.0

Definition at line 354 of file atomic.h.

References lunchbox::memoryBarrier().

+ Here is the call graph for this function:

template<class T>
bool lunchbox::Atomic< T >::operator== ( const Atomic< T > &  rhs) const
Returns
true if the variable has the given value.
Version
1.1.2

Definition at line 397 of file atomic.h.

References lunchbox::memoryBarrier().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Atomic< T >::subAndGet ( T &  value,
const T  increment 
)
static
Returns
the new value after substracting the increment.

Definition at line 299 of file atomic.h.

References lunchbox::memoryBarrierAcquire(), and lunchbox::memoryBarrierRelease().

Referenced by lunchbox::Atomic< T >::decAndGet(), and lunchbox::Atomic< T >::operator-=().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:


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