If I create a variable on one thread then block using a ManualResetEvent
's WaitOne()
method until another thread assigns a value to the same variable and signals the EventWaitHandel
. When I come to read the variable on the first thread am I guaranteed to always get the value just assigned by the other thread?
(I fear I could not get a value from a CPU cache because of some optimisation as I have not used any memory barriers as far as I know).
e.g.
var str = "multi-threading is hard!";
var mre = new ManualResetEvent(false);
Task.Factory.StartNew(() =>
{
str = Console.ReadLine();
mre.Set();
));
mre.WaitOne();
Console.WriteLine(str);