简体   繁体   中英

is it necessary to explicitly limit the number of threads

When running a multithreaded application, is it necessary to explicitly specify the maximum number of threads used for the application, or will Linux administrate it by itself? If Linux handles it by itself, I would expect it to be "social", in the sense that the CPU is divided between all users. But maybe I am wrong about this?

Some systems have a maximum number of threads that the system can cope with, but most systems simply run out of memory [of some kind] before that happens.

If your threads are "not doing much", then you can probably have as many as you like (although handling several thousand threads may cause things to run slower simply because every now and again, the system will have to "walk" every thread in the system). If your threads are heavy in calculations and such, it's "nice" to other processes in the system to leave some spare CPU capacity for those processes.

So, it really isn't something that is set in stone, but depends on what you are trying to achieve. And of course, what the overall system is intended for - if your task is to model the aerodynamics of a new car for BMW, then using 99.999% of the CPU to do the aerodynamics calculation is perfectly fine. If the machine is a dedicated web-server, using 99.99% of the CPU to serve web-pages is also perectly fine. If it's someone's desktop machine that they use to browse the web, write emails and compile code on, then using 99.99% of the available CPU to Fourier transform their music collection to check for duplicate songs is probably not an ideal solution.

The program itself determines how many threads it starts, not the OS. As for what happens when a process starts too many threads, that's up to the scheduler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM