If thread A spawns another thread B for the sole purpose of writing to variable V and then waits for it to complete, are memory barriers necessary to ensure that subsequent readings of V in thread A are fresh? I'm not sure if there are any implicit barriers in termination / join operations that make them redundant.
Here is an example:
public static T ExecuteWithCustomStackSize<T>
(Func<T> func, int stackSize)
{
T result = default(T);
var thread = new Thread(
() =>
{
result = func();
Thread.MemoryBarrier();
}
, stackSize);
thread.Start();
thread.Join();
Thread.MemoryBarrier();
return result;
}
Are both (or more) barriers in the above fragment?
source
share