4 ms·
volatile should only be used for accessing MMIO registers in device drivers; that's it.
by loeg 7y ago
volatile should only be used for accessing MMIO registers in device drivers; that's it.
- ychen306 7y agoThis is probably not useful for production, but volatile is a great way to see what kind of code compiler generates in a realistic setting. For example, if you want to see how compiler optimizes a code snippet and the code depends on a constant that you don't want to get constant folded away.
- ridiculous_fish 7y agoThere's definitely more uses. For example, shared memory between processes: you should mark it volatile. C++ atomics are no good here, because they are not guaranteed to be lock free or address free.
- ychen306 7y agomaybe use fences?
- loeg 7y agoShared memory is way outside the scope of standard C or C++. It's implementation-defined. It's inconsistent to insist on the weakest definition of atomics allowed by the C/C++ standard(s) and simultaneous invoke one of the weirdest implementation-defined mechanisms defined by POSIX. If your implementation provides shared memory of some kind, it's up to your implementation to define some sort of reasonable semantics. In POSIX' case, it's up to POSIX operating systems to define reasonable semantics on the memory, using constructs like PTHREAD_PROCESS_SHARED and "robust" pthread mutexes.
- vardump 7y ago> C++ atomics are no good here, because they are not guaranteed to be lock free or address free. That's not right; you can still use std::memory_order to get the memory barriers generated that are required. These are going to obviously be lock free, they deal with memory ordering—what you tried to deal with volatile, but in general case. See: https://en.cppreference.com/w/cpp/atomic/atomic/store https://en.cppreference.com/w/cpp/atomic/atomic/store Effectively std::atomic stores and loads generate volatile accesses plus the required memory barriers to get the desired behavior.
- drivebycomment 7y agoThat is a reasonable heuristic but your statement is not technically correct. E.g. you need volatile around setjmp/longjmp and that has nothing to do with IO.
- loeg 7y agoTechnically for sig_atomic_t as well. Both are... messier parts of the C abstract machine.
- rurban 7y agoAnd if your GC doesn't dump the registers. Only with volatile you can keep all locals on the stack. And yes, that's not stupid. It's actually faster than all the register "optimizations" for practical use cases in fast VM's. Register saving across calls and at the GC is much more expensive. mem2reg is an antipattern mostly
- loeg 7y agoGC (of the kind where an external context walks other thread machine stacks) definitely falls under implementation-defined :-).