14

Since Visual C++ 2005, Microsoft has made additional ordering guarantees for accesses to volatile types which are not required by the C++ Standard.

Does anything in the C++ Standard actually forbid these guarantees? The Microsoft documentation seems to think so.

Please let me know whether the Standard allows the ordering implemented by Microsoft, and also vote on this bug report:

3
  • Also the MSDN documentation states, in the section ISO Compliant: "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." To my eyes this implies that according to the C++03 Standard volatile is to be used for inter-process communication. Is that what they are implying? Commented Oct 15, 2012 at 16:30
  • 1
    @John: I believe they are using the phrase "C++11 ISO Standard code" to mean "portable code that works correctly with any C++11 compiler". Use of volatile for threading (inter- or intra-process) has never been portable. BTW the C++03 Standard doesn't discuss processes or threading much at all.
    – Ben Voigt
    Commented Oct 15, 2012 at 16:31
  • @John: I don't think they intend to imply that. They're comparing conforming C++11 programs with programs intended for their implementations of C++11 and C++03 with the semantics of /volatile:ms. They aren't comparing C++11 with C++03. Commented Oct 15, 2012 at 16:54

2 Answers 2

9

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. 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).

8
  • All volatile requires is that every read to a volatile object results in an actual read and that every write to a volatile object result in an actual write. This can be augmented by what every memory visibility facility they want to add: the standard makes just a few requirements on effects on I/O and volatile variables as its observable behavior. Adding release/acquire semantics to volatile will not reduce guarantees on the observable behavior. Of course, to actually observe change to volatile object you need to observed it from the outside of the program. Commented Oct 15, 2012 at 16:46
  • 1
    That's what I thought. So, in summary "Acquire/release guarantees on volatile variables are permitted but not required. Code that relies on such guarantees will be non-portable."
    – Ben Voigt
    Commented Oct 15, 2012 at 16:49
  • 1
    @BenVoigt: Note that the fact that an implementation can add guarantees to the standard does not imply that a program that depends on those extra guarantees is standard-compliant (your program can still have Undefined Behavior according to the standard even though in that particular implementation the behavior might be fully defined) Commented Oct 15, 2012 at 16:52
  • @David: The compiler is compliant... it provides all guarantees required by the Standard and correctly compiles and executes code written against the Standard (i.e. the behavior is well-defined according to the Standard). We're not arguing that it's a bad idea to include a note about implementation-specific behavior and portability in the documentation. The question is whether the behavior is "non-ISO Compliant".
    – Ben Voigt
    Commented Oct 15, 2012 at 17:08
  • 2
    Being at the meeting allows to quickly poll people: The Microsoft people I talked to said that it is a "doc bug" to call it non-conforming. They think it is a conforming extension but none users should rely on (there were committee members overhearing the discussion who consider the behavior a misfeature). Commented Oct 15, 2012 at 17:23
0

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). 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!" 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.

12
  • So using /volatile:iso magically finds all race conditions? "Nasty surprises" when moving between implementations are common, and getting rid of the acquire/release semantics does nothing to change that. What it does change is that the optimizer is free to emit faster code sequences.
    – Ben Voigt
    Commented Oct 15, 2012 at 17:12
  • Also, the Microsoft compiler hasn't always treated volatile as acquire/release, it started to do so in Visual C++ 2005 (like I said in the question).
    – Ben Voigt
    Commented Oct 15, 2012 at 17:13
  • No, it does not "magically" do anything (this is what many people used to think about the volatile keyword). /volatile:iso just makes the compiler behave like all others, which is less confusing. Your code doesn't suddenly behave incorrectly if you compile it with something else, because you don't rely on "magic".
    – Damon
    Commented Oct 15, 2012 at 17:14
  • No two compilers behave exactly alike. There's always a chance that a race condition is hidden by a particular implementation. Just because code passes tests under /volatile:iso doesn't mean it's strictly conforming, so "no more nasty surprises" just isn't meaningful.
    – Ben Voigt
    Commented Oct 15, 2012 at 17:18
  • 1
    @BenVoigt: they missed a trick then, to instead emit a warning, "you seem to be using volatile, have you considered a critical section?" Commented Oct 15, 2012 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.