C # Max Threads

I just got started with threads and quickly came to the most common question of how many threads are there too many?
After some research, I got even more confused.

challenge
I have 16 cores and an application creating 1000 objects, despite the fact that from the design parameters are consistent. Creating 1000 threads would usually create a huge overhead, ruining the performance benefits of multithreading according to msdn's documentation and Max Threads . Also I have to stay within the 3.5 NET 32-bit limit ( Maximum number of threads in a .NET application? ).

Question 1
With such a one-way task, is it possible to create more threads than kernels on the computer? Is .NET optimizing something out there? What is the maximum number of threads I should be using?

Question 2
Is there a simple beautiful solution and the other is a resource constrained queue? Something like a semaphore where I create all 1000 threads, but immediately inactivates the threads so as not to allocate the thread stack by default?

+3


source to share


3 answers


Yes, you can (and most likely should) create more threads than there are kernels, because the operating system's scheduler will interupt to exchange between active threads, even if nothing is waiting on that kernel. However, you also don't want to create 1000 separate threads. Instead, create multiple threads and split them between them so that each thread will process more than one item from a complete job.

I found it a good rule of thumb to go two threads per logical core (this is counting with hyperthreading cores ... if you have an 8-core hyperthreading processor for 16 logical cores, create 32 threads). The idea is that you want as few scheduler / context swap interceptors as possible, but at the same time keep all cores busy, working mostly on your task. Such scheduler hooks will still occur even if nothing is active in the logical kernel, with two active threads for that kernel meaning that the scheduler can simply put the idle thread out of your program. Even if other stuff is active for this kernel, it will now still be selected by your thread for execution. Higher than this may trigger more context switches than necessary,and degrade performance.

In the short version, the cost of the second thread per core is low (because context switches are still happening), but the payoff is potentially high (all the scheduler blocks where the processor is working on your application, not something else). As you add more threads per core, you begin to increase costs and decrease potential benefits.



But this is just my experience. It is extremely generalized, and not very good for much more than a starting point. You really need a profile of how your application behaves with different thread numbers to get an idea of ​​how to tune it for better performance.

Finally, in the .Net world ThreadPool

, asynchronous tasks are worth mentioning . This is not the best place to get into a complete textbook on these subjects, but reading them is well worth your time.

+5


source


Can more threads be created than kernels on the computer?

Yes it is possible.

Is .NET optimizing something out there? What is the maximum number of threads I should be using?



In fact, you don't have to worry about the number of threads 99.99% of the time, because you ... don't have to create threads manually 99.99% of the time. Use instead ThreadPool

if you must stay in .NET 3.5. Use tasks if your version is 4.0 or higher.

Is there a simple beautiful solution and the other is a resource constrained queue?

Not really. You can't jump over physically available resources, it's a bottleneck.

+3


source


Basically, you get less and less a percentage of the performance from new threads as you do more. 10 threads do not work 10 times faster than 1 thread. Also, when you get more threads than there are cores, you don't get anything of value.

The rule of thumb is to use threads for high performance code so that it runs in the background and not in the UI so you don't block the responsiveness of the UI. If you want to optimize code that is no longer a UI block, you can look at the number of cores on the target machine and create some more threads to help heavy algorithms, if that makes sense. Remember that it is much more difficult to debug and maintain and write multithreaded code.

Hence, only do this to experiment and learn, otherwise only when you really need to stifle some extra power in the application.

Answer 1: Yes, it is possible to create more threads than kernels, but that makes zero sense.

Answer 2: Don't create 1000 threads to create 1000 objects! what is not beautiful

+1


source







All Articles