简体   繁体   English

是否有必要明确限制线程数

[英]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? 运行多线程应用程序时,是否有必要明确指定用于该应用程序的最大线程数,或者Linux将自行管理该线程? If Linux handles it by itself, I would expect it to be "social", in the sense that the CPU is divided between all users. 如果Linux单独处理它,那么我希望它是“社交的”,从某种意义上说,CPU是在所有用户之间分配的。 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. 如果您的线程在计算等方面比较繁琐,那么对系统中的其他进程来说“很不错”,以便为这些进程保留一些备用的CPU容量。

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. 当然,整个系统的用途是-如果您的任务是为宝马新车的空气动力学建模,那么使用99.999%的CPU进行空气动力学计算就可以了。 If the machine is a dedicated web-server, using 99.99% of the CPU to serve web-pages is also perectly fine. 如果该机器是专用的Web服务器,则使用99.99%的CPU服务网页也很好。 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. 如果他们使用某人的台式机浏览网页,写电子邮件和编译代码,那么使用99.99%的可用CPU进行傅立叶变换音乐收藏以检查重复的歌曲,可能不是理想的解决方案。

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 . 至于当进程启动太多线程时会发生什么,这取决于调度程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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