多线程:多于核心的线程有什么意义? [英] Multithreading: What is the point of more threads than cores?

查看:193
本文介绍了多线程:多于核心的线程有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为多核计算机的要点是它可以同时运行多个线程.在这种情况下,如果您有四核计算机,一次运行四个以上的线程有什么意义?他们不是只是在互相抽时间吗?

I thought the point of a multi-core computer is that it could run multiple threads simultaneously. In that case, if you have a quad-core machine, what's the point of having more than 4 threads running at a time? Wouldn't they just be stealing time from each other?

推荐答案

答案围绕着线程的目的,即并行性:一次运行几行独立的执行.在理想"的系统中,每个内核将有一个线程执行:没有中断.实际上并非如此.即使您有四个核心和四个工作线程,您的进程及其线程也将不断被其他进程和线程切换出.如果您正在运行任何现代OS,则每个进程至少都有一个线程,而许多进程都有更多线程.所有这些过程都同时运行.您现在可能已经在计算机上运行了数百个线程.如果没有时间从线程中偷走"线程,您将永远不会遇到这样的情况. (好吧,如果您使用的是实时广告,则可以实时运行操作系统,甚至在Windows上,都使用实时线程优先级,但这很少见.)

The answer revolves around the purpose of threads, which is parallelism: to run several separate lines of execution at once. In an 'ideal' system, you would have one thread executing per core: no interruption. In reality this isn't the case. Even if you have four cores and four working threads, your process and it threads will constantly be being switched out for other processes and threads. If you are running any modern OS, every process has at least one thread, and many have more. All these processes are running at once. You probably have several hundred threads all running on your machine right now. You won't ever get a situation where a thread runs without having time 'stolen' from it. (Well, you might if it's running real-time, if you're using a realtime OS or, even on Windows, use a real-time thread priority. But it's rare.)

以此为背景,答案是:是的,在一台真正的四核计算机上有四个以上的线程可能会给您带来彼此窃取时间"的情况,但前提是每个单独的线程都需要100个线程%CPU .如果某个线程无法100%正常工作(例如UI线程可能无法正常工作,或者某个线程正在做少量工作或正在等待其他事情),那么安排另一个线程实际上是一个很好的情况.

With that as background, the answer: Yes, more than four threads on a true four-core machine may give you a situation where they 'steal time from each other', but only if each individual thread needs 100% CPU. If a thread is not working 100% (as a UI thread might not be, or a thread doing a small amount of work or waiting on something else) then another thread being scheduled is actually a good situation.

实际上比这更复杂:

  • 如果您需要同时完成五项工作,该怎么办?一次运行它们要比运行其中四个然后再运行第五个更有意义.

  • What if you have five bits of work that all need to be done at once? It makes more sense to run them all at once, than to run four of them and then run the fifth later.

一个线程真正需要100%CPU的情况很少.例如,当它使用磁盘或网络I/O时,它可能会花费时间等待无所事事.这是非常普遍的情况.

It's rare for a thread to genuinely need 100% CPU. The moment it uses disk or network I/O, for example, it may be potentially spend time waiting doing nothing useful. This is a very common situation.

如果您有需要运行的工作,则一种常见的机制是使用线程池.与核心具有相同数量的线程似乎很有意义,但是

If you have work that needs to be run, one common mechanism is to use a threadpool. It might seem to make sense to have the same number of threads as cores, yet the .Net threadpool has up to 250 threads available per processor. I'm not certain why they do this, but my guess is to do with the size of the tasks that are given to run on the threads.

因此:窃取时间并不是一件坏事(并且也不是真正的盗窃:这是系统应该工作的方式.)根据线程将要执行的工作类型来编写多线程程序,这可能不受CPU限制.根据性能分析和测量找出所需的线程数.您可能会发现,从任务或作业而不是线程方面进行思考更有用:编写工作对象并将它们分配给要运行的池.最后,除非您的程序确实对性能至关重要,否则不要太担心:)

So: stealing time isn't a bad thing (and isn't really theft, either: it's how the system is supposed to work.) Write your multithreaded programs based on the kind of work the threads will do, which may not be CPU-bound. Figure out the number of threads you need based on profiling and measurement. You may find it more useful to think in terms of tasks or jobs, rather than threads: write objects of work and give them to a pool to be run. Finally, unless your program is truly performance-critical, don't worry too much :)

这篇关于多线程:多于核心的线程有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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