I want to use Thread.VolatileWrite() (or an equivalent function) to change the value of a T[] field, so that the updated value is immediately visible to all other threads. However, the method does not provide a generic version, and I'm not able to use the Object overload since it takes a ref parameter.
Is there an alternative? Would Interlocked.Exchange<T> do the job? Is there a better way to achieve what I want to do?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With