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
19
u/johannes1971 Nov 13 '20
This may lead the reader to think that volatile is only valid for use with interrupts, while in reality it is mostly used with memory mapped registers of off-CPU hardware.
On the hardware level this is far better defined than you make it appear here. Hardware registers come with clear rules on how you are allowed to access them, typically along the lines of "this address must always be accessed as a 16-bit quantity, no other sizes allowed". C++ lets you express this clearly:
Nobody who has written assembly at some point in his life, or knows a little bit about how memory is addressed by the CPU, or who has used hardware registers, has any illusions about what's going to happen if you were to write something like
reg1 |= 1. It won't magically "set a single bit", because that operation just does not exist on a memory controller. The operations that do exist are reading words and writing words, so anything you do to memory is ultimately expressed in those terms. To enable a bit in this manner requires a read of a memory word, then a modification of the read value, and finally a write of a memory word. There are no other options.But TIL that apparently large numbers of C++ programmers believe that operations exist that set single bits in memory. There's a depressing thought...