Microsoft在VS2012文档中有以下评论:
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 asstd::atomic<T>
from the C++ Standard Template Library.
它继续说:
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.
我认为这意味着我们现有的代码不会破坏,但不一定是可移植的(对我们来说不是问题).
但是,如果可能的话,它确实提出了这些问题,我希望得到一些建议:
>我们是否应该在我们的代码中删除volatile限定符的使用并替换为符合C 11 ISO标准的等价物,即使我们不会将代码从MS中移除?
>如果我们不这样做,是否有任何缺点?
我理解这不是一个特定的编程问题,但我们正在进行一些非常重要的重构,我希望能够为这项工作提供一些明智的指导.