Interlocked.Exchange和Volatile.Write区别? [英] difference between Interlocked.Exchange and Volatile.Write?
问题描述
什么是Interlocked.Exchange和Volatile.Write区别?
What is the difference between Interlocked.Exchange and Volatile.Write?
一些变量的两种方法更新值。有人可以总结时要使用他们每个人?
Both methods update value of some variable. Can someone summarize when to use each of them?
HTTP ://msdn.microsoft.com/ru-ru/library/bb337971
和的 http://msdn.microsoft.com/en-us/library/gg712713.aspx
在我特别需要更新我的数组的双项,我想另一个线程看到最新鲜的价值。什么是首选? Interlocked.Exchange(ARR文献[3],myvalue的)
或 Volatile.Write(ARR文献[3],资讯);
其中,改编
声明为双击
?
In particular I need to update double item of my array, and I want another thread to see the freshest value. What is preferred? Interlocked.Exchange(ref arr[3], myValue)
or Volatile.Write(ref arr[3], info);
where arr
is declared as double
?
================================================== ==========================
真实的例子,我宣布这样的双数组:
============================================================================ Real example, I declare double array like that:
private double[] _cachedProduct;
在一个线程更新它这样的:
In one thread I update it like that:
_cachedProduct[instrumentId] = calcValue;
...
are.Set();
在另一个线程我读这阵这样的:
In another thread I read this array like that:
while(true) {
are.WaitOne();
...
result += _cachedProduct[instrumentId];
...
}
对于我来说,它只是正常工作原样。然而,为了确保它会永远工作不管什么,似乎我要补充或者 Volatile.Write
或 Interlocked.Exchange
。由于二次更新不guaranted是原子 http://msdn.microsoft.com /en-us/library/aa691278%28VS.71%29.aspx
For me it just works fine as is. However to make sure "it will always work" no matter what it seems I should add either Volatile.Write
or Interlocked.Exchange
. Because double update is not guaranted to be atomic http://msdn.microsoft.com/en-us/library/aa691278%28VS.71%29.aspx
在回答这个问题,我想看到的挥发性详细比较和互锁类。我们为什么需要2班?哪一个,以及何时使用?
In the answer to this question I want to see detailed comparision of Volatile and Interlocked classes. Why we need 2 classes? Which one and when to use?
推荐答案
在Interlocked.Exchange使用,保证原子操作的处理器指令。
the Interlocked.Exchange uses a processor instruction that guarantees an atomic operation.
Volatile.Write不相同,但它也包括了内存屏障操作。
我认为微软增加Volatile.Write上DOTNET的4.5由于对Windows 8的英特尔和ARM处理器的支持ARM处理器的不同,对内存操作重新排序。
The Volatile.Write does the same but it also includes a memory barrier operation. I think Microsoft added Volatile.Write on DotNet 4.5 due to support of ARM processors on Windows 8. Intel and ARM processors differs on memory operation reordering.
在英特尔,必须保证该存储器访问操作将在它们被发出,或者至少一个写操作将不会被重新排序的相同顺序进行。
On Intel, you have a guarantee that memory access operations will be done in the same order they are issued, or at least that a write operation won't be reordered.
从英特尔®64和IA-32架构软件开发人员手册,第8章:
From Intel® 64 and IA-32 Architectures Software Developer’s Manual, Chapter 8:
在P6 8.2.2内存排序和最近的处理器系列英特尔酷睿2双核,英特尔凌动,英特尔酷睿双核,奔腾4和P6系列
处理器还采用了处理器有序内存排序模型可以
进一步被定义为具有存储缓冲转发写命令道,
如下该模型可表征。
8.2.2 Memory Ordering in P6 and More Recent Processor Families The Intel Core 2 Duo, Intel Atom, Intel Core Duo, Pentium 4, and P6 family processors also use a processor-ordered memory-ordering model that can be further defined as "write ordered with store-buffer forwarding." This model can be characterized as follows.
在ARM你没有这样的保证,因此需要一个内存屏障。一个ARM博客解释这个可以在这里找到:的 http://blogs.arm.com/software-enablement/594-memory-access-ordering-part-3-memory-access-ordering-in-the-arm-建筑/
On ARM you don't have this kind of guarantee, so a memory barrier is required. An ARM blog explaining this can be found here: http://blogs.arm.com/software-enablement/594-memory-access-ordering-part-3-memory-access-ordering-in-the-arm-architecture/
在你的榜样,与双操作不保证是原子,我会建议一个锁来访问它。请记住,你必须使用你的代码的两个部分,阅读和设置该值时锁定。
In your example, as the operation with double is not guaranteed to be atomic, I would recommend a lock to access it. Remember that you have to use the lock on both parts of your code, when reading and setting the value.
一个更完整的例子会更好地回答你的问题,因为目前尚不清楚这些值设置后会发生什么。对于一个矢量,如果你有比作家更多的读者,可考虑使用一个ReaderWriterLockSlim对象:的 http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx
A more complete example would be better to answer your question, as it is not clear what happens after these values are set. For a vector, if you have more readers than writers, consider the use of a ReaderWriterLockSlim object: http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx
数线程和读取的频率/写操作可以显着改变您的锁策略。
The number of threads and the frequency of read/writes can change dramatically your locking strategy.
这篇关于Interlocked.Exchange和Volatile.Write区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!