Best practice for using the volatile keyword in VS2012

Starting with the upgrade of our development and build environments from VS2008 to VS2012, I am confused about the consequences of using the volatile keyword in our old code base (this is quite extensive, since there are many copied templates for managing flows from the "old" days).

In the Microsoft VS2012 documentation, Microsoft has the following comments:

If you are familiar with the C # volatile keyword or familiar with the volatile behavior in earlier versions of Visual C ++, keep in mind that the C ++ 11 ISO Standard mutable keyword is different and supported in Visual Studio when the /volatile:iso option is specified /volatile:iso compiler. (For ARM, it is specified by default). volatile keyword in the C ++ 11 ISO standard is used only for access to hardware; do not use it for cross-threading. To exchange data between threads, use the following methods: std::atomic<T> from the standard C ++ template library.

The following is said:

When the /volatile:ms compiler option is used, by default, when architectures other than ARM are targeted, the compiler generates additional code to maintain order among references to mutable objects in addition to maintaining the order of references to other global objects.

I assume that our existing code will not break, but will not necessarily be portable (not a problem for us).

However, this raises these questions on which I would like to get some advice, if possible:

  • Should we remove the use of volatile qualifiers in our code and replace equivalents compatible with the ISO C ++ 11 standard, even if we do not cancel the code from MS?
  • If we do not do this, is there a flaw?

I appreciate that this is not a very specific programming problem, but we are embarking on a fairly large refactoring, and I would like to offer some reasonable recommendations for this work.

+7
source share
1 answer
  • If you have time for this. The benefits are not that great - C ++ 11 atomistics allows you to more accurately control exactly what kind of synchronization you need, and have more clearly defined semantics that can allow the compiler to better optimize the code.
  • Theoretically, but it is very unlikely that a future version of the compiler may completely abandon support for MS-style volatility. Or one day you really want to get away from the MS compiler, even if you stay on Windows. If you are refactoring now, this may be the right time to work on replacing volatiles with atomic engineering, which will save you from doing future work.
+6
source

All Articles