Parallel Library MaxDegreeOfParallelism value?

2019-09-17 17:12发布

问题:

I am new to threading and parallel library. I am trying to understand the MaxDegreeOfParallelism and to what value it should be set.

Done some reading and to me might be a bit misleading.

If I want to run on 4 threads I thought I could do

var parOptions=new ParallelOptions();
parOptions.MaxDegreeOfParallelism=4;

However doing some more reading it looks like that in my case 4 does not mean runs on 4 threads but is more todo with Cores and how much it uses.

To keep it simple what is the correct way to set the number of threads it should run? Or maybe you can set on many threads it can use but more on how much cores it uses

I am planning in having a settings in config.eg "ThreadCount" with a value that the user can specify. This is an internal application that other developers will run to import stuff.

any clarification on this? thanks

回答1:

It seems like you don't understand how scheduling threads works, you might want to read up about it. In short, a program can create threads, but it has very little control about when and what core will a thread run on, that's the job of the operating system.

So, when your program creates 4 threads, it's possible (and usually quite likely) that the OS will decide to run those 4 threads on its 4 cores. But if there are other processes already using the CPU, the OS might for example decide to run those 4 threads on a single core.

Now, if you set MaxDegreeOfParallelism to 4, it means that the Parallel loop that uses these ParallelOptions may use up to 4 threads to run the loop, but not more (though it is allowed to use less). If it decides to create 4 threads, those will be run on up to 4 cores, which is decided by the OS.

But when your code is CPU-bound (i.e. you're not reading files or using the network or something like that), then it's usually best if you don't set MaxDegreeOfParallelism, the TPL will try to find the optimal amount of threads to use.



回答2:

The short answer - pasted from MSDN - Ordinarily, you do not need to modify this setting.

For more information - checkout the MSDN page about this value http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(v=vs.110).aspx

This value should depends on number of cores in the machine, type of jobs you're doing (cpu intensive vs. i/o intensive, length of josbs, etc ...). So just let the Parallel Library to decide on how many threads to run it