Is List <BlockingCollection <T>> Thread safe?

I will not keep a list of several BlockingCollections

List<BlockingCollection<ExtDocument>> a = new List<BlockingCollection<ExtDocument>>(); 

And check in subthreads if any of the "queues" still have pending elements:

 if (a.Where(x => x.IsAddingCompleted).Count > 0) 

Is the use of List<T> in this case thread-safe if the number of elements in the list does not change after initialization (the contents of the blocking lists inside the collection will change without changes ...)?

Or should I choose an array of BlockingCollection or the following construct:

 BlockingCollection<BlockingCollection<workitem>> a = new BlockingCollection<BlockingCollection<workitem>>(); 
+7
multithreading c #
source share
1 answer

A good advantage of using an array instead of List<T> is that you can use BlockingCollection<T>.TakeFromAny and similar methods. Most likely you are approaching your problem because of the wrong angle - your workflows could just do BlockingCollection<T>.TryTakeFromAny , and if it's a lie, you're done. Completely thread safe and works pretty well. Thus, your processing cycle will look something like this:

 while (BlockingCollection<ExtDocument>.TryTakeFromAny(collections, out workItem) >= 0) { // Do work on workItem } // We're done! 
+7
source share

All Articles