I have one NonVolatileTest class:
public class NonVolatileTest
{
public bool _loop = true;
}
and I have two code examples:
1
private static void Main(string[] args)
{
NonVolatileTest t = new NonVolatileTest();
Task.Run(() => { t._loop = false; });
while (t._loop) ;
Console.WriteLine("terminated");
Console.ReadLine();
}
2:
private static void Main(string[] args)
{
NonVolatileTest t = new NonVolatileTest();
Task.Run(() => { t._loop = false; });
Task.Run(() =>
{
while (t._loop) ;
Console.WriteLine("terminated");
});
Console.ReadLine();
}
In the first example, everything works as expected, and the while loop never ends, but in the second example everything works, as stated, the _loop field is mutable.
Why?
PS. VS 2013, .NET 4.5, x64 Release Mode and Ctrl + F5
Hypothesis:
This error may be related to TaskScheduler. I think, before the JIT poses a second task to compile and run, the first task has been completed, so the JIT takes on a changed value.
source
share