Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel.ForEach() - How many threads?

I am downloading files which means that even though I had only 2 cores, it would improve performance to make more than two threads.

Does the Parallel.ForEach() only make as many threads as the amount of cores, even though I set ParallelOptions.MaxDegreeOfParallelism to a higher value?

like image 216
Skuli Avatar asked Nov 01 '25 00:11

Skuli


1 Answers

From documentation:

The MaxDegreeOfParallelism limits the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance to the set value, if it is positive. If MaxDegreeOfParallelism is -1, then there is no limit placed on the number of concurrently running operations.

This function throw only ArgumentOutOfRangeException and type of MaxDegreeOfParalleism is int. So it means, that it can create more threads than count of cores.

I can confirm it, because I've ran a lot of xml generation processes and Task Manager shown count of processes exactly as I've set before.

like image 166
Tomasz Dzięcielewski Avatar answered Nov 04 '25 20:11

Tomasz Dzięcielewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!