For me it looks quite strange, and like a bug.
This code in Release mode in Visual Studio 2019 provides infinite loop.
class Program
{
private static int _a;
static void Main(string[] args)
{
_a = 1;
while (_a == 1)
{
Console.WriteLine(_a);
_a = 0;
}
}
}
volatile
or Thread.MemoryBarrier();
(after _a = 0;
) solves the issue. Don't think I had such issue with VS2015. Is this correct behavior? What exact part is optimized?