Are reads/writes of 64-bit values atomic on a 64-bit cpu? [closed]

梦想与她 提交于 2019-12-10 20:25:34

问题


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.

  1. But are reads/writes atomic on 64-bit cpus?
  2. If so, does the (Windows) OS have to be 64-bit as well?
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!