Let's say I've got a struct that consist of 100 bytes. What guarantees have I got about the following code?
m_myLargeStruct = someValue; // copying 100 bytes
Thread.MemoryBarrier();
// Executed by another thread, after "Thread.MemoryBarrier" was called by the first thread
Console.WriteLine(m_myLargeStruct.ToString());
Does the memory model guarantee that the 100 bytes copy will be complete after the placement of the memory barrier? or do memory barriers only apply for types that are at the size of the processor's architecture? (4 bytes for 32bit and 8 bytes to 64bit).
Is this the reason why the volatile keyword only applies for primitive types? (if i declare an 8 byte member as volatile, this means that an interlocked instrinct will be used to change it's value? [since atomicity isn't guaranteed for types larger than 4 bytes on 32bit machines]).
I hope I was clear enough.. :)
Thanks
Clearly the answer is no, or rather, you have no guarantees about anything. Nothing prevents the operating system from swapping out the thread that is writing to the 100 byte struct before starting the thread that prints out the 100 byte struct.
A memory barrier is used when you want to coordinate access to data through a flag or some other atomic value. I don't know what exactly you are trying to do, so I can't give you good example code about how you should do it.