multithreading 线程与核心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3211936/
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
Threads vs Cores
提问by teonghan
Say if I have a processor like thiswhich says # cores = 4, # threads = 4 and without Hyper-threading support.
说,如果我有这样的处理器这样它说#核= 4,#线程= 4,不支持超线程。
Does that mean I can run 4 simultaneous program/process (since a core is capable of running only one thread)? Or does that mean I can run 4 x 4 = 16 program/process simultaneously?
这是否意味着我可以同时运行 4 个程序/进程(因为一个核心只能运行一个线程)?或者这是否意味着我可以同时运行 4 x 4 = 16 个程序/进程?
From my digging, if no Hyper-threading, there will be only 1 thread (process) per core. Correct me if I am wrong.
从我的挖掘来看,如果没有超线程,每个内核将只有 1 个线程(进程)。如果我错了,请纠正我。
采纳答案by Marcelo Cantos
That's basically correct, with the obvious qualifier that most operating systems let you execute far more tasks simultaneously than there are cores or threads, which they accomplish by interleaving the executing of instructions.
这基本上是正确的,明显的限定词是大多数操作系统允许您同时执行比内核或线程多得多的任务,它们通过交错执行指令来完成。
A system with hyperthreading generally has twice as many hardware threads as physical cores.
具有超线程的系统的硬件线程通常是物理内核的两倍。
回答by AlexanderMP
A thread differs from a process. A process can have many threads. A thread is a sequence of commands that have a certain order. A logical core can execute on sequence of commands. The operating system distributes all the threads to all the logical cores available, and if there are more threads than cores, threads are processed in a fast cue, and the core switches from one to another very fast.
线程不同于进程。一个进程可以有多个线程。线程是具有特定顺序的一系列命令。逻辑核心可以按命令序列执行。操作系统将所有线程分配给所有可用的逻辑内核,如果线程数多于内核数,则以快速提示处理线程,并且内核非常快速地从一个切换到另一个。
It will look like all the threads run simultaneously, when actually the OS distributes CPU time among them.
看起来所有线程同时运行,而实际上操作系统在它们之间分配 CPU 时间。
Having multiple cores gives the advantage that less concurrent threads will be placed on one single core, less switching between threads = greater speed.
拥有多个内核的优势在于,单个内核上放置的并发线程更少,线程之间的切换更少 = 速度更快。
Hyper-threading creates 2 logical cores on 1 physical core, and makes switching between threads much faster.
超线程在 1 个物理核心上创建 2 个逻辑核心,并且可以更快地在线程之间切换。
回答by Ira Baxter
The term thread is generally used as a description of an operating system concept that has the potentialto execute independently of other threads. Whether it does so depends on whether it is stuck waiting for some event (disk or screen I/O, message queue), or if there are enough physical CPUs (hyperthreaded or not) to allow it run in the face of other non-waiting threads.
术语线程通常用于描述具有独立于其他线程执行的潜力的操作系统概念。它是否这样做取决于它是否卡在等待某个事件(磁盘或屏幕 I/O、消息队列),或者是否有足够的物理 CPU(超线程与否)允许它在面对其他非等待时运行线程。
Hyperthreading is a CPU vendor term that means a single core, that can multiplex its attention between two computations. The easy way to think about a hyperthreaded core is as if you had two real CPUs, both slightly slower than what the manufacture says the core can actually do.
超线程是 CPU 供应商的术语,意思是单核,可以在两个计算之间多路复用它的注意力。考虑超线程内核的简单方法是,好像您有两个真正的 CPU,它们都比制造商所说的内核实际可以执行的速度稍慢。
回答by Mikael Brandin
Basically this is up to the OS. A thread is a high-level construct holding a instruction pointer, and where the OS places a threads execution on a suitable logical processor. So with 4 cores you can basically execute 4 instructions in parallell. Where as a thread simply contains information about what instructions to execute and the instructions placement in memory.
基本上这取决于操作系统。线程是包含指令指针的高级构造,操作系统将线程执行放置在合适的逻辑处理器上。因此,使用 4 个内核,您基本上可以并行执行 4 条指令。线程只包含有关要执行的指令和指令在内存中的位置的信息。
An application normally uses a single process during execution and the OS switches between processes to give all processes "equal" process time. When an application deploys multiple threads the processes allocates more than one slot for execution but shares memory between threads.
应用程序通常在执行期间使用单个进程,而操作系统在进程之间切换以给予所有进程“相等”的处理时间。当应用程序部署多个线程时,进程会分配多个插槽用于执行,但在线程之间共享内存。
Normally you make a difference between concurrent and parallell execution. Where parallell execution is when you actually physically execute instructions of more than one logical processor and concurrent execution is the the frequent switching of a single logical processor giving the apperence of parallell execution.
通常你会区分并发和并行执行。并行执行是当您实际执行多个逻辑处理器的指令时,并发执行是单个逻辑处理器的频繁切换,从而产生并行执行的外观。