C # BlockingCollection TryTakeFromAny guarantee consistent validation of BlockingCollection objects?

I have two blocking collections - one higher priority than the other. If I use TryTakeFromAny and set the BlockingCollection to a higher priority first, is it guaranteed that the higher priority queue will be considered first?

+3


source to share


1 answer


This is not documented, so I would say there is no guarantee that it will not change in the future. Building on this for a long time is probably not recommended. However, it currently BlockingCollection<T>.TryTakeFromAny

does a quick check, iterating over all the collections by checking the index on the item (it checks to see if the count is> 0, then TryTake

). If no items are found, it gets an internal wait descriptor for each collection and passes them to WaitHandle.WaitAny

. This provides a guarantee:

This method is returned when any descriptor is signaled. If more than one object is passed during the call, the return value is the index of the signal object array with the lowest index value for all signaled objects.



So the current implementation will indeed behave on demand. If two collections receive an item at the same time, the subscript will be accepted.

+4


source







All Articles