多线程降低速度?
我正在编辑一个多线程应用程序,因此我必须学习它并有一个理论问题。
当主线程等待资源变得可用时,拥有第二个线程允许应用程序继续运行,因此如果正确完成,它应该会加快应用程序的速度。
我的想法是,在单线程应用程序中浪费的处理时间只有这么多,因此如果您有很多线程,那么每个线程都必须等待轮流处理,从而导致程序速度变慢。
我是不是搞错了?
如果不是,建议的经济线程数是多少。
I am editing a multi-threaded application so am having to learn it and have a theory question.
Having a second thread allows the application to continue functioning when the main thread would be waiting for resources to become available so it it supposed speed up the application if done properly this I get.
What I was thinking was that there is only so much wasted processing time in a single thread application so if you have a lot of threads then each one would have to wait for a turn to process resulting in a slower program all together.
Have I got this wrong ?
If not what is a recommended economical number of threads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这里:
Wiki 是你的朋友...
-----编辑-----
线程只是为了更好地利用你的CPU,所以它们不应该加速你的应用程序。
以前,在我的多线程应用程序中,我创建了自己的线程池,实际上按核心创建了 2-5 个线程池。现在我使用系统API管理的池,它最多可以达到30个。
我的建议是,在异步API之上构建要并行运行的任务,并花一些时间构建调用图。这样每个任务就可以交给任意线程。这样做,让系统给你下一个可用的线程。
另一个建议是,减少线程中调用 API 进入内核的次数。
在你的情况下,你必须找到瓶颈、磁盘、网络。
Have a look here :
Wiki is your friend ...
----- Edited -----
Threads are just there to do a better usage of you CPU, so they are not supposed to speed up you application.
Formerly, in my multhreaded applications I created my own pool of thread in fact 2-5 by core. Now I use the pool managed by the system API and it can be up to 30.
My advice, built the tasks you want to run in parallel on the top of asynchronous API, and spent some time to build the graph of calls. So that each task can be given to any thread. Doing this way, let the system give you the next available thread.
Another advice, reduce in your thread the number of time you call APIs getting into the kernel.
In your case you have to find the bottleneck, disk, network.
这不是浪费的问题。多线程并不总是正确的解决方案。
如果您有一个设计为顺序的单线程代码(每行代码都需要处理前一行代码),那么将其分离到不同的线程中是没有意义的。
仅当您可以打开线程并且不会通过连接相互冻结时(当您不必真正等待其他线程完成时),多线程才有效。
您需要考虑的另一件事是单个线程在单个处理器中运行。因此,如果您有很长的计算(例如矩阵乘法),您可以将块划分到多处理器计算机上的不同处理器中。
那么,大约有多少?这取决于你在做什么。
如果您只是执行异步方法,只希望它运行并在完成时返回一个值,那么线程数量没有限制。您可以使用逻辑上找到的正确数量(尽管线程过多会产生开销)
如果您想分解计算并将不同的块发送到不同的处理器,则没有理由打开比处理器数量更多的线程。 (尽管我发现在某些情况下如果使用大约 1.5 个处理器数量可以获得更好的结果)
It's not a matter of waste. And multiple threads isn't always the right solution.
If you have a single threaded code that is designed to be sequential (Each line of code needs the previous line of code to be processed) then there is no point in separating it into different threads.
Multithreading is only good when you can open up threads and not freeze one-another with joins (when you don't have to really wait for other threads to finish).
Another thing you need to take into account is that a single thread runs in a single processor. So, if you have a long calculation (like matrix multiplication) you can divide chunks into different processors on a multi-processor machine.
So, about how many ? It depends on what you're doing.
If you are just doing an asynchronous method, where you just want it to run and return a value when it's finished, then there's no limit for the number of threads. You can use how many you find right logically (Although there is an overhead for too many threads)
If you want to break-up a calculation and send different chunks to different processors, there's no reason to open more threads then the number of processors. (although I find that in some cases if you use about 1.5 the number of processors you can get better results)
实际上,我认为您对并行计算背后的理论更感兴趣,而不是多线程:请参阅此处。
简而言之,这在很大程度上取决于工作实际的可并行性、有多少处理器正在处理它,以及并行部分之间有多少数据依赖关系。
I actually think you are more interested in the theory behind parallel computing, not so much multi-threading: see here.
The short answer is that it depends a lot on how parallelizable the work actually is, how many processors are working on it, and how many data dependencies there are between the parallelized parts.