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:
- if I get the
IEnumerator<Double> dEnum = d.GetEnumerator()fromd(to iterate overdwith awhileloop, for instance) would thed.MoveNext()consume the list as well? (My answer: I don't think so, because the thedEnumis not linked withd, if you know what I mean. So it would consumedEnum, but notd, nor evenbc) - May I loop through
bc(ord) in a way other than theforeachloop, consuming the items? (thewhilecycles much faster than theforeachloop and I'm worried with performance issues for scientific computation problems) - 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!
foreachwould have performance issues?while, forforand forforeachto finish the task... In the, thewhilewas considerably faster...