简体   繁体   English

C#BlockingCollection的TryTakeFromAny是否保证对BlockingCollection对象进行顺序检查?

[英]Does C# BlockingCollection's TryTakeFromAny guarantee sequential checking of BlockingCollection objects?

I have two blocking collections - one of higheer priority than the other. 我有两个阻止集合 - 一个比另一个更高优先级。 If I use TryTakeFromAny and specify the higher priority BlockingCollection first, is it guaranteed that the higher priority queue will be looked into first? 如果我使用TryTakeFromAny并首先指定更高优先级的BlockingCollection,是否可以保证首先查看优先级较高的队列?

This isn't documented, so I would say there is no guarantee that it won't change in the future. 这没有记录,所以我想说不能保证它将来不会改变。 Relying on it long term is probably not recommended. 可能不建议长期依赖它。 However, currently, BlockingCollection<T>.TryTakeFromAny does a quick check by looping through all the collections by index checking for an item (it checks if Count > 0 and then does TryTake ). 但是,目前, BlockingCollection<T>.TryTakeFromAny通过索引检查项目来循环遍历所有集合(它检查Count> 0然后执行TryTake )来快速检查。 If no items are found it gets an internal wait handle for each collection and passes them to WaitHandle.WaitAny . 如果没有找到任何项,则会为每个集合获取一个内部等待句柄,并将它们传递给WaitHandle.WaitAny This provides the guarantee: 这提供了保证:

This method returns when any handle is signaled. 当发出任何句柄信号时,此方法返回。 If more than one object becomes signaled during the call, the return value is the array index of the signaled object with the smallest index value of all the signaled objects. 如果在调用期间发出多个对象的信号,则返回值是具有所有信号通知对象的索引值最小的信号通知对象的数组索引。

So the current implementation would indeed behave as requested. 所以当前的实现确实会按照要求行事。 If two collections get an item simultaneously, the lower index one would get taken. 如果两个集合同时获得一个项目,则会获得较低的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM