views:

54

answers:

2

For single-producer, single-consumer should I use a BlockingCollection or a ConcurrentQueue?

Concerns:

  • My goal is to pull up to 100 items at a time and send them as a batch to the next step.
  • If I use a ConcurrentQueue, I have to manually cause it to go asleep when there is no work to be done. Otherwise I waste CPU cycles on spinning.
  • If I use a BlockingQueue and I only have 99 work items, it could indefinitely block until there the 100th item arrives.

http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx

A: 

You can use java.util.concurrent.SynchronousQueue

Loop
Seems like he is not using Java.
PartlyCloudy
...but he should :D
Loop
A: 

You could probably get away with using either. Though like you said if using the ConcurrentQueue then you would have to perform a spin wait while the queue was empty. That might not be so bad if you expect the lag between the producer and consumer to be short. In the end, I think you will find the BlockingCollection a lot more pliable for your scenario. It is the data structure of choice for the producer-consumer pattern afterall. There are a couple of different ways you could work around the indefinite wait problem.

  • Use a sentinel value as a stopping indicator in the consumer. A null value inserted into the queue would work well here.
  • Take advantage of the CancellationToken mechanism to unblock the Take method.
Brian Gideon