.NET Quiz

QUESTION: Why does not this program quit when compiled in Release mode?

The following works as expected when building in Debug – the execution is done after three seconds. But, for some reason, it fails when in Release.

class Program
    static void Main(string[] args)
        var w = new Worker();
        while (!w.IsDone);
        Console.WriteLine("The work is done.");

class Worker
    public bool IsDone;

    public Worker()
        var thread = new Thread(Job);

    private void Job()
        IsDone = true;

Could you explain what is happening? How could you fix it?

Tomorrow, I will share the answer.

More posts in .NET Quiz series

  1. Nícolas Tarzia

    When you build your code in Release mode, some optimizations will be applied, in an multithreading environment it’s a problem, because the JIT optimizer cache the value of your var “IsDone”.

    To solve this problem just add volatile keyword will solve
    public volatile bool IsDone;

    Ref.: https://stackoverflow.com/questions/41632387/this-code-hangs-in-release-mode-but-works-fine-in-debug-mode

  2. Angelo Belchior

    Por otimização o JIT faz cache da variável IsDone em Release.
    Dessa maneira, deve-se usar o volatile:

    public volatile bool IsDone;



  3. Matheus Fantinel

    This was a nice lesson. I’m still quite new to multithreading, so I had to learn what was going on. Ended up diving in C# documentation to know more. Below is the answer, so if you haven’t done it, SPOILER ALERT!

    This happens because the compiler assumes your application is single-threaded, and optimizes it accordingly. Therefore, when Worker is created, it only sees the need to check for the variable’s value one single time, since it isn’t being changed on that thread. Simply adding the keyword “volatile” on the variable’s declaration will tell the compiler to keep watching for changes.

Leave a Reply

Your email address will not be published. Required fields are marked *