Simplified the question to give a more clear representation of what I'm actually asking
I have two threads, call them A and B. They share one object of type Foo which has a field called Name and is stored in an array of type Foo[] at index 0. The threads will always access index 0 in a order which is guaranteed by the system already, so there is no race condition of thread B getting before thread A.
The order is this.
// Thread A
array[0].Name = "Jason";
// Thread B
string theName = array[0].Name
As I said this order is already guaranteed, there is no way for thread B to read the value before thread A
What I want to ensure is two things:
- Both threads get the latest object at index 0.
- Thread
Balways gets the latest value in the .Name field
Marking Name as volatile is not an option, as the real objects are a lot more complex and even have custom structs which can't even have the volatile attribute attached to them.
Now, satisfying 1 is easy (always getting the latest object), you can do a .VolatileRead:
// Thread A
Foo obj = (Foo)Thread.VolatileRead(ref array[0]);
obj.Name = "Jason";
// Thread B
Foo obj = (Foo)Thread.VolatileRead(ref array[0]);
string theName = obj.Name
Or you can insert a memory barrier:
// Thread A
array[0].Name = "Jason";
Thread.MemoryBarrier();
// Thread B
Thread.MemoryBarrier();
string theName = array[0].Name
So my question is: Is this enough to also satisfy condition 2? That I always get the latest value from the fields of the object I read out? If the object at index 0 has not changed, but the Name has. Will doing a VolatileRead or a MemoryBarrier on index 0 make sure all the fields IN the object at index 0 also get their latest values?
System.Collections.Concurrentnamespace.