So the topic is the questions.
I get this AsParallel method returns a wrapper ParallelQuery<TSource> that uses the same LINQ keywords, but from System.Linq.ParallelEnumerable instead of System.Linq.Enumerable
This is clear enough, but when I look for decompiled sources, I donβt understand how it works.
Let's start with the simplest extension: the Sum () method. The code:
[__DynamicallyInvokable] public static int Sum(this ParallelQuery<int> source) { if (source == null) throw new ArgumentNullException("source"); else return new IntSumAggregationOperator((IEnumerable<int>) source).Aggregate(); }
clear, release the Aggregate() method. This is the wrapper of the InternalAggregate method, which delays some exceptions. Now take a look at that.
protected override int InternalAggregate(ref Exception singularExceptionToThrow) { using (IEnumerator<int> enumerator = this.GetEnumerator(new ParallelMergeOptions?(ParallelMergeOptions.FullyBuffered), true)) { int num = 0; while (enumerator.MoveNext()) checked { num += enumerator.Current; } return num; } }
and here's the question: how does it work? I do not see any compatibility for the variable changed by many threads, we see only iterator and summation. Is this a magic enumerator? Or how does it work? GetEnumerator() returns a QueryOpeningEnumerator<TOutput> , but the code is too complicated.
Alex Zhukovskiy
source share