Parallel and The C# Memory Model

Wednesday, March 6, 2013

Parallel and The C# Memory Model Parallel programming can be tricky, both compiler and CPU's optimization can lead into a twilight zone's debugging. lets take the following code snippet snippet: Code Snippet class Program {     static void Main(string args)     {         Console.WriteLine("Start");         var u = new Util();         u.Exec();         Console.ReadKey();     } }   public class Util {     private bool _stop = true;     public void Exec()     {         Task t = Task.Run(() =>             {                 bool b = true;                 while (_stop)                 {                     b = !b;                 }                 Console.WriteLine("Complete {0}", b);             });         Thread.Sleep(30);         _stop = false;     } } ...