Visual C ++易失性 [英] Visual C++ Volatile

查看:142
本文介绍了Visual C ++易失性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual C ++中的volatile的MSDN文档指出,除了确保读取始终从内存中读取,并且写入总是相应写入之外,写入具有释放语义,读取具有获取语义。

The MSDN docs for "volatile" in Visual C++ indicate that writes have "release semantics" and that reads have "acquire semantics", in addition to ensuring that reads always read from memory and that writes always write accordingly.

volatile的C规范包括第二部分(不要做疯狂的优化),而不是第一部分(内存栅栏)。

The C spec for "volatile" includes the second part (don't do crazy optimizations), but not the first part (a memory fence).

Visual C ++中是否有任何方式只获取C的不稳定行为,而没有内存范围?

Is there any way in Visual C++ to get the "C" volatile behaviour only, without the memory fence?

我想强制一个变量总是在堆栈,在一个固定的点,但我不想花任何记忆围栏的每一个任务的开销。

I want to force a variable to always be on the stack, in a fixed spot, but I don't want to take the overhead of a memory fence on every assignment to it.

有什么简单的方法来做这个Visual C ++源?

Is there any easy way to do that with Visual C++ source?

推荐答案


在Visual C ++中有没有任何方式只获得C的挥发性行为,没有内存围栏?

Is there any way in Visual C++ to get the "C" volatile behaviour only, without the memory fence?

在读取和写入到 volatile 存储器位置时在汇编级别创建的存储器栅栏,因为在该平台上每个加载都具有获取语义,并且每个存储器具有释放语义。因此,对于x86上的MSVC, volatile 指令只是指示编译器阻止加载和存储的重新排序,这取决于您是从标记为 volatile

On x86 there are no memory fences created at the assembly level on reads and writes to a volatile memory location since on that platform every load has acquire semantics, and every store has release semantics. Therefore for MSVC on x86, the volatile directive simply directs the compiler to prevent the reordering of loads and stores depending on if you are writing or reading from the memory location that was marked volatile.

您只会在IA64架构上产生内存围栏的惩罚,因为内存平台的顺序模型不能确保用于加载和存储的获取和释放语义。

You would only incur the "penalty" of a memory fence on the IA64 architecture, since there the memory ordering model of the platform does not ensure acquire and release semantics for loads and stores.

请记住,这种行为是MSVC特有的,不是 volatile

Keep in mind this behavior is MSVC-specific, and is not a standardized semantic of volatile.

更新:根据@ildjarn, ARM与Windows 8,因为该平台还有一个弱顺序的内存一致性模型,如IA64。

Update: According to @ildjarn you would also see a memory fence on ARM with Windows 8 since that platform also has a weakly ordered memory-consistency model like IA64.

这篇关于Visual C ++易失性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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