Qt 原子操作的类QAtomicInteger

QAtomicInteger Class

The QAtomicInteger class provides platform-independent atomic operations on integers.More...

Header: #include <QAtomicInteger>
qmake: QT += core
Since: Qt 5.3
Inherits: QBasicAtomicInteger<T>
Inherited By:

QAtomicInt

Public Functions

  QAtomicInteger(T value = 0)
  QAtomicInteger(const QAtomicInteger &other)
bool deref()
T fetchAndAddAcquire(T valueToAdd)
T fetchAndAddOrdered(T valueToAdd)
T fetchAndAddRelaxed(T valueToAdd)
T fetchAndAddRelease(T valueToAdd)
T fetchAndAndAcquire(T valueToAnd)
T fetchAndAndOrdered(T valueToAnd)
T fetchAndAndRelaxed(T valueToAnd)
T fetchAndAndRelease(T valueToAnd)
T fetchAndOrAcquire(T valueToOr)
T fetchAndOrOrdered(T valueToOr)
T fetchAndOrRelaxed(T valueToOr)
T fetchAndOrRelease(T valueToOr)
T fetchAndStoreAcquire(T newValue)
T fetchAndStoreOrdered(T newValue)
T fetchAndStoreRelaxed(T newValue)
T fetchAndStoreRelease(T newValue)
T fetchAndSubAcquire(T valueToSub)
T fetchAndSubOrdered(T valueToSub)
T fetchAndSubRelaxed(T valueToSub)
T fetchAndSubRelease(T valueToSub)
T fetchAndXorAcquire(T valueToXor)
T fetchAndXorOrdered(T valueToXor)
T fetchAndXorRelaxed(T valueToXor)
T fetchAndXorRelease(T valueToXor)
T load() const
T loadAcquire() const
bool ref()
void store(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)
  operator T() const
T operator&=(T valueToAnd)
T operator++()
T operator++(int)
T operator+=(T valueToAdd)
T operator--()
T operator--(int)
T operator-=(T valueToSub)
QAtomicInteger & operator=(const QAtomicInteger &other)
QAtomicInteger & operator=(T newValue)
T operator^=(T valueToXor)
T operator|=(T valueToOr)

Static Public Members

Macros

Detailed Description

The QAtomicInteger class provides platform-independent atomic operations on integers.

For atomic operations on pointers, see the QAtomicPointer class.

An atomic operation is a complex operation that completes without interruption. TheQAtomicInteger class provides atomic reference counting, test-and-set, fetch-and-store, and fetch-and-add for integers.

The template parameter T must be a C++ integer type:

  • 8-bit: char, signed char, unsigned char, qint8, quint8
  • 16-bit: short, unsigned short, qint16, quint16, char16_t (C++11)
  • 32-bit: int, unsigned int, qint32, quint32, char32_t (C++11)
  • 64-bit: long long, unsigned long long, qint64, quint64
  • platform-specific size: long, unsigned long
  • pointer size: qintptr, quintptr, qptrdiff

Of the list above, only the 32-bit- and pointer-sized instantiations are guaranteed to work on all platforms. Support for other sizes depends on the compiler and processor architecture the code is being compiled for. To test whether the other types are supported, check the macro Q_ATOMIC_INT\e{nn}_IS_SUPPORTED, where \e{nn} is the number of bits desired.

The Atomic API

Reference counting

The ref() and deref() functions provide an efficient reference counting API. The return value of these functions are used to indicate when the last reference has been released. These functions allow you to implement your own implicitly shared classes.

MySharedType &MySharedType::operator=(const MySharedType &other)
{
    (void) other.data->atomicInt.ref();
    if (!data->atomicInt.deref()) {
        // The last reference has been released
        delete d;
    }
    d = other.d;
    return *this;
}
Memory ordering

QAtomicInteger 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 QAtomicInteger is an expected value, the test-and-set functions assign a new value to the QAtomicInteger 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 QAtomicInteger and then assign a new value, returning the original value. This operation equates to the following code:

int 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 QAtomicInteger and then add the given value to the current value, returning the original value. This operation equates to the following code:

int 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 QAtomicInteger is guaranteed to work atomically on all processors. However, since not all processors implement support for every operation provided byQAtomicInteger, 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_INTnn_OPERATION_IS_HOW_NATIVE. nn is the size of the integer (in bits),OPERATION is one of REFERENCE_COUNTINGTEST_AND_SETFETCH_AND_STORE, orFETCH_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_INT32_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined, neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor Q_ATOMIC_INT32_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_INTnn_OPERATION_IS_WAIT_FREE in addition to the Q_ATOMIC_INTnn_OPERATION_IS_ALWAYS_NATIVE.

In cases where an atomic operation is only supported in newer generations of the processor, QAtomicInteger 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 QAtomicInteger:

  • Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
  • Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
  • Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_NOT_NATIVE
  • Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_WAIT_FREE
  • Q_ATOMIC_INTnn_TEST_AND_SET_IS_ALWAYS_NATIVE
  • Q_ATOMIC_INTnn_TEST_AND_SET_IS_SOMETIMES_NATIVE
  • Q_ATOMIC_INTnn_TEST_AND_SET_IS_NOT_NATIVE
  • Q_ATOMIC_INTnn_TEST_AND_SET_IS_WAIT_FREE
  • Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_ALWAYS_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_NOT_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_WAIT_FREE
  • Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_ALWAYS_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_NOT_NATIVE
  • Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_WAIT_FREE

For compatibility with previous versions of Qt, macros with an empty nn are equivalent to the 32-bit macros. For example, Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE is the same as Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_WAIT_FREE.

See also QAtomicPointer.

Member Function Documentation

QAtomicInteger::QAtomicInteger(T value = 0)

Constructs a QAtomicInteger with the given value.

QAtomicInteger::QAtomicInteger(const QAtomicInteger &other)

Constructs a copy of other.

bool QAtomicInteger::deref()

Atomically decrements the value of this QAtomicInteger. Returns true if the new value is non-zero, false otherwise.

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.

See also ref() and operator--().

T QAtomicInteger::fetchAndAddAcquire(T valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicInteger 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.

See also operator+=() and fetchAndSubAcquire().

T QAtomicInteger::fetchAndAddOrdered(T valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicInteger 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.

See also operator+=() and fetchAndSubOrdered().

T QAtomicInteger::fetchAndAddRelaxed(T valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicInteger 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.

See also operator+=() and fetchAndSubRelaxed().

T QAtomicInteger::fetchAndAddRelease(T valueToAdd)

Atomic fetch-and-add.

Reads the current value of this QAtomicInteger 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.

See also operator+=() and fetchAndSubRelease().

T QAtomicInteger::fetchAndAndAcquire(T valueToAnd)

Atomic fetch-and-and.

Reads the current value of this QAtomicInteger and then bitwise-ANDs valueToAnd 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.

This function was introduced in Qt 5.3.

See also operator&=().

T QAtomicInteger::fetchAndAndOrdered(T valueToAnd)

Atomic fetch-and-and.

Reads the current value of this QAtomicInteger and then bitwise-ANDs valueToAnd 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.

This function was introduced in Qt 5.3.

See also operator&=().

T QAtomicInteger::fetchAndAndRelaxed(T valueToAnd)

Atomic fetch-and-and.

Reads the current value of this QAtomicInteger and then bitwise-ANDs valueToAnd 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.

This function was introduced in Qt 5.3.

See also operator&=().

T QAtomicInteger::fetchAndAndRelease(T valueToAnd)

Atomic fetch-and-and.

Reads the current value of this QAtomicInteger and then bitwise-ANDs valueToAnd 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.

This function was introduced in Qt 5.3.

See also operator&=().

T QAtomicInteger::fetchAndOrAcquire(T valueToOr)

Atomic fetch-and-or.

Reads the current value of this QAtomicInteger and then bitwise-ORs valueToOr 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.

This function was introduced in Qt 5.3.

See also operator|=().

T QAtomicInteger::fetchAndOrOrdered(T valueToOr)

Atomic fetch-and-or.

Reads the current value of this QAtomicInteger and then bitwise-ORs valueToOr 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.

This function was introduced in Qt 5.3.

See also operator|=().

T QAtomicInteger::fetchAndOrRelaxed(T valueToOr)

Atomic fetch-and-or.

Reads the current value of this QAtomicInteger and then bitwise-ORs valueToOr 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.

This function was introduced in Qt 5.3.

See also operator|=().

T QAtomicInteger::fetchAndOrRelease(T valueToOr)

Atomic fetch-and-or.

Reads the current value of this QAtomicInteger and then bitwise-ORs valueToOr 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.

This function was introduced in Qt 5.3.

See also operator|=().

T QAtomicInteger::fetchAndStoreAcquire(T newValue)

Atomic fetch-and-store.

Reads the current value of this QAtomicInteger 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.

T QAtomicInteger::fetchAndStoreOrdered(T newValue)

Atomic fetch-and-store.

Reads the current value of this QAtomicInteger 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.

T QAtomicInteger::fetchAndStoreRelaxed(T newValue)

Atomic fetch-and-store.

Reads the current value of this QAtomicInteger 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.

T QAtomicInteger::fetchAndStoreRelease(T newValue)

Atomic fetch-and-store.

Reads the current value of this QAtomicInteger 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.

T QAtomicInteger::fetchAndSubAcquire(T valueToSub)

Atomic fetch-and-sub.

Reads the current value of this QAtomicInteger and then subtracts valueToSub 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.

This function was introduced in Qt 5.3.

See also operator-=() and fetchAndAddAcquire().

T QAtomicInteger::fetchAndSubOrdered(T valueToSub)

Atomic fetch-and-sub.

Reads the current value of this QAtomicInteger and then subtracts valueToSub 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.

This function was introduced in Qt 5.3.

See also operator-=() and fetchAndAddOrdered().

T QAtomicInteger::fetchAndSubRelaxed(T valueToSub)

Atomic fetch-and-sub.

Reads the current value of this QAtomicInteger and then subtracts valueToSub 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.

This function was introduced in Qt 5.3.

See also operator-=() and fetchAndAddRelaxed().

T QAtomicInteger::fetchAndSubRelease(T valueToSub)

Atomic fetch-and-sub.

Reads the current value of this QAtomicInteger and then subtracts valueToSub 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.

This function was introduced in Qt 5.3.

See also operator-=() and fetchAndAddRelease().

T QAtomicInteger::fetchAndXorAcquire(T valueToXor)

Atomic fetch-and-xor.

Reads the current value of this QAtomicInteger and then bitwise-XORs valueToXor 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.

This function was introduced in Qt 5.3.

See also operator^=().

T QAtomicInteger::fetchAndXorOrdered(T valueToXor)

Atomic fetch-and-xor.

Reads the current value of this QAtomicInteger and then bitwise-XORs valueToXor 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.

This function was introduced in Qt 5.3.

See also operator^=().

T QAtomicInteger::fetchAndXorRelaxed(T valueToXor)

Atomic fetch-and-xor.

Reads the current value of this QAtomicInteger and then bitwise-XORs valueToXor 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.

This function was introduced in Qt 5.3.

See also operator^=().

T QAtomicInteger::fetchAndXorRelease(T valueToXor)

Atomic fetch-and-xor.

Reads the current value of this QAtomicInteger and then bitwise-XORs valueToXor 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.

This function was introduced in Qt 5.3.

See also operator^=().

[static]bool QAtomicInteger::isFetchAndAddNative()

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

[static]bool QAtomicInteger::isFetchAndAddWaitFree()

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

[static]bool QAtomicInteger::isFetchAndStoreNative()

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

[static]bool QAtomicInteger::isFetchAndStoreWaitFree()

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

[static]bool QAtomicInteger::isReferenceCountingNative()

Returns true if reference counting is implemented using atomic processor instructions, false otherwise.

[static]bool QAtomicInteger::isReferenceCountingWaitFree()

Returns true if atomic reference counting is wait-free, false otherwise.

[static]bool QAtomicInteger::isTestAndSetNative()

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

[static]bool QAtomicInteger::isTestAndSetWaitFree()

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

T QAtomicInteger::load() const

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

See also store() and loadAcquire().

T QAtomicInteger::loadAcquire() const

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

See also store() and load().

bool QAtomicInteger::ref()

Atomically increments the value of this QAtomicInteger. Returns true if the new value is non-zero, false otherwise.

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.

See also deref() and operator++().

void QAtomicInteger::store(T newValue)

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

See also storeRelease() and load().

void QAtomicInteger::storeRelease(T newValue)

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

See also store() and load().

bool QAtomicInteger::testAndSetAcquire(T expectedValueT newValue)

Atomic test-and-set.

If the current value of this QAtomicInteger is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInteger 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 QAtomicInteger::testAndSetOrdered(T expectedValueT newValue)

Atomic test-and-set.

If the current value of this QAtomicInteger is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInteger 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 QAtomicInteger::testAndSetRelaxed(T expectedValueT newValue)

Atomic test-and-set.

If the current value of this QAtomicInteger is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInteger 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 QAtomicInteger::testAndSetRelease(T expectedValueT newValue)

Atomic test-and-set.

If the current value of this QAtomicInteger is the expectedValue, the test-and-set functions assign the newValue to this QAtomicInteger 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.

QAtomicInteger::operator T() const

Atomically loads the value of this QAtomicInteger using a sequentially consistent memory ordering if possible; or "Acquire" ordering if not. The value is not modified in any way, but note that there's no guarantee that it remains so.

This function was introduced in Qt 5.3.

See also load() and loadAcquire().

T QAtomicInteger::operator&=(T valueToAnd)

Atomic add-and-fetch.

Reads the current value of this QAtomicInteger and then bitwise-ANDs valueToAnd to the current value, returning the new value value.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also fetchAndAndOrdered().

T QAtomicInteger::operator++()

Atomically pre-increments the value of this QAtomicInteger. Returns the new value of this atomic.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also ref(), operator++(int), and operator--().

T QAtomicInteger::operator++(int)

Atomically post-increments the value of this QAtomicInteger. Returns the old value of this atomic.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also ref(), operator++(), and operator--(int).

T QAtomicInteger::operator+=(T valueToAdd)

Atomic add-and-fetch.

Reads the current value of this QAtomicInteger and then adds valueToAdd to the current value, returning the new value value.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also fetchAndAddOrdered() and operator-=().

T QAtomicInteger::operator--()

Atomically pre-decrements the value of this QAtomicInteger. Returns the new value of this atomic.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also deref(), operator--(int), and operator++().

T QAtomicInteger::operator--(int)

Atomically post-decrements the value of this QAtomicInteger. Returns the old value of this atomic.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also deref(), operator--(), and operator++(int).

T QAtomicInteger::operator-=(T valueToSub)

Atomic sub-and-fetch.

Reads the current value of this QAtomicInteger and then subtracts valueToSub to the current value, returning the new value value.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also fetchAndSubOrdered() and operator+=().

QAtomicInteger &QAtomicInteger::operator=(const QAtomicInteger&other)

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

QAtomicInteger &QAtomicInteger::operator=(T newValue)

Atomically stores the newValue value into this atomic type using a sequentially consistent memory ordering if possible; or "Release" ordering if not. This function returns a reference to this object.

This function was introduced in Qt 5.3.

See also store() and storeRelease().

T QAtomicInteger::operator^=(T valueToXor)

Atomic xor-and-fetch.

Reads the current value of this QAtomicInteger and then bitwise-XORs valueToXor to the current value, returning the new value value.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also fetchAndXorOrdered().

T QAtomicInteger::operator|=(T valueToOr)

Atomic or-and-fetch.

Reads the current value of this QAtomicInteger and then bitwise-ORs valueToOr to the current value, returning the new value value.

This function uses a sequentially consistent memory ordering if possible; or "Ordered" ordering if not.

This function was introduced in Qt 5.3.

See also fetchAndOrOrdered().

Macro Documentation

Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_ALWAYS_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_NOT_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_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 QAtomicInteger::isFetchAndAddNative() function to check what your processor supports.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_FETCH_AND_ADD_IS_WAIT_FREE

This macro is defined together withQ_ATOMIC_INTnn_FETCH_AND_ADD_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-add on integers is wait-free.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_ALWAYS_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_NOT_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_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 QAtomicInteger::isFetchAndStoreNative() function to check what your processor supports.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_FETCH_AND_STORE_IS_WAIT_FREE

This macro is defined together withQ_ATOMIC_INTnn_FETCH_AND_STORE_IS_ALWAYS_NATIVE to indicate that the atomic fetch-and-store on integers is wait-free.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_IS_SUPPORTED

This macro is defined if atomic integers of size nn (in bits) are supported in this compiler / architecture combination. Q_ATOMIC_INT32_IS_SUPPORTED is always defined.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_ALWAYS_NATIVE

This macro is defined if and only if all generations of your processor support atomic reference counting.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_NOT_NATIVE

This macro is defined when the hardware does not support atomic reference counting.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE

This macro is defined when only certain generations of the processor support atomic reference counting. Use the QAtomicInteger::isReferenceCountingNative() function to check what your processor supports.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_REFERENCE_COUNTING_IS_WAIT_FREE

This macro is defined together withQ_ATOMIC_INTnn_REFERENCE_COUNTING_IS_ALWAYS_NATIVE to indicate that the reference counting is wait-free.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_TEST_AND_SET_IS_ALWAYS_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_TEST_AND_SET_IS_NOT_NATIVE

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

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_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 QAtomicInteger::isTestAndSetNative() function to check what your processor supports.

nn is the size of the integer, in bits (8, 16, 32 or 64).

Q_ATOMIC_INTnn_TEST_AND_SET_IS_WAIT_FREE

This macro is defined together withQ_ATOMIC_INTnn_TEST_AND_SET_IS_ALWAYS_NATIVE to indicate that the atomic test-and-set on integers is wait-free.

nn is the size of the integer, in bits (8, 16, 32 or 64).Qt Merchandise

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中的原子bool型是一种特殊的型,它提供了一种线程安全的方式来处理bool值。原子bool型使用Qt的线程库提供的功能,确保多个线程同时访问bool值时不会发生数据竞争。 原子bool型通常用于需要多个线程同时修改同一bool变量的场景,例如条件变量、信号与槽机制等。当多个线程同时访问原子bool变量时,它们将通过互斥锁(mutex)或原子操作来确保对变量的访问是原子的,即一次只有一个线程能够修改该变量。 原子bool型提供了几个有用的成员函数,用于在多个线程之间同步对bool变量的访问。例如,可以使用`load()`函数获取当前bool变量的值,使用`store()`函数设置新的bool值,使用`testAndSetRelaxed()`函数进行原子交换操作等。这些函数都使用了Qt的线程库提供的同步机制,以确保线程安全。 使用原子bool型的好处是,它能够避免多个线程同时修改同一bool变量时可能出现的竞争条件,从而提高程序的性能和可靠性。然而,需要注意的是,虽然原子bool型提供了线程安全,但它并不能完全消除所有竞争条件的风险。在某些情况下,仍然需要额外的同步机制来确保数据的一致性。 总之,Qt中的原子bool型是一种特殊的bool型,它提供了一种线程安全的方式来处理bool值,适用于需要多个线程同时访问同一变量的场景。它使用Qt的线程库提供的同步机制来确保数据的安全性和一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值