3 ms·
> which especially on embedded systems creates bugs where the things like "read the status register THEN read the data register (which clears the status registe
by Thiez 5y ago
> which especially on embedded systems creates bugs where the things like "read the status register THEN read the data register (which clears the status register when read)" those get re-ordered and suddenly your loop never exits because your status bit is never set.
Why are the embedded systems programmers not using `volatile`, which exists for this exact reason?
- compiler-guy 5y ago"volatile" is not a synchronization primitive and doesn't forbid reordering amongst access to different memory locations. All it does is forbid reordering or removing accesses to a particular memory location. Historically, many compilers implemented it as a hard memory barrier, but that isn't how the standard defines it.
- Thiez 5y agoVolatile operations are considered a side effect and as such may not be reordered with other volatile operations. How is that not literally sufficient for the operations that GGP described?
- compiler-guy 5y agoI spoke perhaps a little strongly. But that said: The compiler is free to reorder accesses to multiple volatile variables if they happen prior to the same sequence point. So (roughly) expressions involving two volatile variables do not have those accesses sequenced. But you are right that the usage described earlier is OK, as long as both variables are marked volatile, and the accesses straddle a sequence point. The more common mistake with volatiles is to use the for multithreading primitives.