VolatileWrite
is only useful if you also use VolatileRead
; the volatile
modifier is easier to use, but I wonder if it wouldn't be simpler to use synchronization - perhaps a ReaderWriterLockSlim
(since it sounds like you have lots of readers).
Interlocked.Exchange<T>
simply performs the get and set as an atomic operation; it doesn't (AFAIK) make any volatility claims (indeed, the compiler even tells you that a volatile
field won't be treated as volatile
when used as a ref
argument).
Edit to clarify "it doesn't (AFAIK) make any volatility claims" - the behaviour of Interlocked
is predictable, and you should see updates immediately on other thread as long as they also use Interlocked
; my point is that if one thread uses Interlocked
, and another relies on volatile
(to talk to the same field), I don't know of any guarantees. Stick to one or the other for all access.