多线程,并发线程如何工作? [英] Multi-threading, how do concurrent threads work?

查看:66
本文介绍了多线程,并发线程如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有双核CPU,这是否意味着它最多可以运行2个线程?

If I have a dual core CPU, does it mean that it can run a maximum of 2 threads?

然后;如果是这样,那么一个看似受CPU限制的并发线程如何运行4个并发线程? (因为双核PC最多只能运行2个.)

Then; if so, how does one run 4 concurrent threads, when they are seemingly limited by their CPU, to two? (Since it can only run maximum of 2 for a dual core PC).

推荐答案

这是一个非常非常大的问题.

This is a very big question.

基本上您是对的,对于双核CPU,当前只能执行两个线程.但是,实际计划执行的线程多于两个.此外,操作系统可以(几乎)在任何时间中断正在运行的线程,从而有效地中断该线程的执行,以允许另一个线程运行.

Basically you are correct that with a dual-core CPU only two threads can be currently executing. However, more threads than two are actually scheduled to execute. Furthermore, a running thread can be interrupted at (almost) any time by the operating system, effectively halting execution of that thread to allow another thread to be run.

有很多因素影响线程如何中断和运行.每个线程都有一个给定的时间片"在其中执行,并且在该时间片过去之后,该线程可以停止以允许其他线程执行(如果有任何线程正在等待).还可以分配线程优先级,以使优先级较高的任务优先于优先级较低的任务.

There are a lot of factors that weigh into how threads are interrupted and run. Each thread has a given "time-slice" in which to execute and after that time-slice has elapsed that thread may be stopped to allow other threads to execute (if any are waiting). There are also thread priorities that can be assigned that allow for higher priority tasks to take precedence over lower priority tasks.

一些可以从主CPU卸载(到GPU或磁盘控制器)的工作也可以与其他线程并行运行.

Some work that can be offloaded from the main CPU (to the GPU or to a disk controller) can also be run in parallel with other threads.

建议您学习基础知识.

这篇关于多线程,并发线程如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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