If I understand correctly, in C # a blocking block guarantees exclusive access to a set of instructions ...
Right The specification guarantees this.
but it also ensures that any reads from memory reflect the latest version of that memory in any processor cache.
The C # specification says nothing about the "CPU cache". You left the area of ββwhat is guaranteed by the specification, and entered the area of ββimplementation details. There is no requirement that a C # implementation be executed on a processor with any particular cache architecture.
Is there any magic that only variables read and written in the code protected by the blocking block are guaranteed to be fresh, or do the memory barriers used in the implementation of the blocking ensure that all memory is now equally fresh for all threads?
Instead of trying to make out your question or question, say what is actually guaranteed by the language. Special effect:
- Any write to a variable, unstable or not
- Any reading of a volatile field
- Any throws
The order of special effects is stored at specific special points:
- Read and write variable fields
- locks
- thread creation and termination
Runtime is required to ensure that special effects are ordered sequentially with special points. So, if the volatile field is read before the lock, and then the record is written after that, the reading cannot be moved after the record.
So how is this achieved at runtime? Beats me up. But runtime, of course, is not required to "ensure that all memory is fresh for all threads." Runtime is required to ensure that certain readings, records, and outliers are performed in chronological order with respect to specific points and that is all.
Runtime, in particular, does not require all threads to follow the same order.
Finally, I always end such discussions, pointing to you here:
http://blog.coverity.com/2014/03/26/reordering-optimizations/
After reading this, you should get a mark for the terrible things that can happen even on x86 when you play at ease about castles.