What is the purpose of BlockingCollection(Of T)
Asked Answered
B

4

20

I´m trying to understand the purpose of BlockingCollection in the context of the new Parallel Stacks on .NET 4.

The MSDN documentation says:

BlockingCollection is used as a wrapper for an IProducerConsumerCollection instance, allowing removal attempts from the collection to block until data is available to be removed. Similarly, a BlockingCollection can be created to enforce an upper-bound on the number of data elements allowed in the IProducerConsumerCollection; addition attempts to the collection may then block until space is available to store the added items.

However when I look at the implementation of some IProducerConsumerCollection, like ConcurrentQueue I see that they provide a lock free, thread safe, implementations. So why is needed the lock mechanism that BlockingCollection provides? All the examples in the MSDN show using those collections via BlockingCollection wrapper, what are the troubles of using those collections directly? What benefit produces using BlockingCollection?

Bulrush answered 21/12, 2009 at 7:57 Comment(0)
L
18

Blocking until the operation can be performed is a convenience if you have nothing else to do anyway (or rather: cannot proceed until the operation has been performed).

If you have a non-blocking queue from which you want to read data, and there is no data at the moment, you have to periodically poll it, or wait on some semaphore, until there is data. If the queue blocks, that is already done automatically.

Similarly, if you try to add to a non-blocking queue that is full, the operation will just fail, and then you have to figure out what to do. The blocking queue will just wait until there is space.

If you have something clever to do instead of waiting (such as checking another queue for data, or raising a QueueTooFullException) then you want the non-blocking queue, but often that is not the case.

Often, there is a way to specify a timeout on blocking queues.

Luxembourg answered 21/12, 2009 at 8:20 Comment(2)
I cannot find anywhere - what is the meaning of "blocking", is it rather "ignoring" к "waiting until"?Comfy
"blocking" means "wait until the operation can be completed"Luxembourg
S
7

The purpose of locking is the locking itself. You can have several threads read from the collection, and if there is no data available the thread will just stay locked until new data arrives.

Also, with the ability to set a size limit, you can let the producer thread that is filling the collection just feed as much as it can into it. When the collection reaches the limit, the thread will just lock until the consumer threads have made space for the data.

This way you can use the collection to throttle the throughput of data, without doing any checking yourself. Your threads just read and write all they can, and the collection takes care of keeping the threads working or sleeping as needed.

Stowaway answered 21/12, 2009 at 8:21 Comment(1)
The important part is "without doing any checking yourself". Both your producer and consumer code can be really simple, almost completely the same as for your non-parallel version and still you get the benefit of threads falling asleep if there's nothing (useful) to do for them.Precondition
J
4

It's one of those things that's much easier to understand once you do it.

For producer consumer, let's have two objects, Producer and Consumer. They both share a queue they're given when constructed, so they can write between it.

Adding in a producer consumer is pretty familiar, just with the CompleteAdding a little different:

    public class Producer{
       private BlockingCollection<string> _queue;
       public Producer(BlockingCollection<string> queue){_queue = queue;}  

       //a method to do something
       public MakeStuff()
       {
           for(var i=0;i<Int.MaxValue;i++)
           {
                _queue.Add("a string!");
           }

           _queue.CompleteAdding();
       }
}

The consumer doesn't seem to make sense - until you realize that the foreach will not stop looping UNTIL the queue has completed adding. Until then, if there's no items, it will just go back to sleep. And since it's the same instance of the collection in the producer and consumer, you can have the consumer ONLY taking up cycles when there's actually things to do, and not have to worry about stopping it, restarting it, etc.

public class Consumer()
{
      private BlockingCollection<string> _queue;
      public Consumer(BlockingCollection<string> queue)
      {
           _queue = queue;
      }

      public void WriteStuffToFile()
      {
          //we'll hold until our queue is done.  If we get stuff in the queue, we'll start processing it then
          foreach(var s in _queue.GetConsumingEnumerable())
          {
             WriteToFile(s);
          }
      }
}

So you wire them together by using the collection.

var queue = new BlockingCollection<string>();
var producer = new Producer(queue);
var consumer = new Consumer(queue);

producer.MakeStuff();
consumer.WriteStuffToFile();
Jensen answered 7/11, 2013 at 22:35 Comment(1)
Forgot to add, the reason to do this is I can put the producer and consumer in separate threads, and leave the main thread to do other things now.Jensen
G
0

Alternatively, AsyncEx provides AsyncCollection, which is an async version of BlockingCollection. See https://github.com/StephenCleary/AsyncEx/wiki/AsyncCollection

Gaming answered 20/1, 2015 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.