![]() |
ezEngine Release 26.3
|
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. | |
| ezAtomicInteger & | operator= (T value) |
| Assigns a new integer value to this object. | |
| ezAtomicInteger & | operator= (const ezAtomicInteger &value) |
| Assignment operator. | |
| T | Increment () |
| Increments the internal value and returns the incremented value. | |
| T | Decrement () |
| Decrements the internal value and returns the decremented value. | |
| T | PostIncrement () |
| Increments the internal value and returns the value immediately before the increment. | |
| T | 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) |
| T | 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. | |
| T | CompareAndSwap (T expected, T x) |
| Atomic compare-and-swap operation returning the previous value. | |
| operator T () const | |
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.
| EZ_ALWAYS_INLINE T ezAtomicInteger< T >::CompareAndSwap | ( | T | expected, |
| T | 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.
| EZ_ALWAYS_INLINE bool ezAtomicInteger< T >::TestAndSet | ( | T | expected, |
| T | 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.