ezEngine Release 26.3
Loading...
Searching...
No Matches
ezAtomicInteger< T > Class Template Reference

Thread-safe atomic integer with lock-free operations. More...

#include <AtomicInteger.h>

Public Member Functions

 EZ_DECLARE_POD_TYPE ()
 
 ezAtomicInteger ()
 Initializes the value to zero.
 
 ezAtomicInteger (const T value)
 Initializes the object with a value.
 
 ezAtomicInteger (const ezAtomicInteger< T > &value)
 Copy-constructor.
 
ezAtomicIntegeroperator= (T value)
 Assigns a new integer value to this object.
 
ezAtomicIntegeroperator= (const ezAtomicInteger &value)
 Assignment operator.
 
Increment ()
 Increments the internal value and returns the incremented value.
 
Decrement ()
 Decrements the internal value and returns the decremented value.
 
PostIncrement ()
 Increments the internal value and returns the value immediately before the increment.
 
PostDecrement ()
 Decrements the internal value and returns the value immediately before the decrement.
 
void Add (T x)
 
void Subtract (T x)
 
void And (T x)
 
void Or (T x)
 
void Xor (T x)
 
void Min (T x)
 
void Max (T x)
 
Set (T x)
 Sets the internal value to x and returns the original internal value.
 
bool TestAndSet (T expected, T x)
 Atomic conditional assignment operation.
 
CompareAndSwap (T expected, T x)
 Atomic compare-and-swap operation returning the previous value.
 
 operator T () const
 

Detailed Description

template<typename T>
class ezAtomicInteger< T >

Thread-safe atomic integer with lock-free operations.

Provides atomic (thread-safe) operations on integer types without requiring explicit locking. All operations are lock-free and use hardware atomic instructions where available. Supports common atomic operations like increment, decrement, compare-and-swap, and bitwise operations. The class is templated to work with various integer types while ensuring proper atomic alignment. Use ezAtomicInteger32 or ezAtomicInteger64 typedefs for common cases.

Member Function Documentation

◆ CompareAndSwap()

template<typename T >
EZ_ALWAYS_INLINE T ezAtomicInteger< T >::CompareAndSwap ( expected,
x 
)

Atomic compare-and-swap operation returning the previous value.

If the current value equals expected, it is atomically replaced with x. Always returns the value that was present before the operation, regardless of whether the swap occurred. This enables retry loops in lock-free algorithms.

◆ TestAndSet()

template<typename T >
EZ_ALWAYS_INLINE bool ezAtomicInteger< T >::TestAndSet ( expected,
x 
)

Atomic conditional assignment operation.

Sets the value to x only if the current value equals expected. Returns true if the assignment occurred, false otherwise. This is a fundamental building block for lock-free algorithms.


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