在其CPU架构是写为int"隐含波动"使用CLR(和变体)? [英] On which CPU architectures are writes to an int "implicitly volatile" using the CLR (and variants)?

查看:119
本文介绍了在其CPU架构是写为int"隐含波动"使用CLR(和变体)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

笔者近日了解到这里以下是线程安全与x86 CLR(不一定是ECMA标准的CLR)

I recently learnt here the following is thread-safe on x86 CPU with the x86 CLR (not necessarily ECMA standard CLR)

public class SometimesThreadSafe
{
    private int value;
    public int Value { get { return value; } }

    public void Update()
    {
        Interlocked.Add(ref value, 47);
    }
}

这是因为写的 INT 在这样的架构,确保任何有价值的其他CPU高速缓存同步。 在的ARM CPU然而,这不是线程安全的!由于读数值由不同的线程可以从CPU高速缓存中读取的旧副本。

This is because writing to an int on such architectures ensures any other CPU caches of value are synched. On ARM CPUs however this is not thread safe! As reading value from a different thread could read an old copy from a CPU cache.

所以,问题是什么CPU架构和CLR的版本是什么和它的变体,例如单声道,这是线程安全的?

So the question is on what CPU architectures and with what versions of the CLR and it's variants, e.g. Mono, is this thread-safe?

推荐答案

线程安全是不完全适用于这种正确的字代码。 Value属性的getter的获取与更新()方法完全不同步的,所以你得到的价值是完全不可预测的。其中的从不的看到更新。

"Thread-safe" isn't exactly the proper word to apply to this kind of code. The access to the Value property getter is completely unsynchronized with the Update() method so the value you get is entirely unpredictable. Including never seeing the update.

在这里,如果Value属性的getter是唯一关心的原子。换句话说,如果你永远能够观察到,其中一些字节将已被更新(改变)和一些没有物业的部分更新值。这是通过在CLI规格保证。 ECMA-335,部分I.12.6.6,原子读取和写入:

The only concern you have here if the Value property getter is atomic. In other words, if you'll ever be able to observe a partially updated value of the property where some bytes will have been changed by Update() and some not. This is guaranteed by the CLI spec. Ecma-335, section I.12.6.6, "Atomic reads and writes":

一个符合CLI须保证读写访问以正确对齐没有比天然字长(型天然int的长度)更大的存储器
位置是原子
(参见§I.12.6.2)当所有的写访问的位置是相同的尺寸。原子写入应
改变比写其他任何位。除非显式布局控制(见
分区II(控制实例布局))来改变默认行为,数据元素没有
比天然字大小(本机int的长度)放大应妥善对齐。就好像它们存储在本地字长对象
引用应进行治疗。

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) when all the write accesses to a location are the same size. Atomic writes shall alter no bits other than those written. Unless explicit layout control (see Partition II (Controlling Instance Layout)) is used to alter the default behavior, data elements no larger than the natural word size (the size of a native int) shall be properly aligned. Object references shall be treated as though they are stored in the native word size.

这保证是有点在淡化在C#语言规范,第5.5节,变量引用的原子。它避免承担IntPtr的大小依赖:

This guarantee is toned down somewhat in the C# Language Specification, chapter 5.5, "Atomicity of variable references". It avoids taking a dependency on the size of IntPtr:

读取及以下数据类型的读写是原子:布尔,焦炭,字节,为sbyte,短,USHORT,UINT,整型,浮点和引用类型。此外,当枚举类型与以前的列表基础类型写操作也是原子的。读取和其他类型,包括长,ULONG,双,和小数,以及用户定义类型的写,不能保证是原子。

Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list are also atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic.

安美居,而不是问题的 INT 的在任何架构。

Anyhoo, not a problem for int on any architecture.

如果线程安全是真正的关心那么这段代码是完全错误的。它不是线程安全上的任何的架构。的隐含波动的概念并没有在.NET内存模型存在。东西86抖动优化利用了优势,这将属性的支持字段存储在CPU寄存器,而不是从内存中更新。你永远不会看到更新。明确声明它的挥发性的需要抑制这种优化。

If thread-safety is truly your concern then this code is just plain wrong. It is not thread-safe on any architecture. The notion of "implicitly volatile" doesn't exist in the .NET memory model. Something the x86 jitter optimizer takes advantage of, it will store the backing field of the property in a cpu register and not update it from memory. You'll never observe the update. Explicitly declaring it volatile is required to suppress this optimization.

这篇关于在其CPU架构是写为int"隐含波动"使用CLR(和变体)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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