I can't find any example of VolatileRead/write (try...) but still:
When should I use volatile
vs VolatileRead
?
AFAIK the whole purpose of volatile
is to create half fences so:
- For a READ operation, reads/writes (on other threads) which comes AFTER the current operation , won't pass before the fence. hence - we read the latest value.
Question #1
So why do I need the volatileRead
? it seems that volatile
already do the work.
Plus - in C# all writes are volatile (unlike say in Java), regardless of whether you write to a volatile
or a non-volatile field - and so I ask: Why do I need the volatileWrite
?
Question #2
This is the implementation for VolatileRead
:
[MethodImpl(MethodImplOptions.NoInlining)]public static int VolatileRead(ref int address){ int num = address; MemoryBarrier(); return num;}
Why the line int num = address;
is there? they already have the address argument which is clearly holding the value.