convert interlockedincrement to atomic
-
I am porting a code from c++ on windows to qt
i have the following part
InterlockedDecrement(&m_lRunCount)
where
volatile long m_lRunCount;I need to convert this to use atomic or any other method in Qt
-
@SherifOmran Did you read https://doc.qt.io/qt-5/atomic-operations.html ?
-
thank you for the link. But in my case i don't need to implement QAtomicBase. However i need to use it but this is an int and i have a unsigned long long? thats what cause the issue. How should i use it?
-
@SherifOmran said in convert interlockedincrement to atomic:
I am porting a code from c++ on windows to qt
Hi,
Qt is a C++ framework so there's no porting to do.
Or do you mean that you want to migrate some platform specific bits to be cross-platform ?
-
@SherifOmran Did you read this link: https://doc.qt.io/qt-5/qatomicinteger.html#public-functions ?
"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" -
I mean that i want to have a similar function like InterlockedDecrement into mingw
with Qt. -
@SherifOmran said in convert interlockedincrement to atomic:
into mingw
Why can't you use InterlockedIncrement there? Or the suggestions from @jsulm and others?
-
MinGw does not undersand interlockedincrement and for me the atomic is new to me, never used it before.
-
@SherifOmran
Hi
mingw should be able to use useInterlockedDecrement
https://docs.microsoft.com/en-us/windows/desktop/api/winnt/nf-winnt-interlockeddecrement
if you link against Kernel32
so try wtih
LIBS += -lkernel32
in .pro file ( qmake + run rebuild all)
and see if it will use it then. -
@SherifOmran said in convert interlockedincrement to atomic:
for me the atomic is new to me
There is really nothing special about it: declare a variable of type QAtomicInteger and increment/decriment it when needed:
QAtomicInteger<int> atomicInt; ... --atomicInt;
Looks easier to me than using InterlockedDecrement.
-
@jsulm said in convert interlockedincrement to atomic:
There is really nothing special about it: declare a variable of type QAtomicInteger and increment/decriment it when needed
100% agree. The only sidenote is that
QAtomicInteger
is some overhead on top ofstd::atomic
so to replicate basic operations like the above I'd just stick with thestd
version of the class