The implementation of Volatile.Read merely inserts a memory barrier after the read:
public static int Read(ref int location)
{
var value = location;
Thread.MemoryBarrier();
return value;
}
Thus, usage of the method like so...
return Volatile.Read(ref _a) + Volatile.Read(ref _b);
...would be equivalent to:
var a = _a;
Thread.MemoryBarrier();
var b = _b;
Thread.MemoryBarrier();
return a + b;
Given the above, wouldn't the resulting behavior be identical if the parameter was not a ref?
public static int Read(int value)
{
Thread.MemoryBarrier();
return value;
}
I guess that the ref parameter was used simply to prevent programmers from passing things other than variables, such as Volatile.Read(2 + 3). Can anyone see any other reason for the ref when variables are passed in?