QAtomicInteger Class
template <typename T> class QAtomicInteger
The QAtomicInteger class provides platform-independent atomic operations on integers. More…
| Header | #include <QAtomicInteger> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
Static Public Members
| bool | isReferenceCountingNative() |
| bool | isReferenceCountingWaitFree() |
| bool | isTestAndSetNative() |
| bool | isTestAndSetWaitFree() |
| bool | isFetchAndStoreNative() |
| bool | isFetchAndStoreWaitFree() |
| bool | isFetchAndAddNative() |
| bool | isFetchAndAddWaitFree() |
Detailed Description
QAtomicInteger<T> wraps a std::atomic<T> and offers the Qt-style load, store, compare-and-swap and fetch-and-modify operations. Functions with Ordered in their name use sequentially consistent ordering; those with Relaxed, Acquire or Release use the corresponding memory order. Plain load(), store(), the conversion operator and the arithmetic operators are sequentially consistent.
QAtomicInt is the common instantiation for int, and is typically used as a thread-safe reference count:
QAtomicInt refCount(1);
void retain() { refCount.ref(); }
void release() { if (!refCount.deref()) delete this; }
See also QAtomicPointer.
Differences from Qt
The is...Native() and is...WaitFree() queries report whether std::atomic<T> is always lock-free, rather than the processor's instructions.
Member Function Documentation
QAtomicInteger::QAtomicInteger(T v = 0)
Constructs an atomic integer holding v.
QAtomicInteger::QAtomicInteger(const QAtomicInteger &o)
Constructs a copy of o. The value is read with relaxed ordering; the copy is not atomic with respect to other threads.
bool QAtomicInteger::deref()
Atomically decrements the value by one. Returns true if the new value is not zero; otherwise returns false.
See also ref().
T QAtomicInteger::fetchAndAddAcquire(T v)
Atomically adds v to the value, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAddOrdered(T v)
Atomically adds v to the value, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAddRelaxed(T v)
Atomically adds v to the value, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAddRelease(T v)
Atomically adds v to the value, using release memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAndAcquire(T v)
Atomically performs a bitwise AND of the value with v, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAndOrdered(T v)
Atomically performs a bitwise AND of the value with v, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAndRelaxed(T v)
Atomically performs a bitwise AND of the value with v, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndAndRelease(T v)
Atomically performs a bitwise AND of the value with v, using release memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndOrAcquire(T v)
Atomically performs a bitwise OR of the value with v, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndOrOrdered(T v)
Atomically performs a bitwise OR of the value with v, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndOrRelaxed(T v)
Atomically performs a bitwise OR of the value with v, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndOrRelease(T v)
Atomically performs a bitwise OR of the value with v, using release memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndStoreAcquire(T v)
Atomically replaces the value with v, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndStoreOrdered(T v)
Atomically replaces the value with v, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndStoreRelaxed(T v)
Atomically replaces the value with v, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndStoreRelease(T v)
Atomically replaces the value with v, using release memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndSubAcquire(T v)
Atomically subtracts v from the value, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndSubOrdered(T v)
Atomically subtracts v from the value, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndSubRelaxed(T v)
Atomically subtracts v from the value, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndSubRelease(T v)
Atomically subtracts v from the value, using release memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndXorAcquire(T v)
Atomically performs a bitwise XOR of the value with v, using acquire memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndXorOrdered(T v)
Atomically performs a bitwise XOR of the value with v, using sequentially consistent memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndXorRelaxed(T v)
Atomically performs a bitwise XOR of the value with v, using relaxed memory ordering, and returns the previous value.
T QAtomicInteger::fetchAndXorRelease(T v)
Atomically performs a bitwise XOR of the value with v, using release memory ordering, and returns the previous value.
bool QAtomicInteger::isFetchAndAddNative()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isFetchAndAddWaitFree()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isFetchAndStoreNative()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isFetchAndStoreWaitFree()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isReferenceCountingNative()
Returns true; reference counting with ref() and deref() is always natively supported.
bool QAtomicInteger::isReferenceCountingWaitFree()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isTestAndSetNative()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
bool QAtomicInteger::isTestAndSetWaitFree()
Returns true if the operations on this type are lock-free (std::atomic<T>::is_always_lock_free).
T QAtomicInteger::load() const
Atomically loads and returns the value with sequentially consistent ordering.
See also loadRelaxed() and loadAcquire().
T QAtomicInteger::loadAcquire() const
Atomically loads and returns the value with acquire memory ordering.
See also loadRelaxed() and load().
T QAtomicInteger::loadRelaxed() const
Atomically loads and returns the value with relaxed memory ordering.
See also loadAcquire() and load().
QAtomicInteger::operator T() const
Atomically loads and returns the value, as load() does.
T QAtomicInteger::operator&=(T v)
Atomically performs a bitwise AND with v and returns the new value.
T QAtomicInteger::operator++()
Atomically increments the value and returns the new value (prefix form).
T QAtomicInteger::operator++(int)
This is an overloaded function.
Atomically increments the value and returns the previous value (postfix form).
T QAtomicInteger::operator+=(T v)
Atomically adds v to the value and returns the new value.
T QAtomicInteger::operator--()
Atomically decrements the value and returns the new value (prefix form).
T QAtomicInteger::operator--(int)
This is an overloaded function.
Atomically decrements the value and returns the previous value (postfix form).
T QAtomicInteger::operator-=(T v)
Atomically subtracts v from the value and returns the new value.
QAtomicInteger & QAtomicInteger::operator=(const QAtomicInteger &o)
Assigns the value of o to this integer, reading it with relaxed ordering, and returns a reference to this object.
QAtomicInteger & QAtomicInteger::operator=(T v)
Atomically stores v and returns a reference to this object.
T QAtomicInteger::operator^=(T v)
Atomically performs a bitwise XOR with v and returns the new value.
T QAtomicInteger::operator|=(T v)
Atomically performs a bitwise OR with v and returns the new value.
bool QAtomicInteger::ref()
Atomically increments the value by one. Returns true if the new value is not zero; otherwise returns false.
See also deref().
void QAtomicInteger::store(T v)
Atomically stores v with sequentially consistent ordering.
See also storeRelaxed() and storeRelease().
void QAtomicInteger::storeRelaxed(T v)
Atomically stores v with relaxed memory ordering.
See also storeRelease() and store().
void QAtomicInteger::storeRelease(T v)
Atomically stores v with release memory ordering.
See also storeRelaxed() and store().
bool QAtomicInteger::testAndSetAcquire(T expected, T desired)
Atomically compares the value with expected and, if they are equal, replaces it with desired, using acquire memory ordering. Returns true if the value was replaced; otherwise returns false.
bool QAtomicInteger::testAndSetAcquire(T expected, T desired, T ¤tValue)
This is an overloaded function.
Like the other overload, and stores the value found in currentValue whether or not it was replaced, comparing with expected and storing desired with acquire memory ordering.
bool QAtomicInteger::testAndSetOrdered(T expected, T desired)
Atomically compares the value with expected and, if they are equal, replaces it with desired, using sequentially consistent memory ordering. Returns true if the value was replaced; otherwise returns false.
bool QAtomicInteger::testAndSetOrdered(T expected, T desired, T ¤tValue)
This is an overloaded function.
Like the other overload, and stores the value found in currentValue whether or not it was replaced, comparing with expected and storing desired with sequentially consistent memory ordering.
bool QAtomicInteger::testAndSetRelaxed(T expected, T desired)
Atomically compares the value with expected and, if they are equal, replaces it with desired, using relaxed memory ordering. Returns true if the value was replaced; otherwise returns false.
bool QAtomicInteger::testAndSetRelaxed(T expected, T desired, T ¤tValue)
This is an overloaded function.
Like the other overload, and stores the value found in currentValue whether or not it was replaced, comparing with expected and storing desired with relaxed memory ordering.
bool QAtomicInteger::testAndSetRelease(T expected, T desired)
Atomically compares the value with expected and, if they are equal, replaces it with desired, using release memory ordering. Returns true if the value was replaced; otherwise returns false.
bool QAtomicInteger::testAndSetRelease(T expected, T desired, T ¤tValue)
This is an overloaded function.
Like the other overload, and stores the value found in currentValue whether or not it was replaced, comparing with expected and storing desired with release memory ordering.