I have these two lines of code in my app
volatile __block int32_t counter = 0;
and later in a loop...
OSAtomicIncrement32(&counter);
But now OSAtomicIncrement32
is deprecated and Xcode is showing me this error message.
'OSAtomicIncrement32' is deprecated: first deprecated in iOS 10.0 - Use atomic_fetch_add_explicit(memory_order_relaxed) from instead
See this error message, there is one parameter to atomic_fetch_add_explicit
, right?
So I try
atomic_fetch_add_explicit(&counter)
and I see this message
Too few arguments to function call, expected 3, have 1
I love the crappy messages Xcode dumps.
How do I use this?
Read the documentation... It takes the pointer to the target variable, the number to add (in your case probably 1) and the memory order, suggested by the compiler as memory_order_relaxed
, probably to match the existing behavior of OSAtomicIncrement32
.
atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);
If you don't know what a memory order is, probably you'd be better served by a plain atomic_fetch_add
with the "safest" sequential ordering.
atomic_fetch_add(&counter, 1);
That being said, if you are actually working in C++ and that variable is used only by your code, you can just use an std::atomic_int
(or std::atomic<std::int32_t>
if you want guaranteed 32 bit range) and the plain ++
operator.
std::atomic_int counter{0};
//...
++counter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With