The atomic fetch-and-add functions read the current value of the QAtomicInt and then add the given value to the current value, returning the original value. This operation equates to the following code:
Feature Tests for the Atomic API
Providing a platform-independent atomic API that works on all processors is challenging. The API provided by QAtomicInt is guaranteed to work atomically on all processors. However, since not all processors implement support for every operation provided by QAtomicInt, 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_INT_OPERATION_IS_HOW_NATIVE. OPERATION is one of REFERENCE_COUNTING, 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_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined, neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor Q_ATOMIC_INT_REFERENCE_COUNTING_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_INT_OPERATION_IS_WAIT_FREE in addition to the Q_ATOMIC_INT_OPERATION_IS_ALWAYS_NATIVE.
In cases where an atomic operation is only supported in newer generations of the processor, QAtomicInt also provides a way to check at runtime what your hardware supports with the isReferenceCountingNative(), isTestAndSetNative(), isFetchAndStoreNative(), and isFetchAndAddNative() functions. Wait-free implementations can be detected using the isReferenceCountingWaitFree(), isTestAndSetWaitFree(), isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
Below is a complete list of all feature macros for QAtomicInt:
See also QAtomicPointer.
Member Function Documentation
QAtomicInt::QAtomicInt ( int value = 0 )
Constructs a QAtomicInt with the given value.
QAtomicInt::QAtomicInt ( const QAtomicInt & other )
Constructs a copy of other.
bool QAtomicInt::deref ()
Atomically decrements the value of this QAtomicInt. Returns true if the new value is non-zero, false otherwise.
See also ref().
int QAtomicInt::fetchAndAddAcquire ( int valueToAdd )
Atomic fetch-and-add.
Reads the current value of this QAtomicInt 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.
int QAtomicInt::fetchAndAddOrdered ( int valueToAdd )
Atomic fetch-and-add.
Reads the current value of this QAtomicInt 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.
int QAtomicInt::fetchAndAddRelaxed ( int valueToAdd )
Atomic fetch-and-add.
Reads the current value of this QAtomicInt 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.
int QAtomicInt::fetchAndAddRelease ( int valueToAdd )
Atomic fetch-and-add.
Reads the current value of this QAtomicInt 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.
int QAtomicInt::fetchAndStoreAcquire ( int newValue )
Atomic fetch-and-store.
Reads the current value of this QAtomicInt 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) may not be re-ordered before the atomic operation.
int QAtomicInt::fetchAndStoreOrdered ( int newValue )
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the newValue, 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.
int QAtomicInt::fetchAndStoreRelaxed ( int newValue )
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the newValue, returning the original value.
This function uses relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.
int QAtomicInt::fetchAndStoreRelease ( int newValue )
Atomic fetch-and-store.
Reads the current value of this QAtomicInt and then assigns it the newValue, 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.
bool QAtomicInt::isFetchAndAddNative () [static]
Returns true if fetch-and-add is implemented using atomic processor instructions, false otherwise.
bool QAtomicInt::isFetchAndAddWaitFree () [static]
Returns true if atomic fetch-and-add is wait-free, false otherwise.
bool QAtomicInt::isFetchAndStoreNative () [static]
Returns true if fetch-and-store is implemented using atomic processor instructions, false otherwise.
bool QAtomicInt::isFetchAndStoreWaitFree () [static]
Returns true if atomic fetch-and-store is wait-free, false otherwise.
bool QAtomicInt::isReferenceCountingNative () [static]
Returns true if reference counting is implemented using atomic processor instructions, false otherwise.
bool QAtomicInt::isReferenceCountingWaitFree () [static]
Returns true if atomic reference counting is wait-free, false otherwise.
bool QAtomicInt::isTestAndSetNative () [static]
Returns true if test-and-set is implemented using atomic processor instructions, false otherwise.
bool QAtomicInt::isTestAndSetWaitFree () [static]
Returns true if atomic test-and-set is wait-free, false otherwise.
bool QAtomicInt::ref ()
Atomically increments the value of this QAtomicInt. Returns true if the new value is non-zero, false otherwise.
See also deref().
bool QAtomicInt::testAndSetAcquire ( int expectedValue, int newValue )
Atomic test-and-set.
If the current value of this QAtomicInt is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInt and return true. If the values are not the same, this function does nothing and returns false.
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.
bool QAtomicInt::testAndSetOrdered ( int expectedValue, int newValue )
Atomic test-and-set.
If the current value of this QAtomicInt is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInt and return true. If the values are not the same, this function does nothing and returns false.
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.
bool QAtomicInt::testAndSetRelaxed ( int expectedValue, int newValue )
Atomic test-and-set.
If the current value of this QAtomicInt is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInt and return true. If the values are not the same, this function does nothing and returns false.
This function uses relaxed memory ordering semantics, leaving the compiler and processor to freely reorder memory accesses.
bool QAtomicInt::testAndSetRelease ( int expectedValue, int newValue )
Atomic test-and-set.
If the current value of this QAtomicInt is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInt and return true. If the values are not the same, this function does nothing and returns false.
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.
QAtomicInt::operator int () const
Returns the value stored by the QAtomicInt object as an integer.
bool QAtomicInt::operator! () const
Returns true is the value of this QAtomicInt is zero; otherwise returns false.
bool QAtomicInt::operator!= ( int value ) const
Returns true if the value of this QAtomicInt is not equal to value; otherwise returns false.
QAtomicInt & QAtomicInt::operator= ( int value )
Assigns the value to this QAtomicInt and returns a reference to this QAtomicInt.
QAtomicInt & QAtomicInt::operator= ( const QAtomicInt & other )
This is an overloaded member function, provided for convenience.
Assigns other to this QAtomicInt and returns a reference to this QAtomicInt.
bool QAtomicInt::operator== ( int value ) const
Returns true if the value is equal to the value in this QAtomicInt; otherwise returns false.
Macro Documentation
Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
This macro is defined if and only if your processor supports atomic fetch-and-add on integers.
Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
This macro is defined when the hardware does not support atomic fetch-and-add on integers.
Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
This macro is defined when only certain generations of the processor support atomic fetch-and-add on integers. Use the QAtomicInt::isFetchAndAddNative() function to check what your processor supports.
Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
This macro is defined together with Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-add on integers is wait-free.
Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
This macro is defined if and only if your processor supports atomic fetch-and-store on integers.
Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
This macro is defined when the hardware does not support atomic fetch-and-store on integers.
Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
This macro is defined when only certain generations of the processor support atomic fetch-and-store on integers. Use the QAtomicInt::isFetchAndStoreNative() function to check what your processor supports.
Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
This macro is defined together with Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-store on integers is wait-free.
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
This macro is defined if and only if all generations of your processor support atomic reference counting.
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
This macro is defined when the hardware does not support atomic reference counting.
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
This macro is defined when only certain generations of the processor support atomic reference counting. Use the QAtomicInt::isReferenceCountingNative() function to check what your processor supports.
Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
This macro is defined together with Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE to indicate that the reference counting is wait-free.
Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
This macro is defined if and only if your processor supports atomic test-and-set on integers.
Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
This macro is defined when the hardware does not support atomic test-and-set on integers.
Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
This macro is defined when only certain generations of the processor support atomic test-and-set on integers. Use the QAtomicInt::isTestAndSetNative() function to check what your processor supports.
Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
This macro is defined together with Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the atomic test-and-set on integers is wait-free.