简体   繁体   English

C ++ 11是否允许(不要求)发布/获取volatile关键字的语义

[英]Does C++11 allow (not require) release/acquire semantics for volatile keyword

Since Visual C++ 2005, Microsoft has made additional ordering guarantees for accesses to volatile types which are not required by the C++ Standard. 从Visual C ++ 2005开始,Microsoft为访问C ++标准不需要的volatile类型提供了额外的排序保证。

Does anything in the C++ Standard actually forbid these guarantees? C ++标准中的任何内容是否真的禁止这些保证? The Microsoft documentation seems to think so. Microsoft文档似乎这么认为。

Please let me know whether the Standard allows the ordering implemented by Microsoft, and also vote on this bug report: 请告诉我标准是否允许Microsoft实施的订购,并对此错误报告进行投票:

An implementation is certainly allowed to do things beyond on what is required as long as it meets the requirements set forth by the standard. 只要满足标准规定的要求,当然允许实现超出所需要的范围。 Adding release/acquire semantics to volatile object is definitely within scope. volatile对象添加释放/获取语义肯定在范围内。 I don't think there is interest in the C++ committee to change the semantics (we are just starting a new week of discussing C++ in Portland with Herb right now talking about how to organize the meeting). 我认为C ++委员会没有兴趣改变语义(我们刚刚开始在波特兰开始新的一周讨论C ++,现在正在讨论如何组织会议)。

The standard requires that access to volatile objects is evaluated strictly according to the rules of the abstract machine , which means roughly "don't optimize", but no more than that. 该标准要求严格根据抽象机器的规则评估对易失性对象的访问 ,这意味着粗略地“不优化”,但不多于此。 For example, the compiler is not allowed to cache a value in a register or perform common subexpression elimination. 例如,不允许编译器将值缓存在寄存器中或执行公共子表达式消除。 It has to do exactly what you tell it. 它必须完全按照你的说法去做。

Thus, for all the standard cares, volatile has memory_order_relaxed semantics (it doesn't specify anything different). 因此,对于所有标准关注, volatile都有memory_order_relaxed语义(它没有指定任何不同的东西)。 This doesn't mean you are not allowed to implement something more strict, of course. 当然,这并不意味着你不允许你实施更严格的事情。

The Microsoft compiler has always ( since 2005 as Ben Voigt points out) treated volatile as acquire/release, which lead to many people assuming "volatile == threadsafe", which in return led to many articles turning this around into "volatile is useless!" 微软编译器总是( 自2005年以来 ,Ben Voigt指出)将volatile视为获取/释放,这导致许多人假设“volatile == threadsafe”,这反过来导致许多文章将其转变为“volatile是无用的! “ and "volatile is evil". 而“挥发是邪恶的”。

The likely reason why MS recommends to use the ISO implementation is that this makes their compiler behave like every other compiler, no more nasty surprises. MS建议使用ISO实现的可能原因是,这使得它们的编译器像其他所有编译器一样,没有更令人讨厌的意外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM