TPL parallelism degree heuristic

Parallel.ForEach is working on ThreadPool and by default TPL sets the number of threads to achieve maximum performance based on some internal rules. But does .Net account for parallel or nested calls to Parallel.Foreach? For example, let's say .Net decided that for the current environment, 10 threads are the best choice and we have:

Parallel.ForEach(collection1, (o1) => {Parallel.ForEach(collection2, (o2) => {...}})

      

Will it create 10 * 10 threads ?


article . Now I have found that it seems to me that the "internal rules" of thread scheduling are so advanced and dynamic that they can rationally describe the cases described.

+3


source to share


1 answer


It doesn't create threads, but tasks. These two loops will cooperate indirectly . This collaboration is not perfect and may result in more tasks in the queue than necessary / optimal. Each cycle stores one replica in the scheduler queue. This allows the scheduler to run more tasks than is optimal.

In any case, this does not mean that 100 threads are competing for OS resources. The stream path works for oversubscription. However, it tends to create more threads than there are processors to be able to deal with blocking.

Try to avoid nested loops. It is generally best to have only one parallel loop at a time. You could, for example, do this:



var items =
 from o1 in collection1
 from o2 in collection2
 select new { o1, o2 };

Parallel.ForEach(items, ...);

      

If your architecture requires nested loops, you can have them.

+3


source







All Articles