Suppose I have the following declaration:
int* volatile x;
I believe that this defines a volatile pointer "normal" variable.
To me this could mean one of two things:
First Guess
The pointer can change, but the number will not change without notice. This means that some other thread (that the compiler doesn't know about) can change the pointer, but if the old pointer was pointing to a "12" then the new pointer (the new value of the pointer, because the thread changes it) would point to another "12".
To me this seems fairly useless, and I would assume that this is not what the real operation is.
Second Guess
The pointer can change, and thus if the pointer changes, the compiler must reload the value in the pointer before using it. But if it verifies that the pointer did not change (with an added check), it can then assume that the value it points to remained the same also.
So my question is this:
What does declaring a volatile pointer to non volatile data actually do?
volatile
specifier doesn't have anything to do with threads. – Backscratchervolatile
just means "its observable behavior to read and write this value". So any mention ofx
means "you must read the value ofx
, and cannot assume you know it/you must write the value ofx
, and cannot assume it's pointless". – Stimulantvolatile
has something to do with the compiler's knowledge of threads. – Backscratchervolatile
specifier doesn't have anything to do with threads. – Backscratchervolatile
is never usable with threads, ever? On any existing architecture? – Leliavolatile
is useful with threads is where the relevant language, threading, or compiler documentation says it has some defined semantics. Otherwise, you're just assuming it will continue to do what you want because it happened to do what you want when you tried it. All sensible threading standard provide guaranteed ways to get whatever semantics you need and you should use those because they're guaranteed. – Backscratchervolatile
might have some platform-specific semantics that might be useful on some platforms. Too many times, I've seen code break horribly because assumptions were made about what future compilers or CPUs would be able to optimize. We need to decide to stop making that mistake sooner or later. I will not accept your invitation to make that mistake. – Backscratchervolatile std::sig_atomic_t
) had minimal performance cost. Wouldn't sensible compilers for that platform implement only what the standard actually requires? (And the worst case scenario imaginable would be if compiler writers have no choice but to make the performance poor because people listened to your advice and relied on behavior wisely not guaranteed by the standard.) – Backscratcher