I was surprised that Parallel
doesn't consume all threads. I write this code:
public static long Sum(this IList<long> list)
{
long result = 0;
Parallel.ForEach(Partitioner.Create(0, list.Count),
() => 0L,
(range, state, sum) => Sum(list, range),
x =>
{
Interlocked.Add(ref result, x);
});
return result;
}
then I try to use this one:
long res = list.AsParallel().Sum();
but it consume single core too.
Question: how can I force to run this code in multiple threads, by setting process affinity or something else, I guess, there is some technique to do it.