G
Guest
Hi All,
I'm trying to get my head around synchronization.
Documentation seems to say that creating a volatile field gives a
memorybarrier.
Great! But when I do a little performance testing Thread.MemoryBarrier is
40 times slower than accessing a volatile int. Also I don't see any
significant difference between the time to increment a volatile int and a
non-volatile int.
Could the difference all be attributable to the overhead of a function call?
When I read documentation on Thread.VolatileWrite it says that all other
processors are guaranteed to see your write immediately.
But when I use reflector to decompile VolatileWrite I get:
public static void VolatileWrite(ref int address, int value)
{
Thread.MemoryBarrier();
address = value;
}
Now if the MemoryBarrier is before the write couldn't I throw the
MemoryBarrier on processor1 read value (through the cache) on processor 2 and
then write to address on processor 1 now the next read of address on
processor2 gets its "cached" value from before the new write.
(Boy it's as hard to write about this stuff as it is to read).
processor1: Thread.MemoryBarrier
processor2: read address getting old_value (old_value cached)
processor1: address = new_value
processor2: read address still returning old_value.
VolatileWrite the way it is written seems to guarantee that any write before
it will get to main memory before this write but makes no guarantees about
this write.
Thanks
I'm trying to get my head around synchronization.
Documentation seems to say that creating a volatile field gives a
memorybarrier.
Great! But when I do a little performance testing Thread.MemoryBarrier is
40 times slower than accessing a volatile int. Also I don't see any
significant difference between the time to increment a volatile int and a
non-volatile int.
Could the difference all be attributable to the overhead of a function call?
When I read documentation on Thread.VolatileWrite it says that all other
processors are guaranteed to see your write immediately.
But when I use reflector to decompile VolatileWrite I get:
public static void VolatileWrite(ref int address, int value)
{
Thread.MemoryBarrier();
address = value;
}
Now if the MemoryBarrier is before the write couldn't I throw the
MemoryBarrier on processor1 read value (through the cache) on processor 2 and
then write to address on processor 1 now the next read of address on
processor2 gets its "cached" value from before the new write.
(Boy it's as hard to write about this stuff as it is to read).
processor1: Thread.MemoryBarrier
processor2: read address getting old_value (old_value cached)
processor1: address = new_value
processor2: read address still returning old_value.
VolatileWrite the way it is written seems to guarantee that any write before
it will get to main memory before this write but makes no guarantees about
this write.
Thanks