AtomicSwap而不是AtomicCompareAndSwap?

我知道在MacOSX / PosiX系统上,通过g ++进行C / C ++代码的原子比较和交换。 但是,我不需要比较 - 我只想原子地交换两个值。是否有可用的原子交换操作? [Everythign我能找到的是atomic_compare_and_swap ...而我只是想做交换,而不进行比较]。 谢谢!     
已邀请:
“lock xchg”intel汇编指令可能会实现你想要的,但我不认为有一个GCC包装函数使其可移植。因此,您使用内联汇编(不可移植)或使用比较和交换并强制比较为真(无效)。希望这可以帮助 :-)     
GCC确实在某些处理器上提供此操作,在(令人困惑的名称)
__sync_lock_test_and_set
下。从海湾合作委员会文件:
 This builtin, as described by Intel, is not a traditional
 test-and-set operation, but rather an atomic exchange operation.
 It writes VALUE into `*PTR', and returns the previous contents of
 `*PTR'.

 Many targets have only minimal support for such locks, and do not
 support a full exchange operation.  In this case, a target may
 support reduced functionality here by which the _only_ valid value
 to store is the immediate constant 1.  The exact value actually
 stored in `*PTR' is implementation defined.
但是,x86-32和x86-64支持完全交换操作,有效地提供了你需要写入的
lock xchg
包装器。     
不要以为有。这是参考,顺便说一下: http://developer.apple.com/Mac/library/documentation/DriversKernelHardware/Reference/libkern_ref/OSAtomic_h/index.html#//apple_ref/doc/header/user_space_OSAtomic.h     

要回复问题请先登录注册