You could use Interlocked.CompareExchange to handle publishing without explicitly locking:
let mutable data = [1;2;3;4;5]
let newValue = 0
// To publish:
let mutable tmp = data;
while not(tmp.Equals(Interlocked.CompareExchange(&data, newValue::data, tmp))) do
tmp <- data
This would likely provide a small benefit if you have synchronized writers.
If you decide you do want consumers to always have the latest data, a ReaderWriterLockSlim
would allow you to completely synchronize the data without forcing reads to block on each call.
That could look something like:
let mutable data = [1;2;3;4;5]
let rwl = ReaderWriterLockSlim()
let newValue = 0
// To publish:
let publish newValue =
rwl.EnterWriteLock()
try
data <- newValue :: data
finally
rwl.ExitWriteLock()
// To read:
let readCurrent =
rwl.EnterReadLock()
try
data
finally
rwl.ExitReadLock()