是引用赋值和读取原子操作? [英] Are reference assignment and reading atomic operations?

查看:179
本文介绍了是引用赋值和读取原子操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个同一主题的几个问题,但与一般的变量(价值和引用类型) 从这个问题说:

分区我,的CLI规范规定第12.6.6:一个符合CLI须保证读写访问正确对齐的内存位置并不比本地字大小是原子时,所有的写访问到一个位置都较大相同的大小。

参考变量(即类)的指针,等于本地字大小,但我有几个疑惑:

均指保证在正确对齐的内存位置?

我不明白的最后一部分。这是什么意思?的......当所有的写访问的位置是相同的尺寸。

总之,是OBJ2保证在以下code中的循环?

的每次迭代有效

  MyClass类
{
    私人OtherClass m_Object;

    无效线程1()
    {
        而(真)
        {
            OtherClass OBJ1 =新OtherClass();
            m_Object = OBJ1;
        }
    }

    作废线程2()
    {
        而(真)
        {
            OtherClass OBJ2 = m_Object;
            //是OBJ2保证有效?
            obj2.Check();
        }
    }
}
 

解决方案

是的,一切都保证是正确对齐,除非你刻意去追求自己的方式错位的东西,这意味着基准分配/读取保证是原子。

在CLI规范第12.6.6接着说这样的:

  

除非显式布局控制(见   二分区(控制实例   布局))被用于改变默认   行为,数据元素不大于   自然字长(的大小   本地INT )应妥善   对齐。对象引用应   处理,就好像它们被存储在   本地字大小。

也有关于规范第12.6.2调整等进一步的细节。

请注意,在你的榜样code,读的线程2被保证是原子,但它的没有的保证实际看到由线程1所做的任何更改:无强制记忆障碍或波幅每个线程都可以使用 m_Object 字段的自己的观点而没有看到其他线程所做的更改。

因此​​,举例来说,线程1可以使(原子)写入自己的 m_Object 的看法,但数据永远都只有真正在一个寄存器或CPU举行缓存和从未comitted到主存储器。同样,线程2也可以使(原子)读 m_Object 的,但实际上从寄存器或CPU缓存中读取,而不是主存储器中。

I have found several questions about this same topic but related to general variables (value and reference types) The accepted answer from this question says:

Partition I, Section 12.6.6 of the CLI spec states: "A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size."

Reference variables (i.e. classes) are pointers, equal to the native word size, but I have a couple doubts:

Are references guaranteed to be in properly aligned memory locations?

I don't understand the last part. What does this mean? "...when all the write accesses to a location are the same size."

In short, is obj2 guaranteed to be valid in every iteration of the loop in the following code?

class MyClass
{
    private OtherClass m_Object;

    void Thread1()
    {
        while(true)
        {
            OtherClass obj1 = new OtherClass();
            m_Object = obj1;
        }
    }

    void Thread2()
    {
        while (true)
        {
            OtherClass obj2 = m_Object;
            // Is obj2 guaranteed to be valid?
            obj2.Check();
        }
    }
}

解决方案

Yes, everything is guaranteed to be properly aligned unless you deliberately go out of your way to misalign stuff, meaning that reference assignment/reading is guaranteed to be atomic.

Section 12.6.6 of the CLI spec goes on to say this:

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.

There are also further details about alignment etc in section 12.6.2 of the spec.

Note that in your example code, the read in thread 2 is guaranteed to be atomic, but it's not guaranteed to actually see any changes made by thread 1: without enforcing memory barriers or volatility each thread can use its own "view" of the m_Object field without ever seeing changes made by other threads.

So, for example, thread 1 could be making (atomic) writes into its own view of m_Object, but the data is only ever actually held in a register or CPU cache and never comitted to main memory. Similarly, thread 2 could also be making (atomic) reads of m_Object, but actually reading from a register or CPU cache rather than main memory.

这篇关于是引用赋值和读取原子操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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