Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic increment on mac OS X

Tags:

c++

atomic

I have googled for atomic increment and decrement operators on Mac OS X and found "OSAtomic.h", but it seems you can only use this in kernel space.

Jeremy Friesner pointed me at a cross-platform atomic counter in which they use assembly or mutex on OS X (as far as I understood the interleaving of ifdefs).

Isn't there something like InterlockedDecrement or atomic_dec() on OS X ?

like image 680
Anna B Avatar asked Jan 23 '10 20:01

Anna B


2 Answers

What makes you think OSAtomic is kernel space only? The following compiles and works fine.

#include <libkern/OSAtomic.h>
#include <stdio.h>

int main(int argc, char** argv) {
  int32_t foo = 1;
  OSAtomicDecrement32(&foo);
  printf("%d\n", foo);

  return 0;
}
like image 58
wich Avatar answered Nov 05 '22 07:11

wich


Currently, the recommendation is to use C++11's std::atomic.

like image 28
Taylor Avatar answered Nov 05 '22 08:11

Taylor