Volatile.Read/Volatile.Write 是否为“double"?原子? [英] Does Volatile.Read / Volatile.Write for "double" atomic?
问题描述
MSDN 声明
long、ulong、double 和decimal,以及用户定义的类型,不保证是原子的5.5 变量引用的原子性
long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic 5.5 Atomicity of variable references
将 Volatile.Write 方法(Double%、Double)
作为原子操作工作?如果是这样,如何保证?
Will Volatile.Write Method (Double%, Double)
work as atomic operation? If so how this is guaranteed?
在一个线程中执行 Volatile.Write(ref mydouble, value);
和在另一个线程中执行 Volatile.Read(ref mydouble)
是否安全,其中 mydouble
有 double
类型吗?
Is it safe to do Volatile.Write(ref mydouble, value);
in one thread and Volatile.Read(ref mydouble)
in another where mydouble
has double
type?
这是一个普遍的问题.另一个问题 - 在这种特殊情况下我应该怎么做:
That was general question. Another question - what should I do in this particular situation:
- 2 个处理器至强 x64 服务器
- Windows + MS .NET 4.5
- 从不同线程读取/写入双精度
- 最小延迟(需要最小,因为我正在编写 HFT 软件)
推荐答案
除非您确实需要性能,否则您可能想要Interlocked
,或者Interlocked.交换
或Interlocked.Read
Unless you really need the performance, you probably want Interlocked
instead, either Interlocked.Exchange
or Interlocked.Read
更新: 不,Volatile 不是原子的,在 SMP(>1 个处理器)系统中假设如此是不安全的.在单处理器机器上是安全的.
Update: No, Volatile is not atomic, and it is not safe in an SMP (>1 processor) system to assume so. It is safe on a uniprocessor machine.
这篇关于Volatile.Read/Volatile.Write 是否为“double"?原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!