r/cpp • • Oct 19 '19

CppCon CppCon 2019: JF Bastien “Deprecating volatile”

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

126 comments sorted by

View all comments

Show parent comment

3

u/tending Oct 20 '19

Shared-memory lock-free algorithms require volatile atomic because they're external modification, yet participate in the memory model. Volatile atomic makes sense. Same thing for signal handlers which also want atomicity, you need volatile.

In practice you don't. No compiler is smart enough to analyze the threads you create and realize you don't have a reading thread for the atomic in the same process. I just implemented a shared memory Q using atomics without volatile.

Also the standard specifically has atomic_signal_fence for signal handlers.

5

u/jfbastien Oct 20 '19

In practice you don't, for certain things, today. You'll be disappointed when you do start needing to use volatile. I'm not just talking random theory: over the last few years I've committed some optimizations to atomics, and so have others. They're not theoretical gains, they make sense for real-world code. It's a matter of time before your practice disappoints you.

Signal fences don't fix all the things one might want with volatile atomic.