In C#, we know that a bool is atomic - then why is it valid to mark it as volatile? what is the difference and what is a good (or even practical) use-case for one versus the other?
The supposition of your question is that you believe that volatile
makes an access atomic. But volatility and atomicity are completely different things, so stop conflating them.
Volatility is the property that the compiler and runtime are restricted from making certain optimizations involving moving reads and writes of variables forwards and backwards in time with respect to each other, and more generally, with respect to other important events such as starting and stopping threads, running constructors, and so on. Consult the C# specification for a detailed list of how operations may or may not be re-ordered with respect to visible side effects.
Atomicity is the property that a particular operation can only be observed as not started or totally completed, and never "halfway done".
As you can see from the definitions, those two things have nothing whatsoever to do with each other.
In C#, all accesses to references, bools, and integer types of size 4 and smaller are guaranteed to be atomic.
Now, in C# there is some slight non-orthogonality between atomicity and volatility, in that only fields of atomic types may be marked as volatile. You may not make a volatile double, for example. It would be really weird and dangerous to say "we're going to restrict how reads and writes may be optimized but still allow tearing". Since volatility does not cause atomicity, you don't want to put users in a position of thinking that an operation is atomic just because it is also volatile.
You should read my series of articles that explains in far more detail what the differences between these things are, and what volatile actually does, and why you do not understand nearly enough to be using it safely.
https://ericlippert.com/2011/05/26/atomicity-volatility-and-immutability-are-different-part-one/
https://ericlippert.com/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two/
https://ericlippert.com/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three/
https://web.archive.org/web/20160323025740/http://blog.coverity.com/2014/03/12/can-skip-lock-reading-integer/
If you think you understand volatility after reading all that, I invite you to try to solve the puzzle I pose here:
https://web.archive.org/web/20160729162225/http://blog.coverity.com/2014/03/26/reordering-optimizations/
volatile bool
use case there exists another more optimal solution. SO in theorybool
will always be enough. – Mesh