Does Volatile.Read / Volatile.Write for “double” atomic?

女生的网名这么多〃 提交于 2019-12-19 07:35:25

问题


MSDN states that

long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic 5.5 Atomicity of variable references

Will Volatile.Write Method (Double%, Double) work as atomic operation? If so how this is guaranteed?

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 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)

回答1:


Unless you really need the performance, you probably want Interlocked instead, either Interlocked.Exchange or Interlocked.Read

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.




回答2:


If you really care about latency that much, then you need to stop writting to the same location from different threads. Can you expand on what you're solving?



来源:https://stackoverflow.com/questions/12435325/does-volatile-read-volatile-write-for-double-atomic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!