Is Interlocked.Read(ref long)
"optimized away" on 64-bit architectures? I.e. if I am writing a library which could be used by both architectures, should I be concerned about performance impact of using Interlocked.Read
unnecessarily on 64-bit CPUs?
I thought about using something like this, so I am wondering if this makes sense:
// X64 is a preprocessor constant set for x64 builds
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Read(ref long address)
{
#if X64
// atomic on 64-bit processors
return address;
#else
// if I got it right, this creates a full memory barrier
return Interlocked.Read(ref address);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write(ref long address, long value)
{
#if X64
// atomic on 64-bit processors
address = value;
#else
// if I got it right, this creates a full memory barrier
Interlocked.Exchange(ref address, value);
#endif
}