After looking at a [bunch](https://stackoverflow.com/questions/2484980/why-is-volatile-not-considered-useful-in-multithreaded-c-or-c-programming?noredirect=1&lq=1) [of](https://stackoverflow.com/questions/78172/using-c-pthreads-do-shared-variables-need-to-be-volatile) [other](https://stackoverflow.com/questions/6837699/are-mutex-lock-functions-sufficient-without-volatile?noredirect=1&lq=1) [questions](https://stackoverflow.com/questions/23449562/when-would-you-need-a-volatile-pointer) [and](https://stackoverflow.com/questions/2478397/atomic-swap-in-gnu-c/2478520#2478520) [their](https://stackoverflow.com/a/44243426/706054) [answers](https://stackoverflow.com/a/2485733/706054), I get the impression that **there is no widespread agreement on what the "volatile" keyword in C means exactly.**

Even the standard itself does not seem to be clear enough for everyone to agree on [what it means](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1956.htm).

Among other problems:

1. It seems to provide different guarantees depending on your hardware and depending on your compiler.
2. It affects compiler optimizations but not hardware optimizations, so on an advanced processor that does its own run-time optimizations, it is not even clear whether the compiler *can* prevent whatever optimization you want to prevent. (Some compilers do generate instructions to prevent some hardware optimizations on some systems, but this does not appear to be standardized in any way.)

To summarize the problem, it appears (after reading a lot) that "volatile" guarantees something like: *The value will be read/written not just from/to a register, but at least to the core's L1 cache, in the same order that the reads/writes appear in the code.* But this seems useless, since reading/writing from/to a register is already sufficient within the same thread, while coordinating with L1 cache doesn't guarantee anything further regarding coordination with other threads. I can't imagine when it could ever be important to sync just with L1 cache.

**USE 1**

The only widely-agreed-upon use of volatile seems to be for old or embedded systems where certain memory locations are hardware-mapped to I/O functions, like a bit in memory that controls (directly, in the hardware) a light, or a bit in memory that tells you whether a keyboard key is down or not (because it is connected by the hardware directly to the key).

It seems that **"use 1" does not occur in portable code whose targets include multi-core systems.**

**USE 2**

Not too different from "use 1" is memory that could be read or written at any time by an interrupt handler (which might control a light or store info from a key). But already for this we have the problem that depending on the system, the interrupt handler [might run on](https://stackoverflow.com/q/33955582/706054) [a different core](https://stackoverflow.com/a/25153972/706054) with [its own memory cache](https://stackoverflow.com/a/954069/706054), and "volatile" does not guarantee cache coherency on all systems.

So **"use 2" seems to be beyond what "volatile" can deliver.**

**USE 3**

The only other undisputed use I see is to prevent mis-optimization of accesses via different variables pointing to the same memory that the compiler doesn't realize is the same memory. But this is probably only undisputed because people aren't talking about it -- I only saw one mention of it. And I thought the C standard already recognized that "different" pointers (like different args to a function) might point to the same item or nearby items, and already specified that the compiler must produce code that works even in such cases. However, I couldn't quickly find this topic in the latest (500 page!) standard.

So **"use 3" maybe doesn't exist** at all?

*Hence my question:*

**Does "volatile" guarantee anything at all in portable C code for multi-core systems?**

---

EDIT -- update
--------------

After browsing the [latest standard](https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf), it is looking like the answer is at least a *very* limited yes:

1. The standard repeatedly specifies special treatment for the specific type "volatile sig\_atomic\_t". However the standard also says that use of the signal function in a multi-threaded program results in undefined behavior. So this use case seems limited to communication between a single-threaded program and its signal handler.

2. The standard also specifies a clear meaning for "volatile" in relation to setjmp/longjmp. (Example code where it matters is given in other [questions](https://stackoverflow.com/questions/1393443/setjmp-longjmp-and-local-variables) and [answers](https://stackoverflow.com/questions/7996825/why-volatile-works-for-setjmp-longjmp).)

So the more precise question becomes:

**Does "volatile" guarantee anything at all in portable C code for multi-core systems, apart from (1) allowing a single-threaded program to receive information from its signal handler, or (2) allowing setjmp code to see variables modified between setjmp and longjmp?**

*This is still a yes/no question.*

*If "yes", it would be great if you could show an example of bug-free portable code which becomes buggy if "volatile" is omitted. If "no", then I suppose a compiler is free to ignore "volatile" outside of these two very specific cases, for multi-core targets.*