QAtomicPointer Class

template <typename T> class QAtomicPointer

The QAtomicPointer class is a template class that provides platform-independent atomic operations on pointers. More...

Header: #include <QAtomicPointer>
qmake: QT += core
Since: Qt 4.4

This class was introduced in Qt 4.4.

Public Functions

QAtomicPointer(const QAtomicPointer<T> &other)
QAtomicPointer(T *value = nullptr)
QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other)
T *fetchAndAddAcquire(qptrdiff valueToAdd)
T *fetchAndAddOrdered(qptrdiff valueToAdd)
T *fetchAndAddRelaxed(qptrdiff valueToAdd)
T *fetchAndAddRelease(qptrdiff valueToAdd)
T *fetchAndStoreAcquire(T *newValue)
T *fetchAndStoreOrdered(T *newValue)
T *fetchAndStoreRelaxed(T *newValue)
T *fetchAndStoreRelease(T *newValue)
T *loadAcquire() const
T *loadRelaxed() const
void storeRelaxed(T *newValue)
void storeRelease(T *newValue)
bool testAndSetAcquire(T *expectedValue, T *newValue)
bool testAndSetOrdered(T *expectedValue, T *newValue)
bool testAndSetRelaxed(T *expectedValue, T *newValue)
bool testAndSetRelease(T *expectedValue, T *newValue)

Static Public Members

Macros

Detailed Description

For atomic operations on integers, see the QAtomicInteger class.

An atomic operation is a complex operation that completes without interruption. The QAtomicPointer class provides atomic test-and-set, fetch-and-store, and fetch-and-add for pointers.

The Atomic API

Memory ordering

QAtomicPointer provides several implementations of the atomic test-and-set, fetch-and-store, and fetch-and-add functions. Each implementation defines a memory ordering semantic that describes how memory accesses surrounding the atomic instruction are executed by the processor. Since many modern architectures allow out-of-order execution and memory ordering, using the correct semantic is necessary to ensure that your application functions properly on all processors.

  • Relaxed - memory ordering is unspecified, leaving the compiler and processor to freely reorder memory accesses.
  • Acquire - memory access following the atomic operation (in program order) may not be re-ordered before the atomic operation.
  • Release - memory access before the atomic operation (in program order) may not be re-ordered after the atomic operation.
  • Ordered - the same Acquire and Release semantics combined.

Test-and-set

If the current value of the QAtomicPointer is an expected value, the test-and-set functions assign a new value to the QAtomicPointer and return true. If values are not the same, these functions do nothing and return false. This operation equates to the following code:

 if (currentValue == expectedValue) {
     currentValue = newValue;
     return true;
 }
 return false;

There are 4 test-and-set functions: testAndSetRelaxed(), testAndSetAcquire(), testAndSetRelease(), and testAndSetOrdered(). See above for an explanation of the different memory ordering semantics.

Fetch-and-store

The atomic fetch-and-store functions read the current value of the QAtomicPointer and then assign a new value, returning the original value. This operation equates to the following code:

 T *originalValue = currentValue;
 currentValue = newValue;
 return originalValue;

There are 4 fetch-and-store functions: fetchAndStoreRelaxed(), fetchAndStoreAcquire(), fetchAndStoreRelease(), and fetchAndStoreOrdered(). See above for an explanation of the different memory ordering semantics.

Fetch-and-add

The atomic fetch-and-add functions read the current value of the QAtomicPointer and then add the given value to the current value, returning the original value. This operation equates to the following code:

 T *originalValue = currentValue;
 currentValue += valueToAdd;
 return originalValue;

There are 4 fetch-and-add functions: fetchAndAddRelaxed(), fetchAndAddAcquire(), fetchAndAddRelease(), and fetchAndAddOrdered(). See above for an explanation of the different memory ordering semantics.

Feature Tests for the Atomic API

Providing a platform-independent atomic API that works on all processors is challenging. The API provided by QAtomicPointer is guaranteed to work atomically on all processors. However, since not all processors implement support for every operation provided by QAtomicPointer, it is necessary to expose information about the processor.

You can check at compile time which features are supported on your hardware using various macros. These will tell you if your hardware always, sometimes, or does not support a particular operation. The macros have the form Q_ATOMIC_POINTER_OPERATION_IS_HOW_NATIVE. OPERATION is one of TEST_AND_SET, FETCH_AND_STORE, or FETCH_AND_ADD, and HOW is one of ALWAYS, SOMETIMES, or NOT. There will always be exactly one defined macro per operation. For example, if Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE is defined, neither Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE nor Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE will be defined.

An operation that completes in constant time is said to be wait-free. Such operations are not implemented using locks or loops of any kind. For atomic operations that are always supported, and that are wait-free, Qt defines the Q_ATOMIC_POINTER_OPERATION_IS_WAIT_FREE in addition to the Q_ATOMIC_POINTER_OPERATION_IS_ALWAYS_NATIVE.

In cases where an atomic operation is only supported in newer generations of the processor, QAtomicPointer also provides a way to check at runtime what your hardware supports with the isTestAndSetNative(), isFetchAndStoreNative(), and isFetchAndAddNative() functions. Wait-free implementations can be detected using the isTestAndSetWaitFree(), isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.

Below is a complete list of all feature macros for QAtomicPointer:

See also QAtomicInteger.

Member Function Documentation

QAtomicPointer::QAtomicPointer(const QAtomicPointer<T> &other)

Constructs a copy of other.

QAtomicPointer::QAtomicPointer(T *value = nullptr)

Constructs a QAtomicPointer with the given value.

QAtomicPointer<T> &QAtomicPointer::operator=(const QAtomicPointer<T> &other)

Assigns other to this QAtomicPointer and returns a reference to this QAtomicPointer.

T *QAtomicPointer::fetchAndAddAcquire(qptrdiff valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicPointer and then adds valueToAdd to the current value, returning the original value.

This function uses acquire memory ordering semantics, which ensures that memory access following the atomic operation (in program order) may not be re-ordered before the atomic operation.

T *QAtomicPointer::fetchAndAddOrdered(qptrdiff valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicPointer and then adds valueToAdd to the current value, returning the original value.

This function uses ordered memory ordering semantics, which ensures that memory access before and after the atomic operation (in program order) may not be re-ordered.

T *QAtomicPointer::fetchAndAddRelaxed(qptrdiff valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicPointer and then adds valueToAdd to the current value, returning the original value.

This function uses relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.

T *QAtomicPointer::fetchAndAddRelease(qptrdiff valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicPointer and then adds valueToAdd to the current value, returning the original value.

This function uses release memory ordering semantics, which ensures that memory access before the atomic operation (in program order) may not be re-ordered after the atomic operation.

T *QAtomicPointer::fetchAndStoreAcquire(T *newValue)

Atomic fetch-and-store.

Reads the current value of this QAtomicPointer and then assigns it the newValue, returning the original value.

This function uses acquire memory ordering semantics, which ensures that memory access following the atomic operation (in program order