Atomic increment on mac OS X

后端 未结 4 2060
天命终不由人
天命终不由人 2021-02-06 10:10

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 poi

4条回答
  •  广开言路
    2021-02-06 10:38

    You can also use IncrementAtomic() and DecrementAtomic() via CoreServices:

    #include 
    
    int main(int argc, char** argv) 
    {
      int val = 0;
      IncrementAtomic(&val);
      DecrementAtomic(&val);    
    
      return 0;
    }
    

    Note: the return value of these functions is the value of the integer before it is incremented, so if you want similar behavior to the Win32 InterlockedIncrement() and InterlockedDecrement() functions, you will need to create wrappers that +1 to the return value.

提交回复
热议问题