C++ 多线程和多核的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11835046/
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
Multithreading and multicore differences
提问by DavidColson
I have a couple of small questions.
我有几个小问题。
Firstly is there a difference between multithreading and multicore? Are they two completely different things or does multithreading use more than one core if it needs?
首先,多线程和多核之间有区别吗?它们是两种完全不同的东西,还是多线程在需要时使用多个内核?
Secondly Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460. What dictates how many threads your computer has?
其次,大多数内核有两个线程,但是在分析我的应用程序时,我注意到有很多不同的线程,从线程 128 到线程 3460。是什么决定了您的计算机有多少线程?
Thanks
谢谢
回答by Wug
Firstly is there a difference between multithreading and multicore?
首先,多线程和多核之间有区别吗?
Yes.
是的。
Multithreading and Multicore are different pieces of terminology that apply to different areas of computing.
多线程和多核是适用于不同计算领域的不同术语。
Multicore refers to a computer or processor that has more than one logical CPU core, and that can physically execute multiple instructions at the same time. A computer's "core count" is the total number of cores the computer has: computers may have multiple processors, each of which might have multiple cores; the core count is the total number of cores on all of the processors.
Multithreading refers to a program that can take advantage of a multicore computer by running on more than one core at the same time. In general, twice as many cores equals twice as much computing power (for programs that support multithreading) though some problems are limited by factors other than CPU usage; these problems will not experience gains that are as dramatic from multithreading.
It's important to note that performance is not the only reason programs use many threads. More on that later.
多核是指具有多个逻辑 CPU 内核的计算机或处理器,并且可以在物理上同时执行多条指令。计算机的“内核数”是计算机拥有的内核总数:计算机可能有多个处理器,每个处理器可能有多个内核;核心数是所有处理器上的核心总数。
多线程是指可以通过同时在多个核上运行来利用多核计算机的程序。一般来说,两倍的内核等于两倍的计算能力(对于支持多线程的程序),尽管有些问题受到 CPU 使用率以外的因素的限制;这些问题不会从多线程中获得如此显着的收益。
需要注意的是,性能并不是程序使用多个线程的唯一原因。稍后再谈。
Are they two completely different things or does multithreading use more than one core if it needs?
它们是两种完全不同的东西,还是多线程在需要时使用多个内核?
They are related but seperate.
它们是相关的,但又是独立的。
Programs that support multithreading can use more than one core if more than one is available.
如果有多个内核可用,则支持多线程的程序可以使用多个内核。
Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460.
大多数内核都有两个线程,但是在分析我的应用程序时,我注意到有很多不同的线程,从线程 128 到线程 3460。
The operating system assigns threads numbers so it can keep track of them.
操作系统分配线程编号,以便它可以跟踪它们。
Most programs you will ever run will not need 3400 threads running at once. Also, a running thread will consume all of a core. The only reason your CPU isnt running at 100% all the time is that the operating system knows how to suspend the processor, which basically makes it stop everything and wait until something happens (such as an IO event or a clock tick). Only one thread can run on a core at once. Different threads running is actually just threads jumping onto the CPU and running for short periods of time, then being switched out with other threads which also need to run.
您将运行的大多数程序不需要同时运行 3400 个线程。此外,正在运行的线程将消耗所有内核。你的 CPU 没有一直以 100% 运行的唯一原因是操作系统知道如何挂起处理器,这基本上使它停止一切并等待发生某些事情(例如 IO 事件或时钟滴答)。一次只能在一个核心上运行一个线程。运行的不同线程实际上只是跳到 CPU 上并运行一小段时间的线程,然后被其他也需要运行的线程切换出去。
What dictates how many threads your computer has?
什么决定了您的计算机有多少线程?
The total number of threads in all of your processes. Also, most operating systems impose a hard limit, a maximum number of existing threads that cannot be surpassed.
所有进程中的线程总数。此外,大多数操作系统都施加了硬限制,即不能超过的最大现有线程数。
A process is a program (you probably know this). Multithreading is the process of having more than one thread per process (many processes will not make more than one thread because they do not have to). Windows does not have a hard limit on the number of threads you can create (at least, not since XP. Not going to say anything about w98 and previous), but of course the number of threads you can make is limited by how much memory you have.
进程就是一个程序(您可能知道这一点)。多线程是每个进程有多个线程的过程(许多进程不会创建多个线程,因为它们没有必要)。Windows 对您可以创建的线程数没有硬性限制(至少,从 XP 开始没有。不会说 w98 和以前的任何内容),但是当然您可以创建的线程数受内存大小的限制你有。
You said some programs use multiple threads for reasons other than performance.
您说某些程序出于性能以外的原因使用多线程。
Sometimes it's nice to be able to multitask, even if not concurrently.
有时,能够同时处理多项任务也不错,即使不是同时进行。
Sometimes programs need to do specific things at specific times. A commonly cited example is a program with a visible window. That program might be doing intense background number crunching, but it would benefit it if it could still respond to user events, like clicking buttons and resizing it. This can be accomplished with asynchronous processing, which will require your one thread to repeatedly check for GUI work to do at intervals, pause what it's doing, and handle the GUI for a while. Lots of things do it this way.
有时程序需要在特定时间做特定的事情。一个经常被引用的例子是一个带有可见窗口的程序。该程序可能正在执行大量的后台数字运算,但如果它仍然可以响应用户事件(例如单击按钮并调整其大小),则会对其有益。这可以通过异步处理来完成,这将要求您的一个线程每隔一段时间反复检查 GUI 工作,暂停正在执行的工作,并处理 GUI 一段时间。很多事情都是这样做的。
Another, possibly better way to handle it is with a thread. Your program doesn't have to worry about switching back and forth between number crunching and GUI management, the operating system will manage that for you. Even if you have only one core, you can still run multiple threads, and your OS will do its best to make sure that all of the running threads in all of the running processes get their fair share of CPU time.
另一种可能更好的处理方法是使用线程。您的程序不必担心在数字运算和 GUI 管理之间来回切换,操作系统会为您管理。即使您只有一个内核,您仍然可以运行多个线程,并且您的操作系统将尽最大努力确保所有正在运行的进程中的所有正在运行的线程都获得公平的 CPU 时间份额。
回答by juanchopanza
The number of cores and number of threads are decoupled. You can have many threads running on a single core, and you can have situations where only one thread runs despite the presence of more cores (although I cannot think of a real life scenario where this would happen). Let's say multicore is a hardware characteristic, whereas the number of threads is something in the domain of the OS and the processes running on it.
核心数和线程数是解耦的。您可以在单个内核上运行多个线程,并且您可能会遇到尽管存在更多内核但只有一个线程运行的情况(尽管我无法想象会发生这种情况的真实场景)。假设多核是一种硬件特性,而线程数是操作系统及其上运行的进程范围内的东西。
Of course, with a single core you cannot have more than one thread running concurrently. The OS has to constantly switch back and forth between threads.
当然,对于单核,您不能同时运行多个线程。操作系统必须不断地在线程之间来回切换。
回答by Mahdi
Most cores have two threads
大多数内核有两个线程
Here, I think you are confusing the overloaded term "thread". As correctly pointed out by other answers, a thread usually refers to a "software" concept. But sometimes it is also used as a "hardware" concept. When a "core" has two "threads" (like in many new Intel chips), it means that the core can run two parallel threads, as if there were two cores. This is, however, usually called hyperthreading. See:
在这里,我认为您混淆了重载的术语“线程”。正如其他答案正确指出的那样,线程通常指的是“软件”概念。但有时它也被用作“硬件”概念。当一个“内核”有两个“线程”时(就像在许多新的英特尔芯片中一样),这意味着内核可以运行两个并行线程,就好像有两个内核一样。然而,这通常称为超线程。看:
http://en.wikipedia.org/wiki/Hyper-threading
http://en.wikipedia.org/wiki/超线程
So if you have N threads (I mean software threads, those made within your application, or just by running different applications at the same time) and M processors (being cores, or the hardware threads explained above), the following happens:
所以如果你有 N 个线程(我的意思是软件线程,那些在你的应用程序中创建的线程,或者只是通过同时运行不同的应用程序)和 M 个处理器(作为核心,或者上面解释的硬件线程),会发生以下情况:
- N<=M: then the Operating System should assign each thread a different processor. Then the application threads run truly in parallel.
- N>M: then some threads have to share a processor.
- N<=M:那么操作系统应该为每个线程分配一个不同的处理器。然后应用程序线程真正并行运行。
- N>M:那么一些线程必须共享一个处理器。
回答by Science_Fiction
Threading on a single core usually means you can create x number of threads and they will each be given set amount of time to run (thread quantum). When threads are switched this is known as context switching, all this takes some time so one needs to do some benchmarking to find an ideal number of threads to have per core.
在单个内核上进行线程处理通常意味着您可以创建 x 个线程,并且每个线程都将获得一定的运行时间(线程量子)。当线程被切换时,这被称为上下文切换,所有这一切都需要一些时间,因此需要做一些基准测试来找到每个核心拥有的理想线程数。
If the majority of work is cpu bound there is little point spawning hundreds of threads since this is unlikely to improve performance (in-fact make it worse maybe, remember context switching does not come for free). However doing this for I/O bound work it may help since while the system is busy doing this work, another thread can be given cpu time.
如果大部分工作都受 CPU 限制,那么产生数百个线程就没什么意义了,因为这不太可能提高性能(实际上可能会使情况变得更糟,记住上下文切换不是免费的)。但是,对于 I/O 绑定工作这样做可能会有所帮助,因为当系统忙于执行此工作时,可以为另一个线程提供 CPU 时间。
Having physical additional cores means two things can truly run in parallel at the hardware level.
拥有额外的物理内核意味着两件事可以真正在硬件级别并行运行。