Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two atomic ints Qt5

Tags:

c++

atomic

qt

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?

like image 336
DrDonut Avatar asked Sep 22 '26 02:09

DrDonut


1 Answers

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.

like image 106
demonplus Avatar answered Sep 24 '26 18:09

demonplus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!