title: Poor performance with Concurrent Queue
body: <p>I have two threads.  One is collecting data from a serial port and placing it into an array that is then added to a concurrent queue.  The other thread, is taking these arrays and plotting the data in real time.  There are about 150 packets of data of 50 bytes each per second.
  The problem I am having is that when this runs, it is consuming about 10% CPU.  And this is on a very fast core I7 Haswell. If I add a Thread.Sleep(1) in the consuming thread then the CPU utilization drops to 1%.  The problem is if I put this at Thread.Sleep(2) then the packets are not synchronized anymore.  So this is not a solution, and if it is run on a slower computer it probably will not work.</p>
  
  <p>The code is simple.  Here is the producer thread:</p>
  
  <pre><code>    static void FillQueue(byte[] buffer)
      {
          dataQueue.Enqueue(buffer);
      }
  </code></pre>
  
  <p>and here is the consumer thread:</p>
  
  <pre><code>        while (continuePolling)
          {                
              Thread.Sleep(1); //if removed, there is 10% CPU utilization.  If higher then packet synchronization is lost.
  
              if (dataQueue.TryDequeue(out result))
              {
                  ProcessPacket(result);
              }               
          }
  </code></pre>
  
  <p>I find this hard to understand since the ProcessPacket method takes about 0.3 ms to execute and is called about 10 times a second.</p>
  
  <p>I have now tried using a blocking collection instead.
  Here is the producer </p>
  
  <pre><code> BlockingCollection&lt;byte[]&gt; dataQueue = new BlockingCollection&lt;byte[]&gt;;
  
   public static void addData(buffer)
   {
        dataQueue.add(buffer);
   }
  </code></pre>
  
  <p>and here is the consumer</p>
  
  <pre><code> while (dataQueue.TryTake(out result)
   {
       ProcessPacket(result);
   }
  </code></pre>
  
  <p>There is no difference at all! It uses 10% CPU and if I add a Thread.Sleep(1) is is fine, but a Thread.Sleep(2) and I loose packets
  I don't get it.  There are 50 bytes in each packet.  That's it.  And they are consumed as fast as produced.
  Thanks</p>