Question

I'm starting to work with TPL right now. I have seen a simple version of the producer/consumer model utilizing TPL in this video.

Here is the problem:

The following code:

BlockingCollection<Double> bc = new BlockingCollection<Double>(100);
IEnumerable<Double> d = bc.GetConsumingEnumerable();

returns an IEnumerable<Double> which can be iterated (and automatically consumed) using a foreach:

foreach (var item in d)
{
    // do anything with item
    // in the end of this foreach,
    // will there be any items left in d or bc? Why?
}

My questions are:

  1. if I get the IEnumerator<Double> dEnum = d.GetEnumerator() from d (to iterate over d with a while loop, for instance) would the d.MoveNext() consume the list as well? (My answer: I don't think so, because the the dEnum is not linked with d, if you know what I mean. So it would consume dEnum, but not d, nor even bc)
  2. May I loop through bc (or d) in a way other than the foreach loop, consuming the items? (the while cycles much faster than the foreach loop and I'm worried with performance issues for scientific computation problems)
  3. What does exactly consume mean in the BlockingCollection<T> type?

E.g., code:

IEnumerator<Double> dEnum = d.GetEnumerator();
while (dEnum.MoveNext())
{
    // do the same with dEnum.Current as
    // I would with item in the foreach above...
}

Thank you all in advance!

Was it helpful?

Solution

If I get the IEnumerator<Double> dEnum = d.GetEnumerator() from d (to iterate over d with a while loop, for instance) would the d.MoveNext() consume the list as well?

Absolutely. That's all that the foreach loop will do anyway.

May I loop through bc (or d) in a way other than the foreach loop, consuming the items? (the while cycles much faster than the foreach loop and I'm worried with performance issues for scientific computation problems)

If your while loop is faster, that suggests you're doing something wrong. They should be exactly the same - except the foreach loop will dispose of the iterator too, which you should do...

If you can post a short but complete program demonstrating this discrepancy, we can look at it in more detail.

An alternative is to use Take (and similar methods).

What does exactly consume mean in the BlockingCollection type?

"Remove the next item from the collection" effectively.

OTHER TIPS

There is no "performance issue" with foreach. Using the enumerator directly is not likely to give you any measurable improvement in performance compared to just using a foreach loop directly.

That being said, GetConsumingEnumerable() returns a standard IEnumerable<T>, so you can enumerate it any way you choose. Getting the IEnumerator<T> and enumerating through it directly will still work the same way.

Note that, if you don't want to use GetConsumingEnumerable(), you could just use ConcurrentQueue<T> directly. By default, BlockingCollection<T> wraps a ConcurrentQueue<T>, and really just provides a simpler API (GetConsumingEnumerable()) to make Producer/Consumer scenarios simpler to write. Using a ConcurrentQueue<T> directly would be closer to using BlockingCollection<T> without using the enumerable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top