单词关系 :: 'Thread'和“核心”
程序创建/能够创建的线程数量与 CPU 拥有的数量或核心有任何关系/限制吗? 线程
这个词与CPU核心
有任何关系吗?
Does the number of threads created/able by a program has any relation/constrain with the number or core that CPU has? Does the word thread
has any relation with Cores of CPU
at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
计算机只能并行运行 number_of_cores 线程。如果系统只有一个核心,则一次只有一个线程运行。多任务处理只能通过在很短的时间内连续运行每个线程来“模拟”。
通常,操作系统在内核上任意调度线程。例如,线程的一个量程可以在一个核心上运行,而同一线程的下一个量程可以在另一个核心上运行。这就是为什么当您运行单个 cpu 密集型线程时,您会看到两个核心都很忙。这允许单线程进程根据不同的可用性,通过在多个内核之间分配负载来利用多核系统。
不仅如此,操作系统允许线程“锁定”在一个或多个内核上,因此它仅在指定的内核上专用运行。这在 Windows 上称为亲和力掩码。当今的多核感知软件可以利用这一点,并将某些线程专用于某些核心来优化其工作负载。游戏这样做是为了将人工智能专用于一个核心,例如为另一个核心提供渲染。为了有效地做到这一点,软件必须知道系统有多少个核心,并根据该数量创建和分配线程。
类似地,OpenMP 等并行编程框架会计算内核数量以高效地并行化操作。它们创建与系统上的核心数量一样多的线程,以获得物理并行的最佳性能。
我认为这是核心数量和线程数量之间关系的唯一情况。
A computer can only run number_of_cores threads in parallel. If a system has only a single core, only one thread runs at once. Multitasking is only "simulated" by running each thread consecutively for very short periods of time.
Normally the OS schedules threads arbitrarily on cores. For instance a thread's one quantum could be run on one core and the same thread's next quantum could be run on another core. That's why you see both cores busy when you run a single cpu-heavy thread. This allows single-threaded processes take advantage of multicore systems by distributing load among multiple cores, depending on varying availability.
More than that, operating systems allow a thread to be "locked" on one or more cores, so it runs dedicated on specified cores only. That's called affinity mask on Windows. Today's multicore aware software can take advantage of that and dedicate certain threads to certain cores to optimize its workload. Games do that for dedicating AI for one core, rendering for another for instance. In order to do that efficiently the software has to know how many cores a system has and create and assign threads based on that number.
Similarly parallel programming frameworks such as OpenMP count the cores to parallelize operations efficiently. They create as many threads as the number of cores on the system to get optimal performance for physical parallelism.
I think that's the only case of a relation between number of cores and number of threads.
机器上的核心数量是能够同时运行的独立处理单元的数量。
程序可以生成的线程数量是程序希望彼此并行运行的逻辑执行单元的数量。
通常,程序可以生成的线程数量受到语言运行时和操作系统的内部线程实现的限制。但是,如果生成的线程多于计算机上的核心数量,则并非所有这些线程都可以彼此并行运行。相反,将以某种方式调度线程,以尝试最大化每个线程的运行时间。拥有比核心更多的线程很好,但是一旦所有核心同时运行线程,引入新线程不一定会获得很大的加速。
在许多情况下,一个程序将有数百个线程,但任何时候只有一小部分线程在运行。其他线程可能正在休眠并等待某些事件发生(用户输入、计时器、网络或磁盘事件等)。这不是问题;由于这些线程没有主动执行任何操作,因此机器不需要为它们分配任何核心,并且可以将其处理能力用于其他线程。
The number of cores on a machine is the number of independent processing units that are capable of running at the same time.
The number of threads that a program can spawn is the number of logical units of execution that the program would like to run in parallel with each other.
Typically, the number of threads a program can spawn is limited by the language runtime and the operating system's internal threading implementation. However, if you spawn more threads than there are cores on the machine, not all of those threads can be running in parallel with one another. Instead, the threads will be scheduled in some fashion to try to maximize the amount of time that each thread is running. It's fine to have more threads than cores, but you won't necessarily get much of a speedup by introducing new threads once all the cores are running threads at the same time.
In many cases, a program will have many hundreds of threads, only a small number of which will be running at any time. The other threads might be sleeping and waiting for some event to occur (user input, a timer, a network or disk event, etc.). This isn't a problem; since those threads aren't actively doing anything, the machine doesn't need to dedicate any cores to them, and can spend its processing power working on other threads.
每个核心可以执行一个线程。如果您有 X 个核心,则 X 个线程可能会同时执行。如果你有 X 个核心和 X + n 个线程,那么最多 X 个线程将同时执行。但是,所有线程都将执行,因为操作系统内核将在所有线程之间共享核心。
Each core can execute one thread. If you have X cores, then X threads may execute simultaneously. If you have X cores and X + n threads, then at most X threads will execute simultaneously. However, all threads will execute, as the operating system kernel will share the cores among all of the threads.