Thursday, 1 June 2017

c++ - Does "volatile" only prevent from compiler optimisation?

Leave the compiler out. The compiler is the least interesting aspect of C++ and doesn't usually play a role in how you think about the language.


The language has this to say about volatile:


1.9, 1 paraphrased:



Access to volatile objects are evaluated strictly according to the rules of the abstract machine.


...


Accessing an object designated by a volatile glvalue is side effect, which is a changes in the state of the execution environment.


...


The implementation may assume that any thread will eventually do one of the following:



  • ...

  • access or modify a volatile object

  • ...



So, as you can see, volatile objects are in some sense part of the interface of your program with the outside world. One of the consequences is that volatile access is given certain ordering guarantees, but that's just a detail. The bigger picture is that volatile means "may interact with the environment".

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...