According to cppreference, most uses of the volatile
keyword are to be deprecated in C++20. What is the disadvantage of volatile
? And what is the alternative solution when not using volatile
?
There's a good talk by the C++ committee language evolution chair on why.
Brief summary, the places that volatile
is being removed from didn't have any well defined meaning in the standard and just caused confusion.
+=
a single/atomic instruction? How about ++
?compare_exchange
? What if it fails?void foo(int volatile n)
mean? or int volatile foo()
?*vp;
do a load? (This has changed twice in the standard.)Historically, people have used volatile
to achieve thread safety in C and C++. In C++11, non-UB ways to create synchronization and shared state between threads were added. I recommend Back to Basics: Concurrency as a good introduction.