MSDN states that:
Reads and writes of other types, including
long
,ulong
,double
, anddecimal
, as well as user-defined types, need not be atomic.
C# 7.0 draft specification - Variables - 9.6 Atomicity of variable references
Will Volatile.Write(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:
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.
Unless you really need the performance, you probably want Interlocked
instead, either Interlocked.Exchange
or Interlocked.Read
.