Server/Study

[Server] 컴파일러 최적화 (Release)

GiveZero 2023. 4. 25. 18:46

주의

using System;

namespace Server
{
    class Program
    {
        static bool _stop = false;

        static void ThreadMain()
        {
            Console.WriteLine("Thread Start");

            while (!_stop)
            {
                //Wait
            }
            
            //=>
            // if (!_stop)
            // {
            //     while (true)
            //     {
            //         
            //     }
            // }
            
            Console.WriteLine("Thread Finish");
        }
        
        static void Main(string[] args)
        {
            Task t = new Task(ThreadMain);
            t.Start();
            
            Thread.Sleep(1000);
            
            _stop = true;
            Console.WriteLine("Call Stop");
            Console.WriteLine("Waiting Finish");
            
            t.Wait(); // Release Build -> 종료 X
            
            Console.WriteLine("Success Finish");
        }
    }
}

Release 시 t.Wait에서 종료되지않음.

 

그 이유는 ThreadMain에서

 

while(!stop)

{

}

->

if(!stop)

{

  while(true){}

}  

 

로 최적화하기때문