Search code examples
c++qtqt5atomic

Compare two atomic ints Qt5


In Qt5 a few operations on QAtomicInt have disappeared, including the operator==.

Does anyone know how I can compare two QAtomicInts in Qt5? The old code was like this:

qAtomicInt a;
qAtomicInt b;
if(a == b)
    //Do something

As far as I understand the documentation (http://doc.qt.io/qt-5/sourcebreaks.html) I could do it like this:

qAtomicInt a;
qAtomicInt b;
if(a.load() == b.load())
    //Do something

But if I do it like this, is the compare operation still atomic? Can the values that are being compared change while this operation is in progress?


Solution

  • Your code seems to be correct taking into account the changes in Qt5. You can use load() or loadAcquire() for comparison of QAtomicInt.

    Though these operations itself are marked as atomic while you are comparing ints in

    if(a.load() == b.load())
    

    values in QAtomicInts may change.

    Operator== which was available in Qt4

    http://doc.qt.io/qt-4.8/qatomicint.html#operator-eq-eq

    was not marked as atomic in documentation, by the way.