问题
On 32-bit machines, reading and writing of 64-bit values in .net used to be non-atomic, i.e. when reading, I could read a value that had never been written to memory, consisting of one older 32-bit half, and one newer half.
- But are reads/writes atomic on 64-bit cpus?
- If so, does the (Windows) OS have to be 64-bit as well?
- And if so, would even the app have to be a 64-bit .net build?
回答1:
From this answer, if you're on a 64 bit system and using a 64-bit CLR, then they are atomic. The answer references this blog post from Eric Lippert: https://blogs.msdn.microsoft.com/ericlippert/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two/
The CLI specification actually makes stronger guarantees. The CLI guarantees that reads and writes of variables of value types that are the size (or smaller) of the processor's natural pointer size are atomic; if you are running C# code on a 64 bit operating system in a 64 bit version of the CLR then reads and writes of 64 bit doubles and long integers are also guaranteed to be atomic. The C# language does not guarantee that, but the runtime spec does. (If you are running C# code in some environment that is not implemented by some implementation of the CLI then of course you cannot rely upon that guarantee; contact the vendor who sold you the runtime if you want to know what guarantees they provide.)
来源:https://stackoverflow.com/questions/52959183/are-reads-writes-of-64-bit-values-atomic-on-a-64-bit-cpu