One major difference between the debug and release modes of the DOTNET project is whether the compiler optimization is enabled.
Due to the use of editor Optimization in release, some of the original operations are normal.CodeThere will be problems.
The following code is as follows:
Internal Static Class Strangebehavior
{
// As you'll see later, mark this field as volatile to fix the problem
Private Static Boolean s_stopworker = False ;
Public Static Void Main ()
{
Console. writeline ( " Main: lew.worker run for 5 seconds " );
Thread t = New Thread (worker );
T. Start ();
Thread. Sleep ( 5000 );
S_stopworker = True ;
Console. writeline ( " Main: waiting for worker to stop " );
T. Join ();
}
Private Static Void Worker (Object O)
{
Int32 x = 0 ;
While ( ! S_stopworker) x ++ ;
Console. writeline ( " WORKER: stopped when x = {0} " , X );
}
}
Compile and run in debug mode and you will findProgramIt's normal.
Compile and run the program in release mode and you will find that the program is in an endless loop.
This is because the compiler considers s_stopworker as a constant in optimization mode (false if not true)
When s_stopworker is set to false, the compiler considers that while (true) always enters an endless loop.
But when s_stopworker is true, the editor will ignore the while (! S_stopworker )...