multithreading 什么是 CPU 线程,它与代码中的逻辑线程有什么关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/916048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a CPU thread and how is it related to logical threads in code?
提问by MikeJ
I have been seeing in the literature for some of the newer CPU's such as the Intel Xeon "Nehalem-EX"as having 8 cores and 16 threads. What are they talking about here? I saw mention of this in reference so SPARCS too, surely this isn't the kind of logical threads spawned by code ? Is this hyperthreading re-named?
我一直在文献中看到一些较新的 CPU,例如英特尔至强“Nehalem-EX”具有 8 个内核和 16 个线程。他们在这里谈论什么?我在参考资料中也看到了这一点,所以 SPARCS 也是如此,这肯定不是代码产生的那种逻辑线程吗?这个超线程重命名了吗?
回答by coobird
Yes, Nehalem-basedprocessors implement Hyper-threading.
是的,基于 Nehalem 的处理器实现了超线程。
The new Nehalem-EX which you refer to has 8 physical cores where each core can be seen as 2 logical cores for a total of 16 logical cores, allowing for the execution of 16 application threads on a single processor.
您所指的新 Nehalem-EX 具有 8 个物理内核,其中每个内核可以视为 2 个逻辑内核,总共 16 个逻辑内核,允许在单个处理器上执行 16 个应用程序线程。
This is the same technology used in the Hyper-threading-enabled Pentium 4 processors, and more recently, on the Atom processors. My Eee PC has a single-core Atom processor which has two logical cores -- the Windows Task Manager will show two CPU graphs; one for each logical core.
这与支持超线程的 Pentium 4 处理器以及最近在 Atom 处理器上使用的技术相同。我的 Eee PC 有一个单核 Atom 处理器,它有两个逻辑核心——Windows 任务管理器将显示两个 CPU 图表;每个逻辑核心一个。
Sun's UltraSPARC T2(and the T1) also allow for simultaneous multithreading(of which Intel's implementation is called Hyper-Threading -- an trademark of Intel) which allows a single core to appear as multiple logical cores to execute multiple threads on a single core.
Sun 的UltraSPARC T2(和 T1)还允许同时进行多线程(英特尔的实现称为超线程——英特尔的商标),它允许单个内核显示为多个逻辑内核以在单个内核上执行多个线程。
The rough idea behind simultaneous multithreading is to have multiple registers to store the processor state, so it appears that there actually are multiple cores in a single core, because it has multiple full-sets of hardware registers.
并发多线程背后的粗略想法是有多个寄存器来存储处理器状态,因此看起来实际上在单个内核中有多个内核,因为它具有多个完整的硬件寄存器集。
While the physical facilities such as the ALU and FPU may not increase, having more sets of registers to run more threads on a physical core can lead to better utilization of the available processor resources. The core may have not been saturated when executing a single thread, but executing multiple could saturate all the units to its fullest potential.
虽然 ALU 和 FPU 等物理设施可能不会增加,但拥有更多组寄存器以在物理内核上运行更多线程可以更好地利用可用处理器资源。执行单个线程时,内核可能尚未饱和,但执行多个线程可以使所有单元达到最大潜力。
So what does it mean for programmers?
那么对于程序员来说意味着什么呢?
It means that we still will need to write multi-threaded software -- having a program that only has a single thread will only be able to utilize a single logical core. Only by having well-written multi-threaded code are we able to take advantage of the massive number of logical cores these processors offer.
这意味着我们仍然需要编写多线程软件——只有一个线程的程序将只能使用一个逻辑核心。只有拥有精心编写的多线程代码,我们才能利用这些处理器提供的大量逻辑内核。
Even with simultaneous multithreading, the code is executed at one thread per logical core.
即使使用同步多线程,代码也是在每个逻辑核心的一个线程上执行的。
回答by sybreon
An extreme of a multi-threaded processor is the barrel processor. This is a form of SMT where the processor divides up slots between the multiple threads equally in a round robin manner. To do this, it only needs copies of the various registers while using the same set of execution units. So, in 4 clock cycles it would put code from Threads 0-3 in the pipeline.
多线程处理器的一个极端是桶形处理器。这是 SMT 的一种形式,其中处理器以循环方式在多个线程之间平均分配插槽。为此,它只需要在使用同一组执行单元时复制各种寄存器。因此,它会在 4 个时钟周期内将线程 0-3 中的代码放入管道中。
You can think of the rest of these processors working in a similar fashion, to a more or lesser degree. Instead of distributing slots around equally, it may just use up slots that are empty due to either control or data hazards in the processor.
您可以认为其他处理器或多或少以类似的方式工作。它可能不会平均分配插槽,而可能只是用完由于处理器中的控制或数据危险而空的插槽。
For example, when a branch is taken, instructions in the pipeline may need to be flushed. Instead of completely flushing everything, some of the slots can be used for other threads. The whole idea is to improve performance by not wasting CPU cycles.
例如,当发生分支时,可能需要刷新流水线中的指令。一些插槽可以用于其他线程,而不是完全刷新所有内容。整个想法是通过不浪费 CPU 周期来提高性能。
That's how multiple threads work in hardware.
这就是多线程在硬件中的工作方式。
回答by klew
It isn't hyper-threading renamed - it is hyper-threading (it is written on this webpage you gave link to).
它不是重命名的超线程 - 它是超线程(它写在您提供链接的这个网页上)。
Simply, processor tells OS that it has 16 cores, so it can balance tasks on doubled number of cores. Hyper-threading technology give some benefit becouse in some cases two different instructions from two different programs/threads can be executed on one core simultaneously. But for sure it will not give 200% speed up. I didn't work on such processor, but I think you can get about 10%-20% additional cpu time.
简单地说,处理器告诉操作系统它有 16 个内核,因此它可以在双倍内核数上平衡任务。超线程技术提供了一些好处,因为在某些情况下,可以在一个内核上同时执行来自两个不同程序/线程的两条不同指令。但可以肯定的是,它不会提供 200% 的加速。我没有在这样的处理器上工作,但我认为你可以获得大约 10%-20% 的额外 CPU 时间。
回答by AndreiM
It is up to each operating system's threading model to map OS-level threads to hardware-level threads such as those described in the question.
将操作系统级线程映射到硬件级线程(例如问题中描述的线程)取决于每个操作系统的线程模型。
The logical threads spawned by high-level programming languages used by applications programmers are still an OS-level removed from the hardware, unless of course you're talking about the OS code that does the mapping.
由应用程序程序员使用的高级编程语言产生的逻辑线程仍然是从硬件中移除的操作系统级别,除非您谈论的是执行映射的操作系统代码。
回答by Chris McCauley
Hyperthreading (INTEL's trademark by the way) allows each thread to actually run simultaneously. So in this case you could run 8X2 application threads at the same time.
超线程(顺便提一下 INTEL 的商标)允许每个线程实际上同时运行。因此,在这种情况下,您可以同时运行 8X2 应用程序线程。
From the brochure ...
从小册子...
Intel Nehalem Architecture built on Intel's unique 45nm high-k metal gate technology process
英特尔 Nehalem 架构建立在英特尔独特的 45 纳米高 k 金属栅极技术工艺之上
Up to **8 cores** per processor
Up to **16 threads per processor** with Intel? Hyper-threading
2.3 billion transistors
Compare this to single-CPU, single core systems where each thread must be scheduled and at most only one thread will be active - that one running CPU bound task and the others waiting on an I/O transfer.
将其与单 CPU、单核系统进行比较,其中每个线程必须被调度并且最多只有一个线程处于活动状态——一个运行 CPU 绑定任务,其他等待 I/O 传输。
Originally threading was used either to model a set of concurrent activities (not model not actually run in parallel) or to produce the appearance of a system which was responsive even while doing I/O. For example without threading, your word-processor would appear to stall while saving a doc.
最初线程用于对一组并发活动进行建模(而不是建模实际上不是并行运行的),或者生成即使在执行 I/O 时也能响应的系统外观。例如,如果没有线程处理,您的文字处理器在保存文档时会出现停顿。
For many years I resisted the idea of having multiple threads in my desktop applications - it complicated the code and potentially reduced performance - think of all those mutex operations which require the OS kernel to get involved. With the advent of actually parallel execution of threads, my objections are reduced but I still believe that multiple processes rather than multiple threads in a single process is a better approach.
多年来,我一直拒绝在我的桌面应用程序中使用多个线程的想法——它使代码复杂化并可能降低性能——想想所有需要操作系统内核参与的互斥操作。随着线程实际并行执行的出现,我的反对意见减少了,但我仍然相信多个进程而不是单个进程中的多个线程是更好的方法。
Chris
克里斯