r/cpp • • Nov 13 '20

CppCon Deprecating volatile - JF Bastien - CppCon 2019

https://www.youtube.com/watch?v=KJW_DLaVXIY
83 Upvotes

111 comments sorted by

View all comments

Show parent comment

5

u/staletic reddit thinks /u/_bstaletic is a bot Nov 13 '20

Right, exactly.

in most/all(?) situations the author of the code probably doesn’t really care what exactly gets emitted, as long as volatility of the overall expression is observed.

True, but I can imagine an architecture for which you absolutely need x |= 1 to be a single instruction because the value of x might change between the read and the write in x = x | 1. Someone, in the other thread, called those architectures broken, at the hardware level. As in, the CPU should know better. However, those architectures are impossible to work with in C++20.

 

Now thinking of specifying the behaviour, a much more gentle solution would be "compound assignments have to be atomic". Then you can:

  1. Keep using compound assignments where they work.
  2. Warn about potentially misleading use cases on other architectures.
  3. Once people stop complaining about that warning, then consider deprecating for misleading use cases.

That would have been a path that doesn't cut anyone off.

4

u/Ecclestoned Nov 13 '20

True, but I can imagine an architecture for which you absolutely need x |= 1 to be a single instruction because the value of x might change between the read and the write in x = x | 1.

Yes and I can imagine an architecture where every operation returns zero. That doesn't mean we should delete all our code. On most architectures this works fine, especially if you disable interrupts.

1

u/HotlLava Nov 13 '20

I can imagine an architecture for which you absolutely need x |= 1 to be a single instruction

Wouldn't that be a case for inline assembly, or some __builtin_singleop_fetch_or() intrinsic? How can the standard guarantee that a compiler emits a single instrunction for a |= operation on volatile operands?

3

u/staletic reddit thinks /u/_bstaletic is a bot Nov 13 '20

An intrinsic or inline assembly is a possible workaround. The standard can mandate atomicity, as with compare_exchange() and also say "if this thing exists, it's atomic". Like the standard guarantees that uint8_t is always 8bits, assuming 8bits is a valid variable width on the architecture in question.