Reputation: 1216
I read that number of threads created by Parallel.For and Parallel.Foreach depends on the number of cores in the computer,but is that number decided/calculated during the execution of the .exe or is it when the program is compiled?
For example, if I compile in the computer A that would create 4 threads, but then execute the .exe in another computer B with a different number of cores, will it create 4 threads or is number of threads going to depend on the number of cores of computer B?
Upvotes: 1
Views: 120
Reputation: 244817
There is no magic in Parallel.For()
, it's just a method call and so it compiles to IL that calls that method. This means that the number of threads it uses can't be decided at compile time.
It also does not directly depend on the number of cores: instead it depends on the number of threads the TaskScheduler
gives it. And the default TaskScheduler
gives it as many threads from the ThreadPool
as there are cores, as long as nothing else is using the ThreadPool
.
Upvotes: 1