QAtomicPointer 类

template <typename T> class QAtomicPointer

QAtomicPointer 类是为指针提供独立于平台的原子操作的模板类。 更多...

头: #include <QAtomicPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

QAtomicPointer (T * value = nullptr)
QAtomicPointer (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 )
QAtomicPointer<T> & operator= (const QAtomicPointer<T> & other )

静态公共成员

bool isFetchAndAddNative ()
bool isFetchAndAddWaitFree ()
bool isFetchAndStoreNative ()
bool isFetchAndStoreWaitFree ()
bool isTestAndSetNative ()
bool isTestAndSetWaitFree ()

Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE
Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE

详细描述

有关整数的原子操作,见 QAtomicInteger 类。

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.

原子 API

内存排序

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.

测试和设置

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 (),和 testAndSetOrdered (). See above for an explanation of the different memory ordering semantics.

抓取和存储

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 (),和 fetchAndStoreOrdered (). See above for an explanation of the different memory ordering semantics.

抓取和添加

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 (),和 fetchAndAddOrdered (). See above for an explanation of the different memory ordering semantics.

用于原子 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 是某一 TEST_AND_SET , FETCH_AND_STORE ,或 FETCH_AND_ADD ,和 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 (),和 isFetchAndAddNative () functions. Wait-free implementations can be detected using the isTestAndSetWaitFree (), isFetchAndStoreWaitFree (),和 isFetchAndAddWaitFree () 函数。

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

另请参阅 QAtomicInteger and qYieldCpu ().

成员函数文档编制

[constexpr noexcept] QAtomicPointer:: QAtomicPointer ( T * value = nullptr)

构造 QAtomicPointer 采用给定 value .

[noexcept] QAtomicPointer:: QAtomicPointer (const QAtomicPointer < T > & other )

构造副本为 other .

T *QAtomicPointer:: fetchAndAddAcquire ( qptrdiff valueToAdd )

原子抓取和添加。

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

此函数使用 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 )

原子抓取和添加。

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

此函数使用 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 )

原子抓取和添加。

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

此函数使用 relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.

T *QAtomicPointer:: fetchAndAddRelease ( qptrdiff valueToAdd )

原子抓取和添加。

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

此函数使用 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 )

原子抓取和存储。

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

此函数使用 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:: fetchAndStoreOrdered ( T * newValue )

原子抓取和存储。

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

此函数使用 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:: fetchAndStoreRelaxed ( T * newValue )

原子抓取和存储。

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

此函数使用 relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.

T *QAtomicPointer:: fetchAndStoreRelease ( T * newValue )

原子抓取和存储。

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

此函数使用 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.

[static constexpr] bool QAtomicPointer:: isFetchAndAddNative ()

返回 true if fetch-and-add is implemented using atomic processor instructions, false otherwise.

[static constexpr] bool QAtomicPointer:: isFetchAndAddWaitFree ()

返回 true if atomic fetch-and-add is wait-free, false otherwise.

[static constexpr] bool QAtomicPointer:: isFetchAndStoreNative ()

返回 true if fetch-and-store is implemented using atomic processor instructions, false otherwise.

[static constexpr] bool QAtomicPointer:: isFetchAndStoreWaitFree ()

返回 true if atomic fetch-and-store is wait-free, false otherwise.

[static constexpr] bool QAtomicPointer:: isTestAndSetNative ()

返回 true if test-and-set is implemented using atomic processor instructions, false otherwise.

[static constexpr] bool QAtomicPointer:: isTestAndSetWaitFree ()

返回 true if atomic test-and-set is wait-free, false otherwise.

T *QAtomicPointer:: loadAcquire () const

Atomically loads the value of this QAtomicPointer using the "Acquire" memory ordering. The value is not modified in any way, but note that there's no guarantee that it remains so.

另请参阅 storeRelease () 和 loadRelaxed ().

T *QAtomicPointer:: loadRelaxed () const

Atomically loads the value of this QAtomicPointer using relaxed memory ordering. The value is not modified in any way, but note that there's no guarantee that it remains so.

另请参阅 storeRelaxed () 和 loadAcquire ().

void QAtomicPointer:: storeRelaxed ( T * newValue )

Atomically stores the newValue value into this atomic type, using relaxed memory ordering.

另请参阅 storeRelease () 和 loadRelaxed ().

void QAtomicPointer:: storeRelease ( T * newValue )

Atomically stores the newValue value into this atomic type, using the "Release" memory ordering.

另请参阅 storeRelaxed () 和 loadRelaxed ().

bool QAtomicPointer:: testAndSetAcquire ( T * expectedValue , T * newValue )

Atomic test-and-set.

If the current value of this QAtomicPointer expectedValue , the test-and-set functions assign the newValue 到此 QAtomicPointer and return true. If the values are not the same, this function does nothing and returns false .

此函数使用 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.

bool QAtomicPointer:: testAndSetOrdered ( T * expectedValue , T * newValue )

Atomic test-and-set.

If the current value of this QAtomicPointer expectedValue , the test-and-set functions assign the newValue 到此 QAtomicPointer and return true. If the values are not the same, this function does nothing and returns false .

此函数使用 ordered memory ordering semantics, which ensures that memory access before and after the atomic operation (in program order) may not be re-ordered.

bool QAtomicPointer:: testAndSetRelaxed ( T * expectedValue , T * newValue )

Atomic test-and-set.

If the current value of this QAtomicPointer expectedValue , the test-and-set functions assign the newValue 到此 QAtomicPointer and return true. If the values are not the same, this function does nothing and returns false .

此函数使用 relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.

bool QAtomicPointer:: testAndSetRelease ( T * expectedValue , T * newValue )

Atomic test-and-set.

If the current value of this QAtomicPointer expectedValue , the test-and-set functions assign the newValue 到此 QAtomicPointer and return true. If the values are not the same, this function does nothing and returns false .

此函数使用 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.

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

赋值 other 到此 QAtomicPointer and returns a reference to this QAtomicPointer .

宏文档编制

Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE

This macro is defined if and only if your processor supports atomic fetch-and-add on pointers.

Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE

This macro is defined when the hardware does not support atomic fetch-and-add on pointers.

Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_SOMETIMES_NATIVE

This macro is defined when only certain generations of the processor support atomic fetch-and-add on pointers. Use the QAtomicPointer::isFetchAndAddNative () function to check what your processor supports.

Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE

This macro is defined together with Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-add on pointers is wait-free.

Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE

This macro is defined if and only if your processor supports atomic fetch-and-store on pointers.

Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE

This macro is defined when the hardware does not support atomic fetch-and-store on pointers.

Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_SOMETIMES_NATIVE

This macro is defined when only certain generations of the processor support atomic fetch-and-store on pointers. Use the QAtomicPointer::isFetchAndStoreNative () function to check what your processor supports.

Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE

This macro is defined together with Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-store on pointers is wait-free.

Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE

This macro is defined if and only if your processor supports atomic test-and-set on pointers.

Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE

This macro is defined when the hardware does not support atomic test-and-set on pointers.

Q_ATOMIC_POINTER_TEST_AND_SET_IS_SOMETIMES_NATIVE

This macro is defined when only certain generations of the processor support atomic test-and-set on pointers. Use the QAtomicPointer::isTestAndSetNative () function to check what your processor supports.

Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE

This macro is defined together with Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the atomic test-and-set on pointers is wait-free.