是否BlockingCollection< T>保证清拆令? [英] Does BlockingCollection<T> guarantee removal order?

查看:101
本文介绍了是否BlockingCollection< T>保证清拆令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几年前编写的应用程序有界阻塞队列。远的不说,执行是比恒星,但它的工作。然而,它有一些性能问题。它看起来像.NET 4.0 BlockingCollection< T> 是合适的替代品,但我需要确保,它实际上是一个队列。也就是说,它是保证是严格的FIFO,如果在单一生产商使用,单消费时尚?

I have a bounded blocking queue in an application that was written several years ago. Let's just say that the implementation is less than stellar, but it's worked. However, it has some performance problems. It looks like the .NET 4.0 BlockingCollection<T> is the right replacement, but I need to make sure that it is actually a queue. That is, is it guaranteed to be strictly FIFO if used in a single-producer, single-consumer fashion?

文档没有具体说了。 href=\"http://msdn.microsoft.com/en-us/library/dd267312.aspx\"> BlockingCollection 话题确实说了(在备注)

The documentation doesn't specifically say. The BlockingCollection topic does say (in the Remarks):

BlockingCollection< T> 类似于
传统阻塞队列数据
结构,除了底层
的数据存储机制被抽象
远为 IProducerConsumerCollection< T>

但没有具体说,事情会在他们加入相同的顺序被删除。

But nothing specifically says that things will be removed in the same order they're added.

有人知道肯定?

推荐答案

好了, BlockingCollection< T> 真的是专为并行工作,在那里你有多个simulataneous生产者,一个消费者(使用GetConsumingEnumerable())。

Well, BlockingCollection<T> is really designed for parallel work, where you have multiple simulataneous "producers" and one consumer (using GetConsumingEnumerable()).

在这种情况下,你没有办法保证插入顺序,所以排序约束AREN 。指定的T

In this situation, you have no way to guarantee insertion order, so the ordering constraints aren't specified.

话虽这么说, BlockingCollection< T> 工作于任何 IProducerConsumerCollection< T> (在构造函数中指定)。如果你没有在构造函数中提供的,在内部,它会使用 ConcurrentQueue< T> 。这使得它成为FIFO,因为它实际上将(内部)的队列。所以,是的,在默认情况下,这将是保证严格的FIFO,如果在一个单一的生产者,单消费者的方式使用,在当前实现最少。如果要强制此为未来的打样(因为队列是一个实现细节),只需构造它为:

That being said, BlockingCollection<T> works upon any IProducerConsumerCollection<T> (specified in the constructor). If you don't provide one in the constructor, internally, it will use a ConcurrentQueue<T>. This causes it to be FIFO, since it will actually be (internally) a queue. So yes, by default, it will be "guaranteed to be strictly FIFO if used in a single-producer, single-consumer fashion", at least in the current implementation. If you want to force this for future proofing (since the queue is an implementation detail), just construct it as:

var blockingCollection = new BlockingCollection<MyClass>(new ConcurrentQueue<MyClass>());



这将保证它现在使用队列,并且在未来的(因为队列是一个执行细节)。

That will guarantee that it uses a queue now, and in the future (since the queue is an implementation detail).

这篇关于是否BlockingCollection&LT; T&GT;保证清拆令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆