Reputation: 4502
I have following code:
static IEnumerable<int> foo()
{
int den = 0;
yield return 10;
yield return 10;
yield return 10;
yield return 10 / den;
yield return 10 / den;
}
static public void Main()
{
foreach (var item in foo().AsParallel().Take(3))
{
Console.WriteLine(item);
}
Console.ReadLine();
}
This code fails (because actually unused elements of collection will be computed - plinq computes chunks of data). Does .Net support real "lazy" parallelization (without precomputation of chunks with unused elements)?
Note: it is just simple example. I understand, AsParallel
should be used for big data to avoid overhead.
Upvotes: 2
Views: 301
Reputation: 203802
If you prohibit the precomputation of chunks then you can't do any work in parallel. The only way to parallelize any of the streaming operators is for them to query more items of the data source than they can be sure that they need and pre-compute their values. If you cannot allow any pre-computation of data then you, by definition, can't do any of the processing for any of these operators in parallel, and simply need to use regular LINQ operations rather than PLINQ.
Upvotes: 1