在VS2012中使用“volatile”关键字的最佳做法 [英] Best practice for using the 'volatile' keyword in VS2012

查看:296
本文介绍了在VS2012中使用“volatile”关键字的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于将我们的开发和构建环境从VS2008升级到VS2012,我对在旧代码库中使用 volatile 关键字的影响感到困惑(它非常广泛,因为

Since upgrading our development and build environments from VS2008 to VS2012, I am confused about the implications of using the volatile keyword in our legacy codebase (it's quite extensive as there is a much copied pattern for managing threads from the "old" days).

Microsoft在VS2012文档中有以下注释:

Microsoft has the following remarks in the VS2012 documentation:


如果您熟悉C# volatile 关键字或熟悉早期版本Visual C ++中 volatile 的行为,请注意,当指定 / volatile:iso 编译器选项时,Visual Studio中支持C ++ 11 ISO标准 volatile 关键字。 (对于ARM,它是默认指定的)。 C ++ 11 ISO标准代码中的 volatile 关键字仅用于硬件访问;不要将其用于线程间通信。对于线程间通信,使用C ++标准模板库中的 std :: atomic 等机制。

If you are familiar with the C# volatile keyword, or familiar with the behavior of volatile in earlier versions of Visual C++, be aware that the C++11 ISO Standard volatile keyword is different and is supported in Visual Studio when the /volatile:iso compiler option is specified. (For ARM, it's specified by default). The volatile keyword in C++11 ISO Standard code is to be used only for hardware access; do not use it for inter-thread communication. For inter-thread communication, use mechanisms such as std::atomic<T> from the C++ Standard Template Library.

它还说:


/ volatile:ms 使用编译器选项 - 默认情况下,当除ARM以外的体系结构被定向时 - 编译器生成额外的代码以维护对volatile对象的引用之间的排序,同时保持对其他全局对象的引用的排序。 >

When the /volatile:ms compiler option is used—by default when architectures other than ARM are targeted—the compiler generates extra code to maintain ordering among references to volatile objects in addition to maintaining ordering to references to other global objects.

我认为这意味着,我们现有的代码不会中断,但不一定是可移植的(对我们来说不是一个问题)。

I take this to mean, that our existing code won't break but won't necessarily be portable (not a problem for us).

但是,它确实引发了这些问题,如果可能的话,我想要一些建议:

However, it does raise these questions, on which I would like some advice, if possible:


  1. 如果我们在我们的代码中删除 volatile 限定词的使用,并替换为符合C ++ 11 ISO标准的等效项,即使我们不会将代码移植从MS?

  2. 如果我们不这样做,是否有任何缺点?

  1. Should we remove uses of volatile qualifiers in our our code and replace with C++11 ISO Standard compliant equivalents, even though we would not port the code away from MS?
  2. If we don't do the above, is there any downside?

我明白,这不是一个真正的特定的编程问题,但我们正在开始一些相当大的重构,我想为这项工作提供一些明智的指导方针。

I appreciate that this is not really a specific programming problem but we're embarking on some quite major refactoring and I would like to be able to offer some sensible guidelines for this work.

推荐答案


  1. 如果你有时间了。好处不是那么大 - C ++ 11原子可以允许更准确地控制您需要什么样的同步,并且具有更清晰定义的语义,这可以允许编译器更好地优化代码。

  2. 在理论上,但非常不可能,未来版本的编译器可能会完全放弃对MS风格的volatile的支持。或者有一天你实际上想要远离MS编译器,即使你留在Windows上。如果你现在正在做重构,这可能是一个很好的时间来做原子论替换挥发性物质的工作,从而避免你以后的工作。

这篇关于在VS2012中使用“volatile”关键字的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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