When you have a
long[] myArray = new long[256];
whose items are changed by multiple threads using
Interlocked.Increment(ref myArray[x])
it sure won't be possible to get a snapshot of myArray
at one point in time, as there are non-locked writes going on concurrently, so I'm not trying to get that.
So do I really have to Volatile.Read
of every single element like this to get a copy of all values at some point in the past?
long[] copy = new long[256];
for (int i = 0; i < 256; i++)
copy[i] = Volatile.Read(ref myArray[i]);
As I'm not interested in a snapshot at one point in time, stale values are not a problem, but as 64-bit non-volatile reads are not atomic, I fear that the following may give me a pre-increment half of a long, and a post-increment half, which may give a value that never existed in the array.
long[] copy = new long[256];
for (int i = 0; i < 256; i++)
copy[i] = myArray[i];
So is the Volatile.Read
variant the proper choice, given I don't want to use any locking?