Given the following simple code:
class Program { static bool finish = false; static void Main(string[] args) { new Thread(ThreadProc).Start(); int x = 0; while (!finish) { x++; } } static void ThreadProc() { Thread.Sleep(1000); finish = true; } }
and running it in release mode using MSVS2015 (.NET 4.6), we get an endless application. This is because the JIT compiler generates code that reads finish only once, so it ignores any future updates.
Question: why does the JIT compiler allow this optimization? What part of the specification allows this?
ixSci source share