在C#中使用volatile(Thread.VolatileRead / Thread.VolatileWrite) [英] Use of volatile (Thread.VolatileRead/ Thread.VolatileWrite) in C#

查看:352
本文介绍了在C#中使用volatile(Thread.VolatileRead / Thread.VolatileWrite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多CPU的机器,我需要使用挥发性的读取访问共享状态(_data中的示例代码如下)在多线程程序运行/写操作,以确保正确性。

In a multi-threaded program running on a multi-cpu machine do I need to access shared state ( _data in the example code below) using volatile read/writes to ensure correctness.

换言之,可以堆物体上的CPU缓存

In other words, can heap objects be cached on the cpu?

使用下面的例子中,并假设多线程将访问的GetValue和添加方法,我需要的ThreadA要能够添加数据(使用Add方法),并ThreadB才能够看到/得到增加的数据立即(使用GetValue方法)。所以,做我需要添加挥发性读取/写入_data保证呢?基本上我不想在ThreadA中的CPU缓存添加的数据。

Using the example below and assuming multi-threads will access the GetValue and Add methods, I need ThreadA to be able to add data (using the Add Method) and ThreadB to be able to see/get that added data immediately (using the GetValue method). So do I need to add volatile reads/writes to _data to ensure this? Basically I don’t want to added data to be cached on ThreadA’s cpu.

/我不是锁定(独占实施线程访问)的代码需要超快,我不去除_data任何数据,所以我并不需要锁定_data。

/ I am not Locking (enforcing exclusive thread access) as the code needs to be ultra-fast and I am not removing any data from _data so I don’t need to lock _data.

感谢。

的更新的 * 的**的 * 的**的 * 的**的 * **的 * 的***

Update ****************

显然,你们认为会无锁的使用这个例子是坏主意。但什么副作用或异常我可能会面临在这里?

Obviously you guys think going lock-free using this example is bad idea. But what side effects or exceptions could I face here?

可以,如果1线程迭代的值进行读,另一个线程正在迭代的值的字典类型抛出一个异常更新?或者,将我刚刚经历脏读(这将是我的情况很好)?

Could the Dictionary type throw an exception if 1 thread is iterating the values for read and another thread is iterating the values for update? Or would I just experience "dirty reads" (which would be fine in my case)?

的最终更新的 * 的**的 * 的**的 * 的**的 * 的**的 * 的***

End Update ****************

public sealed class Data
{
    private volatile readonly Dictionary<string, double> _data = new Dictionary<string, double>();

    public double GetVaule(string key)
    {
        double value;
        if (!_data.TryGetValue(key, out value))
        {
            throw new ArgumentException(string.Format("Key {0} does not exist.", key));
        }
        return value;
    }

    public void Add(string key, double value)
    {
        _data.Add(key, value);
    }

    public void Clear()
    {
        _data.Clear();
    }
}






感谢您的答复。关于锁,这些方法几乎不断受到多张线程调用,所以我的问题是有争议的锁不是实际的锁定操作。


Thanks for the replies. Regarding the locks, the methods are pretty much constantly called by mulitple threads so my problem is with contested locks not the actual lock operation.

所以我的问题是关于CPU的缓存,可以堆对象(_data实例字段)在CPU缓存?我是否使用挥发性读/写操作所需要的访问_data领域?

So my question is about cpu caching, can heap objects (the _data instance field) be cached on a cpu? Do i need the access the _data field using volatile reads/writes?

/另外,我坚持的.Net 2.0。

/Also, I am stuck with .Net 2.0.

感谢您的帮助。

推荐答案

MSDN文档词典< TKEY的,TValue> 说,它是多个的读者,但他们不给一个作家,多个读卡器保证一些其他类的功能。总之,我不会这么做

The MSDN docs for Dictionary<TKey, TValue> say that it's safe for multiple readers but they don't give the "one writer, multiple readers" guarantee that some other classes do. In short, I wouldn't do this.

您说你避免锁定,因为你需要的代码是超高速 - 有你的试过的锁定看到的开销是什么?无可争议的锁是很便宜的,当锁是兵家必争之地,当你从增加的安全受益的。我当然决定担心无锁解决方案的并发问题之前大量资料这个。 ReaderWriterLockSlim 可能是如果你实际上得到多个读者有用,但它听起来像是你有一个单一的阅读器和一个作家,至少在此刻 - 简单的锁定将在这种情况下,更容易。

You say you're avoiding locking because you need the code to be "ultra-fast" - have you tried locking to see what the overhead is? Uncontested locks are very cheap, and when the lock is contested that's when you're benefiting from the added safety. I'd certainly profile this extensively before deciding to worry about the concurrency issues of a lock-free solution. ReaderWriterLockSlim may be useful if you've actually got multiple readers, but it sounds like you've got a single reader and a single writer, at least at the moment - simple locking will be easier in this case.

这篇关于在C#中使用volatile(Thread.VolatileRead / Thread.VolatileWrite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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