在 64 位 CPU 上读/写 64 位值是原子的吗? [英] Are reads/writes of 64-bit values atomic on a 64-bit cpu?

查看:35
本文介绍了在 64 位 CPU 上读/写 64 位值是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 32 位机器上,在 .net 中读取和写入 64 位值曾经是非原子的,即在读取时,我可以读取一个从未写入内存的值,由一个较旧的 32-一半,新的一半.

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. 但是在 64 位 CPU 上读/写是原子的吗?
  2. 如果是这样,(Windows) 操作系统是否也必须是 64 位的?
  3. 如果是这样,应用程序是否也必须是 64 位 .net 版本?

推荐答案

来自 这个答案,如果你在一个 64 位系统并使用 64 位 CLR,那么它们是原子的.答案引用了 Eric Lippert 的这篇博文:https://blogs.msdn.microsoft.com/ericlippert/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two/

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/

CLI 规范实际上提供了更强的保证.CLI 保证读取和写入处理器自然指针大小(或更小)的值类型变量是原子的;如果您在 64 位版本的 CLR 中的 64 位操作系统上运行 C# 代码,那么读取和写入 64 位双精度和长整数也保证是原子的.C# 语言不能保证这一点,但运行时规范可以.(如果您在某些 CLI 实现未实现的环境中运行 C# 代码,那么您当然不能依赖该保证;如果您想知道他们提供什么保证,请联系向您出售运行时的供应商.)

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.)

这篇关于在 64 位 CPU 上读/写 64 位值是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆