31 namespace AtomicHelpers
33 template <
typename T>
struct DiffTypeHelper {
using Type = T; };
34 template <
typename T>
struct DiffTypeHelper<T*> {
using Type = std::ptrdiff_t; };
44 template <
typename Type>
47 using DiffType =
typename AtomicHelpers::DiffTypeHelper<Type>::Type;
50 Atomic() noexcept : value (Type()) {}
53 Atomic (Type initialValue) noexcept : value (initialValue) {}
61 #if __cpp_lib_atomic_is_always_lock_free 62 static_assert (std::atomic<Type>::is_always_lock_free,
63 "This class can only be used for lock-free types");
68 Type
get()
const noexcept {
return value.load(); }
71 void set (Type newValue) noexcept { value = newValue; }
74 Type
exchange (Type newValue) noexcept {
return value.exchange (newValue); }
102 return value.compare_exchange_strong (valueToCompare, newValue);
108 value = other.
value.load();
120 Type operator+= (DiffType amountToAdd) noexcept {
return value += amountToAdd; }
123 Type operator-= (DiffType amountToSubtract) noexcept {
return value -= amountToSubtract; }
136 void memoryBarrier() noexcept { atomic_thread_fence (std::memory_order_seq_cst); }
146 JUCE_DEPRECATED (Type compareAndSetValue (Type, Type) noexcept);
A simple wrapper around std::atomic.
Atomic(Type initialValue) noexcept
Creates a new value, with a given initial value.
Type operator--() noexcept
Atomically decrements this value, returning the new value.
Atomic(const Atomic &other) noexcept
Copies another value (atomically).
std::atomic< Type > value
The std::atomic object that this class operates on.
Type exchange(Type newValue) noexcept
Atomically sets the current value, returning the value that was replaced.
void memoryBarrier() noexcept
Implements a memory read/write barrier.
Atomic() noexcept
Creates a new value, initialised to zero.
bool compareAndSetBool(Type newValue, Type valueToCompare) noexcept
Atomically compares this value with a target value, and if it is equal, sets this to be equal to a ne...
~Atomic() noexcept
Destructor.
Type operator++() noexcept
Atomically increments this value, returning the new value.