In Thread synchronization of atomic invariants in .NET 4.5 I’m presenting my observations of what the compiler does in very narrow context of only on Intel x86 and Intel x64 with a particular version of .NET. You can install SDKs that give you access to compilers to other processors. For example, if you write something for Windows Phone or Windows Store, you’ll get compilers for other processors (e.g. ARM) with memory models looser than x86 and x64. That post was only observations in the context of x86 and x64.
I believe more knowledge is always better; but you have to use that knowledge responsibly. If you know you’re only ever going to target x86 or x64 (and you don’t if you use AnyCPU even in VS 2012 because some yet-to-be-created processor might be supported in a future version or update to .NET) and you do want to micro-optimize your code, then that post might give you enough knowledge to do that. Otherwise, take it with a grain of salt. I’ll get into a little bit more detail in part 2: Thread synchronization of non-atomic invariants in .NET 4.5 at a future date—which will include more specific guidance and recommendations.
In the case were I used a really awkwardly placed lock:
It’s important to point out the degree of implicit side-effects that this code depends on. One, it assumes that the compiler is smart enough to know that a while loop is the equivalent of a series of sequential statements. e.g. this is effectively equivalent to:
That is, there is the implicit volatile read (e.g. a memory fence, from the Monitor.Enter implementation detail) at the start of the lock block and an implicit volatile write (e.g. a memory fence, from the Monitor.Exit implementation detail).
In case it wasn’t obvious, you should never write code like this, it’s simply an example—and as I pointed out in the original post, it’s confusing to anyone else reading it: lockObject can’t be shared amongst threads and the lock block really isn’t protecting toggle and can/likely to get “maintained” into something that no longer works.
In the same grain, the same can be said for the original example of this code:
While this code works, it’s not apparently clear that the Thread.MemoryBarrier() is there so that our read of complete (and not toggle) isn’t optimized into a registry read. Regardless of the degree you might be able to depend on the compiler continuing to do this is up to you. The code is equally as valid and more clear if written to use Thread.VolatileRead, except for the fact that Thread.VolatileRead does not support the Boolean type. It can be re-written using Int32 instead. For example:
Which is more clear and shows your intent more explicitly.