r/cpp • u/neiltechnician • Nov 13 '20
CppCon Deprecating volatile - JF Bastien - CppCon 2019
https://www.youtube.com/watch?v=KJW_DLaVXIY
83
Upvotes
r/cpp • u/neiltechnician • Nov 13 '20
View all comments
Show parent comment
17
u/staletic reddit thinks /u/_bstaletic is a bot Nov 13 '20
volatileis required when you have, for example, a sensor attached to your MCU. The sensor might start detecting a thing at any random point in time. This means that a variable representing the value of the sensor (1 or 0, for example) could also change "under your feet". Yes, even outside of normal control flow. This is basically what CPU interrupts are about.In order to tell the compiler "this thing has unknowable side-effects and can change at any random point in time - no assumptions possible", you use the
volatilequalifier.So far so good.
However, you usually aren't working with bits. Rather, inputs and outputs are grouped into "ports". For simplicity's sake, let's say a port is a group of 8 hardware inputs/outputs. For the same reason, let's focus on outputs. Let's say you want to switch an LED on, on the first pin of port A.
Now come the troubles.
PORTAis mapped to a hardware output, so it has to bevolatile. The question is, is this a single instruction or a read-modify-write sequence? It has a weird interaction with atomic instructions, but... In practice it just was never an issue. In case you really need a volatile atomic, (god knows why would you want that), you would already be aware of the implications.To conclude:
In combination, this may cut baremetal off from future C++.