I'm developing an application in C #, one of my functions can be run with multiple threads. But how many should I use? If the processor has 4 cores, is there an advantage in using more than 4 threads?
I'm developing an application in C #, one of my functions can be run with multiple threads. But how many should I use? If the processor has 4 cores, is there an advantage in using more than 4 threads?
Core / color is not directly related to "your threads", nor does it mean that if you use 4 threads it will run each in a different kernel, in> operating system and not your application "at first" (see ThreadPool.SetMaxThreads
).
The reason modern processors have more than one core is not for you to make your program specifically faster, so the operating system can manage the programs in general.
Responding (regardless of core question):
If the processor has 4 cores, is there an advantage in using more than 4 threads?
Not for the processor and for the explanation above, now forgetting about kernels, 4 threads
or less or more will have an advantage if you and your program need to isolate a task, if you have no reason to do so in the context of your program has no advantage whatsoever or why. Of course many things can be done in many ways, but this is your own strategy that will depend on what you are doing.
However, how many should I use?
It will depend on the need, creating multiple threads without need will also incur costs for the machine in question of performance, as I said before, it depends on the need and maybe the strategy you want to implement this.
Note that the method ThreadPool.SetMaxThreads
you can limit the number of threads per core, however you need to check the minimum limit that can be used GetMinThreads
. But it does not mean that you will have absolute control over this or that you will have some advantage just by using it for free , that is, it might help or it might happen to the contrary, / p>
But you can not say anything without doing your own tests for your specific application.