cpu如何处理多个线程? [英] How does the cpu handle multiple threads?

查看:212
本文介绍了cpu如何处理多个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在制作一个使用snmp来发现子网上的设备的程序.我的程序在子网上找到所有IP地址,并向每个IP发送一个snmp get请求.我正在使用多个线程,我想知道:

如果我不限制线程数,并且线程数超出了cpu所能处理的范围,它会等待启动一个线程直到一个线程关闭还是完全不启动?

Ia m making a program that uses snmp to discover devices on a subnet. My program finds all ip addresses on the subnet and sends an snmp get request to each ip. I am using multiple threads and I am wondering:

If I don''t limit the number of threads and there are more threads going than the cpu can handle how does the cpu deal with that, Does it wait to start a thread until one closes or do they just not get started at all?

推荐答案

只要不耗尽线程空间,您就可以拥有尽可能多的线程.
但这不会使您的应用程序运行得更快,因为CPU将花费比实际执行更多的时间在不同线程之间切换上下文.

无论您使用哪种语言,某些库中都可能有一个线程池,可以帮助您为当前平台创建正确数量的线程.

/Fredrik
As long as you don''t run out of thread space you can have as many threads as you like.
But it won''t make your application run faster because the CPU will spend more time switching context between the different threads than actually executing.

Regardless of which language you''re using there''s probably a thread pool available in some library that can help you with creating the right number of threads for the current platform.

/Fredrik


这两个都是不错的答复,但他们没有回答问题.问题是:

如果我不限制线程数,并且线程数超出了cpu所能处理的范围,它会等待启动一个线程直到一个线程关闭还是完全不启动?

在我的应用程序中,我尝试了255个线程,并在大约2秒钟内完成. 100个线程约5秒,其中10个线程约1分钟.因此,更多线程不会加速程序的想法是虚假的.

我不是在寻找有关如何使用线程的建议,我想知道cpu在无法处理线程时如何处理线程.
Both of those are good replys but they don''t answer the question. The question was:

If I don''t limit the number of threads and there are more threads going than the cpu can handle how does the cpu deal with that, Does it wait to start a thread until one closes or do they just not get started at all?

in my application i tried it with 255 threads and it completes in about 2 seconds. with 100 threads about 5 seconds with 10 threads about 1 minute. So the idea that more threads doesn''t speed up the program is bogus.

I''m not looking for advice on how to use threads i''m wondering how the cpu handles threads when it can''t handle them.


即使您正在使用一个线程池(比创建多个线程更好),具有无限数量的线程通常是一个常见的谬误.在某些情况下,您需要无限数量的线程(一个典型的示例:演示线程功能),但是这种情况相对较少.

最好的模式是使用固定数量的线程.通常这是完全有可能的. (我不说恒定数量的线程";可以在配置文件中定义线程的数量;创建所有文件后,它们的数量不再更改:例如:您有N个设备,需要N * 2 + 1线程.)
如果您说是否等待启动一个线程直到一个线程关闭",这已经表明实际上您不需要这两个线程.您的任务看起来也确实不需要无限数量的线程.

我最近的回答将为您提供一个更好的主意:
我正在尝试计算每次新数据输入时是否需要重新启动"线程 [
Even if you''re using a thread pool (which is better than creation of numerous threads), having unlimited number of threads is usually a common fallacy. There are cases when you want unlimited number of threads (a typical example: demonstration of threading capabilities), but such cases are relatively rare.

Best schema is using fixed number of threads; and usually this is quite possible. (I don''t say "constant number of threads"; number of threads can be defined in configuration file; after all file are created, their number is not changed anymore: example: you have N devices which require N * 2 + 1 threads.)
If you say "does it wait to start a thread until one closes", it is already the indication that in fact you don''t need those two thread. You task also looks like you don''t really need unlimited number of threads.

My recent answer will give you a better idea:
I''m trying to figure if I need to "restart" my thread each time new data comes in[^].

—SA


这篇关于cpu如何处理多个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆