挥发性在.NET 4.0引用类型 [英] volatile for reference type in .net 4.0

查看:90
本文介绍了挥发性在.NET 4.0引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我弄糊涂了对挥发性为引用类型。

据我了解,对于基本类型,挥发性可以立即反映在另一个线程值的变化。作参考的类型,它可以立即反映在地址更改。然而,关于所述对象的内容。他们是否仍缓存?

(假设 List.Add()是一个原子操作)

例如,我有:

  A级
{
     挥发性名单,其中,字符串>清单;
     无效的AddValue()
     {
        list.Add(值);
     }

}
 

如果一个线程调用函数的AddValue ,列出的地址并没有改变,将另一个线程得到更新该列表的内容的变化,或内容可以被高速缓存为每个线程,它不为其他线程更新?

解决方案
  

据我了解,对于基本类型,挥发性能反映从另一个线程价值变动立即

您在至少在三个方面理解不正确。你不应该尝试使用挥发性的,直到你深刻体会到的所有的有关弱内存模型,获取和释放的语义,以及它们如何影响你的程序。

首先,要明确挥发性影响的变量,不是

二,波动不会影响包含值类型的值比它影响包含引用变量有什么不同的变量。

三,动荡并不意味着其他线程该值的变化是立即可见。挥发性意味着变量具有的获取和释放语义的。挥发性影响的的顺序可以观察到的存储器突变副作用从一个特定的线程的发生。即存在突变的一致普遍的秩序,在那个为了这些突变可以从所有的线程瞬间观察到的这个想法是不是由内存模型做了保证。

  

然而,关于对象的内容?

怎么样呢?通过引用类型的易失性可变称为存储位置不必具有任何特殊的螺纹特征

  

如果一个线程调用函数的AddValue,列出的地址并没有改变,将另一个线程得到更新该列表的内容的转变。<​​/ P>

都能跟得上。为什么会呢?其它线程可能是在不同的处理器,并且该处理器的高速缓存可能具有$ P $对 - 加载包含正在备份列表中的数组的地址的页面。突变的列表可能已经改变包含该数组的地址来指一些完全不同的位置的存储位置。

当然,列表类不是线程安全摆在首位。如果你不锁进入名单,然后名单可以简单地崩溃,死了,当你尝试这样做。

您不需要挥发;你所需要的就是把线程锁周围访问列表。由于线程锁引起全围栏你不应该需要通过挥发介绍一半围栏。

I got confused on volatile for reference type .

I understand that for primitive type, volatile can reflect value changes from another thread immediately. For reference type, it can reflect the address changes immediately. However, what about the content of the object. Are they still cached?

(Assuming List.Add() is an atomic operation)

For example, I have:

class A
{
     volatile List<String> list;
     void AddValue()
     {
        list.Add("a value");
     }

}

If one thread calls the function AddValue, the address of list does not change, will another thread get updated about the "content" change of the list, or the content may be cached for each thread and it doesn't update for other threads?

解决方案

I understand that for primitive type, volatile can reflect value changes from another thread immediately

You understand incorrectly in at least three ways. You should not attempt to use volatile until you deeply understand everything about weak memory models, acquire and release semantics, and how they affect your program.

First off, be clear that volatile affects variables, not values.

Second, volatile does not affect variables that contain values of value types any differently than it affects variables that contain references.

Third, volatile does not mean that value changes from other threads are visible immediately. Volatile means that variables have acquire and release semantics. Volatile affects the order in which side effects of memory mutations can be observed to happen from a particular thread. The idea that there exists a consistent universal order of mutations and that those mutations in that order can be observed instantaneously from all threads is not a guarantee made by the memory model.

However, what about the content of the object?

What about it? The storage location referred to by a volatile variable of reference type need not have any particular threading characteristics.

If one thread calls the function AddValue, the address of list does not change, will another thread get updated about the "content" change of the list.

Nope. Why would it? That other thread might be on a different processor, and that processor cache might have pre-loaded the page that contains the address of the array that is backing the list. Mutating the list might have changed the storage location that contains the address of the array to refer to some completely different location.

Of course, the list class is not threadsafe in the first place. If you're not locking access to the list then the list can simply crash and die when you try to do this.

You don't need volatile; what you need is to put thread locks around accesses to the list. Since thread locks induce full fences you should not need half fences introduced by volatile.

这篇关于挥发性在.NET 4.0引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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