问题描述
MSDN指出
将可以作为原子操作吗?如果是这样,如何保证?
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个处理器Xeon x64服务器
- Windows + MS .NET 4.5
- 从不同线程读/写两次
- 最小延迟(需要最小,因为我正在编写HFT软件)
- 2 processors Xeon x64 server
- Windows + MS .NET 4.5
- read/write double from different threads
- SMALLEST latency (need smallest because i'm writing HFT software)
推荐答案
除非您真的需要性能,否则您可能希望 Interlocked
,而是 Interlocked.Exchange
或 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”?原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!