开发者

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

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开发者_StackOverflow社区 work items, it could indefinitely block until there the 100th item arrives.

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


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.


You can use java.util.concurrent.SynchronousQueue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜