title: Don't understand the need for Monitor.Pulse()
body: <p>According to <a href="http://msdn.microsoft.com/en-us/library/ateab679.aspx">MSDN</a>, <code>Monitor.Wait()</code>:</p>
  
  <blockquote>
    <p>Releases the lock on an object and blocks the current thread until it
    reacquires the lock.</p>
  </blockquote>
  
  <p>However, everything I have read about Wait() and Pulse() seems to indicate that simply releasing the lock on another thread is not enough. I need to call Pulse() first to wake up the waiting thread. </p>
  
  <p>My question is why? Threads waiting for the lock on a Monitor.Enter() just get it when it's released. There is no need to "wake them up". It seems to defeat the usefulness of Wait().</p>
  
  <p>eg.</p>
  
  <pre><code>static object _lock = new Object();
  
  static void Main()
  {
      new Thread(Count).Start();
      Sleep(10);
  
      lock (_lock)
      {
           Console.WriteLine("Main thread grabbed lock");
           Monitor.Pulse(_lock) //Why is this required when we're about to release the lock anyway?
      }
  }
  
  static void Count()
  {
      lock (_lock)
      { 
          int count = 0;
  
          while(true)
          {
              Writeline("Count: " + count++);
  
              //give other threads a chance every 10th iteration
              if (count % 10 == 0)
                   Monitor.Wait(_lock);
          }
      }
  }
  </code></pre>
  
  <p>If I use Exit() and Enter() instead of Wait() I can do:</p>
  
  <pre><code>static object _lock = new Object();
  
  static void Main()
  {
      new Thread(Count).Start();
      Sleep(10);
  
      lock (_lock) Console.WriteLine("Main thread grabbed lock");
  }
  
  static void Count()
  {
      lock (_lock)
      { 
          int count = 0;
  
          while(true)
          {
              Writeline("Count: " + count++);
  
              //give other threads a chance every 10th iteration
              if (count % 10 == 0)
              {
                   Monitor.Exit(_lock);
                   Monitor.Enter(_lock);
              }
          }
      }
  }
  </code></pre>