locked
When to use BlockingCollection? RRS feed

  • Question

  • I'm slightly confused on when I should be using a BlockingCollection.

    For example, I'm using Parallel.ForEach with an Array defined outside the loop:

    Parallel.ForEach(batch_numbers, lambda { |batch_number| process_batch(batch_number) })

    This is in IronRuby, but would also apply to C#.  batch_numbers is an array of strings.

    Should I be using BlockingCollection<string> instead of my normal array of strings?  If so, why?

    Tuesday, October 26, 2010 6:19 PM

Answers

  • BlockingCollection<T> is really intended for when you have a producer/consumer scenario.

    Say you have multiple threads.  One thread processes items, which other thread(s) add items to a queue in order to be processed.  In this case, the thread processing the items can just use the collection's GetConsumingEnumerable method and process the items in a simple foreach loop.  The other thread(s) can add items whenever there is a new item to process, and it just works.

     

     

    In your scenario, this is completely not required.  Since you're going to be processing independent values from a single array, as long as the "process_batch" routine is thread safe, and batches don't rely on other batches, you can just use a normal array and it will work fine.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Carl Daniel Tuesday, October 26, 2010 6:36 PM
    • Marked as answer by ejstembler Wednesday, October 27, 2010 11:11 AM
    Tuesday, October 26, 2010 6:23 PM
    Moderator

All replies

  • BlockingCollection<T> is really intended for when you have a producer/consumer scenario.

    Say you have multiple threads.  One thread processes items, which other thread(s) add items to a queue in order to be processed.  In this case, the thread processing the items can just use the collection's GetConsumingEnumerable method and process the items in a simple foreach loop.  The other thread(s) can add items whenever there is a new item to process, and it just works.

     

     

    In your scenario, this is completely not required.  Since you're going to be processing independent values from a single array, as long as the "process_batch" routine is thread safe, and batches don't rely on other batches, you can just use a normal array and it will work fine.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Carl Daniel Tuesday, October 26, 2010 6:36 PM
    • Marked as answer by ejstembler Wednesday, October 27, 2010 11:11 AM
    Tuesday, October 26, 2010 6:23 PM
    Moderator
  • Okay, I see.  So, using a normal array with Parallel.For and Parallel.ForEach should work just fine?. Strike my previous question, I just read your last paragraph...
    Tuesday, October 26, 2010 6:25 PM