How many threads should I split a task into?

Asked

Viewed 211 times

2

I am developing an application in C#, one of my functions can be run with multiple threads. However, how many should I use? If the processor has 4 cores, there is advantage in using more than 4 threads?

  • You need to give more detail than your function does

1 answer

2

The cores/colors are not directly related to "your Threads", nor does it mean that if you use 4 threads it will run each one on a different core, who manages it is the operating system and not its application "at first" (see ThreadPool.SetMaxThreads).

The reason modern processors have more than one core is not for you make your program specifically faster, so that the operating system can manage the programs in general.

Answering (independent of the question about nuclei):

If the processor has 4 cores, there is advantage in using more than 4 threads?

Not for the processor and for the explanation above, now forgetting about nuclei and the like, 4 threads or less or more will have an advantage if you and your program have the need to isolate a task, if you have no reason to do so in the context of your program, you have no advantage whatsoever and no reason to do so. 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 entail costs for the machine in question of performance, as I said before, depends on the need and perhaps the strategy that wants to apply this.


About Threadpool.Setmaxthreads

Note that the method ThreadPool.SetMaxThreads can limit the number of Threads per core however it is necessary to check the limit minimum that can be used GetMinThreads. But it doesn’t mean that you will have absolute control over it or that you will have some advantage just by using it gratuitously, that is, it may be that it helps or it may be that it happens the other way around, it makes things worse.

But you can’t state anything without running your own tests for your specific application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.