If a thread A spawns another thread B with the single purpose of writing to a variable V and then waits for it to terminate, are memory-barriers required to ensure that subsequent reads of V on thread A are fresh? I'm unsure if there any implicit barriers in the termination / joining operations that make them redundant.
Here's an example:
public static T ExecuteWithCustomStackSize<T>
(Func<T> func, int stackSize)
{
T result = default(T);
var thread = new Thread(
() =>
{
result = func();
Thread.MemoryBarrier(); // Required?
}
, stackSize);
thread.Start();
thread.Join();
Thread.MemoryBarrier(); // Required?
return result;
}
Are are either / both (or more) of the barriers in the above snippet required?
No, synchronization mechanisms generate implicit memory fences. All data modified by a thread will be visible after the thread is joined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With