mac OS X上的原子增量 [英] Atomic increment on mac OS X

查看:155
本文介绍了mac OS X上的原子增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X上搜索了原子递增和递减运算符,并发现了OSAtomic.h,但似乎只能在内核空间中使用。

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 指示我在跨平台原子计数器,它们在OS X上使用汇编或互斥(根据我的理解ifdefs的交错)。

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).

在OS X上是否有类似 InterlockedDecrement atomic_dec()

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

推荐答案

什么让你认为OSAtomic只是内核空间?以下编译和工作正常。

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;
}

这篇关于mac OS X上的原子增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆