title: c#: what is a thread polling?
body: <p>What does it mean when one says no polling is allowed when implimenting your thread solution since it's wasteful, it has latency and it's non-deterministic. Threads should not use polling to signal each other.</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>Based on your answers so far, I believe my threading implementation (taken from: <a href="http://www.albahari.com/threading/part2.aspx#_AutoResetEvent" rel="nofollow">http://www.albahari.com/threading/part2.aspx#_AutoResetEvent</a>) below is not using polling. Please correct me if I am wrong.</p>
  
  <pre><code>using System;
  using System.Threading;
  using System.Collections.Generic;
  
  class ProducerConsumerQueue : IDisposable {
    EventWaitHandle _wh = new AutoResetEvent (false);
    Thread _worker;
    readonly object _locker = new object();
    Queue&lt;string&gt; _tasks = new Queue&lt;string&gt;();
  
    public ProducerConsumerQueue() (
      _worker = new Thread (Work);
      _worker.Start();
    }
  
    public void EnqueueTask (string task) (
      lock (_locker) _tasks.Enqueue (task);
      _wh.Set();
    }
  
    public void Dispose() (
      EnqueueTask (null);     // Signal the consumer to exit.
      _worker.Join();         // Wait for the consumer's thread to finish.
      _wh.Close();            // Release any OS resources.
    }
  
    void Work() (
      while (true)
      {
        string task = null;
        lock (_locker)
          if (_tasks.Count &gt; 0)
          {
            task = _tasks.Dequeue();
            if (task == null) return;
          }
        if (task != null)
        {
          Console.WriteLine ("Performing task: " + task);
          Thread.Sleep (1000);  // simulate work...
        }
        else
          _wh.WaitOne();         // No more tasks - wait for a signal
      }
    }
  }
  </code></pre>