为双读原子的英特尔架构? [英] Is double read atomic on an Intel architecture?

查看:129
本文介绍了为双读原子的英特尔架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的同事正在对阅读的双重使用C#.NET 4.0的英特尔架构原子的参数。他争辩说,我们应该使用 Interlocked.Exchange 方法编写成,只是读取双精度值(在一些其他线程)保证是原子的。我的观点是,.NET不保证原子性,但他的说法是,在Intel架构,这是保证(也许不是在 AMD SPARC 等)。

My colleague and I are having an argument on atomicity of reading a double on an Intel architecture using C# .NET 4.0. He is arguing that we should use Interlocked.Exchange method for writing into a double, but just reading the double value (in some other thread) is guaranteed to be atomic. My argument is that .NET doesn't guarantee this atomicity, but his argument is that on an Intel architecture this is guaranteed (maybe not on AMD, SPARC, etc.).

任何Intel和.NET专家在此分享一些灯光?

Any Intel and .NET experts share some lights on this?

读者可以阅读陈旧(previous)的价值,而不是不正确的值(给垃圾值之前和之后写部分读取)。

Reader is OK to read a stale (previous) value, but not incorrect value (partial read before and after write giving a garbage value).

推荐答案

是的,没有。在32位处理器,它不能保证原子,因为双重是比天然字长较大。在64位处理器的正确对齐的访问是原子。 64位 CLI 保证一切多达64位读作原子。所以,你需要建立你的组件的 64 的(不是的任何CPU 的)。否则,如果您的组件可以在32位上运行,你最好使用互锁,看到的 <一个href="http://blogs.msdn.com/b/ericlippert/archive/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two.aspx"相对=nofollow>原子性,波动性和永恒性是不同的,第二部分 的由Eric利珀特。我觉得你可以依靠微软 CLR

Yes and no. On 32-bit processors, it is not guaranteed atomic, because double is larger than the native wordsize. On a 64-bit processor properly aligned access is atomic. The 64-bit CLI guarantees everything up to a 64-bit read as atomic. So you'd need to build your assembly for x64 (not Any CPU). Otherwise if your assembly may be run on 32-bit, you better use Interlocked, see Atomicity, volatility and immutability are different, part two by Eric Lippert. I think you can rely on Eric Lippert's knowledge of the Microsoft CLR.

在ECMA CLI标准也支持这一点,尽管C#本身并不能保证它:

The ECMA CLI standard also supports this, even though C# itself does not guarantee it:

一个符合CLI须保证读取和写入访问   正确对齐的内存位置并不比本地字大小大   (原生类型为int的大小)是原子(见§I.12.6.2)

A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size (the size of type native int) is atomic (see §I.12.6.2)

它还说,在哪里操作都是原子处理器,互锁方法通常编译为一个单一的指令,所以在我的书没有在使用它没有性能损失。而另一方面,有可能是一个糟糕的点球不使用它时,你应该。

It also says that on processors where operations are atomic, Interlocked methods are often compiled to a single instruction, so in my book there is no performance penalty in using it. On the other hand, there may be a worse penalty to not using it when you should.

另一个相关的堆栈溢出问题的 什么操作都是原子在C#? 的。

Another related Stack Overflow question is What operations are atomic in C#?.

这篇关于为双读原子的英特尔架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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