Posted By

nivlam on 04/15/12


Tagged


Versions (?)

Volatile demo


 / Published in: C#
 

Run without a debugger in debug mode and release mode. Different behavior.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. bool stop = false;
  6.  
  7. new Thread(() =>
  8. {
  9. Thread.Sleep(2000);
  10. stop = true;
  11. Console.WriteLine("Set \"stop\" to true.");
  12.  
  13. }).Start();
  14.  
  15. Console.WriteLine("Entering loop.");
  16.  
  17. while (!stop)
  18. {
  19. }
  20.  
  21. Console.WriteLine("Out of loop. Finished.");
  22. }
  23. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: shadoh on November 2, 2012

Wow, although it kind of makes sense because Stop will lose focus when it goes in to a separate function. If you make Stop static, and declare it outside the main function, both versions respond the same.

You need to login to post a comment.