title: Most efficient way to process a queue with threads
body: <p>I have a queue onto which pending fourier transform requests (comparatively time consuming operations) are placed - we could get thousands of transform requests per second in some cases, so its gotta be quick.</p>
  
  <p>I'm upgrading the old code to use .net 4, as well as porting to TPL. I'm wondering what the most efficient (fastest throughput) way to handle this queue looks like. I'd like to use all cores available. </p>
  
  <p>Currently I am experimenting with a BlockingCollection. I create a queue handler class that spawns 4 tasks, which block on the BlockingCollection and wait for incoming work. They then process that pending transform. Code:</p>
  
  <pre><code>public class IncomingPacketQueue : IDisposable
      {
          BlockingCollection&lt;IncomingPacket&gt; _packetQ = new BlockingCollection&lt;IncomingPacket&gt;();
  
          public IncomingPacketQueue(int workerCount)
          {
              for (int i = 0; i &lt; workerCount; i++)
              {
                  Task.Factory.StartNew(Consume);
              }
          }
  
          public void EnqueueSweep(IncomingPacket incoming)
          {
              _packetQ.Add(incoming);
          }
  
          private void Consume()
          {
              foreach (var sweep in _packetQ.GetConsumingEnumerable())
              {
                  //do stuff
                  var worker = new IfftWorker();
                  Trace.WriteLine("  Thread {0} picking up a pending ifft".With(Thread.CurrentThread.ManagedThreadId));
                  worker.DoIfft(sweep);                
  
              }
          }
  
          public int QueueCount
          {
              get
              {
                  return _packetQ.Count;
              }
          }
  
      #region IDisposable Members
  
      public void Dispose()
      {
          _packetQ.CompleteAdding();
      }
  
      #endregion
      }
  </code></pre>
  
  <p>Does this look like a good solution? It seems to max out all cores - although I'm currently unsure how many workers I should spawn in my constructor.</p>