title: Monitor.Wait - while or if?
body: <p>Currently, I'm learning for a multithreading exam. I read <a href="http://www.albahari.com/threading/part4.aspx" rel="nofollow">the good threading article of albahari</a>. I've got a question at the monitor usage - why is here used a loop in place of an if?</p>
  
  <pre><code>lock (_locker)
  {
    while (!_go) //why while and not if?
      Monitor.Wait (_locker);  // _lock is released
    // lock is regained
    ...
  }
  </code></pre>
  
  <p>I think, that an if would be sufficient.</p>
  
  <p>I'm afraid, that I don't understand the article completely.</p>
  
  <p>//Edit
  Example-Code:</p>
  
  <pre><code>class SimpleWaitPulse
  {
    static readonly object _locker = new object();
    static bool _go;
  
    static void Main()
    {                                // The new thread will block
      new Thread (Work).Start();     // because _go==false.
  
      Console.ReadLine();            // Wait for user to hit Enter
  
      lock (_locker)                 // Let's now wake up the thread by
      {                              // setting _go=true and pulsing.
        _go = true;
        Monitor.Pulse (_locker);
      }
    }
  
    static void Work()
    {
      lock (_locker)
        while (!_go)
          Monitor.Wait (_locker);    // Lock is released while we’re waiting
  
      Console.WriteLine ("Woken!!!");
    }
  }
  </code></pre>