无锁结构的C ++原子操作 [英] C++ atomic operations for lock-free structures
问题描述
我使用原子(双)比较和交换指令实现无锁机制。 cmpxchg16b
我目前在汇编中编写,然后链接它。但是,我想知道是否有一种方法让编译器自动为我执行此操作?例如环绕式代码块和原子,并让它去了解如何实现代码作为一个原子指令在底层处理器架构(或如果底层arch不支持,在编译时生成一个错误)?
PS我知道gcc有一些内置程序(至少为CAS)
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Atomic-Builtins.html#Atomic-Builtins
已经回答此处。
C ++ 0x标准将提供一些原子数据类型,主要是使用std :: atomic< model模板的整型和void类型。该文章提及了 Boehm的atomic_ops项目,您可以下载并使用今天。 p>
如果不是,在编译器中不能实现内联汇编器?我知道MSVC有内联汇编器的 __asm 关键字例程。 Google说是,gcc 也可以做到这一点。
I'm implementing a lock-free mechanism using atomic (double) compare and swap instructions e.g. cmpxchg16b
I'm currently writing this in assembly and then linking it in. However, I wondered if there was a way of getting the compiler to do this for me automatically? e.g. surround code block with 'atomically' and have it go figure it out how to implement the code as an atomic instruction in the underlying processor architecture (or generate an error at compile time if the underlying arch does not support it)?
P.S. I know that gcc has some built-ins (at least for CAS)
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Atomic-Builtins.html#Atomic-Builtins
Already kindof answered here.
The C++0x standard will provide some atomic datatypes, mainly integer and void types using std::atomic<> template. That article mentions Boehm's atomic_ops project which you can download and use today.
If not, can't you implement your assembler inline in the compiler? I know MSVC has the __asm keyword for inline assembler routines. Google says yes, gcc can do it too.
这篇关于无锁结构的C ++原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!