单个CPU如何处理多线程和多进程应用程序? [英] How does a single CPU handle Multi-threaded and multi-process applications?

查看:657
本文介绍了单个CPU如何处理多线程和多进程应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我所阅读的那样,对于多进程应用程序,单个CPU一次只能处理一个任务,从而在两个进程之间切换上下文.在多线程应用程序中,单个CPU可以处理多个线程.我不明白这个.如果只有一个CPU,CPU一次只能处理一个线程吗?如果是,那么如果CPU一次可以处理一件事情,那么拥有多线程应用程序相对于多进程应用程序的优势在哪里.

解决方案

TL; DR

单核上的多线程可以通过使用线程和指令级并行性来加快应用程序的运行速度.

如果单个CPU具有多个内核,它将在每个内核上运行一个进程.如果没有,则需要在单个内核上的进程之间进行切换.

多线程和多处理可以结合使用以获得更好的结果.

完整说明

在多处理系统包括多个完整的处理单元的情况下,多线程旨在通过使用线程级以及指令级并行性来提高单个内核的利用率.由于这两种技术是互补的,因此有时将它们结合在具有多个多线程CPU的系统中和具有多个多线程内核的CPU中. 多线程|维基百科

示例

单个CPU以这种方式处理多线程.

假设我们有两个进程AB,它们需要运行一组命令.在执行每个命令之后,线程需要结果.这是它们需要运行的线程和命令.

| # | Thread A | Thread B|
|---|----------|---------|
| 1 | 1        | 5       |
| 2 | 3        | 1       |
| 3 | Wait     | 3       |
| 4 | 4        | 2       |

现在让我们看看CPU如何(理论上)执行这些操作

CPU从线程A

开始

CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
5 1 x x x x (3) # Thread B, command 1 (5)
3 5 1 x x x (4) # Thread A, command 2 (3)
1 3 5 1 x x (5) # Thread B, command 2 (1)
3 1 3 5 1 x (6) # Thread B, command 3 (3)... A is waiting for result of command 2
2 3 1 3 5 1 (7) # Thread B, command 4 (2)
x 2 3 1 3 5 (8)
x x 2 3 1 3 (9)
x x x 2 3 1 (10)
4 x x x 2 3 (11) # Thread A, command 4... A now has the result and can continue.
x 4 x x x 2 (12)
x x 4 x x x (13)
x x x 4 x x (14)
x x x x 4 x (15)
x x x x x 4 (16)
x x x x x x (17)

这是没有多线程的情况.

CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
3 1 x x x x (3) # Thread A, command 2 (3)... A is waiting for result of command 2
x 3 1 x x x (4)
x x 3 1 x x (5)
x x x 3 1 x (6)
x x x x 3 1 (7)
x x x x x 3 (8)
4 x x x x x (9)  # Thread A, command 4 (4)... A now has the result and can continue
x 4 x x x x (10)
x x 4 x x x (11)
x x x 4 x x (12)
x x x x 4 x (13)
x x x x x 4 (14)
5 x x x x x (15) # Thread B, command 1 (5)
1 5 x x x x (16) # Thread B, command 2 (1)
3 1 5 x x x (17) # Thread B, command 3 (3)
2 3 1 5 x x (18) # Thread B, command 4 (2)
x 2 3 1 5 x (19)
x x 2 3 1 5 (20)
x x x 2 3 1 (21)
x x x x 2 3 (22)
x x x x x 2 (23)
x x x x x x (24)

因此,具有多线程的线程将在17个时间步长之后完成,而无需花费24.

问题?

As I have read that for a multi-process application, a single CPU can handle only one task at a time, switching contexts between two processes. In a multi-threaded application, a single CPU can handle multiple threads. I do not understand this. Does the CPU handle one thread at a time if there is only one CPU? If yes, where is the advantage of having multi-threaded application vs multi-process application if CPU can handle one thing at a time.

解决方案

TL;DR

Multithreading on a single core can speed up the application by using thread and instruction level parallelism.

If a single CPU has multiple cores it will run a process on each of the cores. If it does not, it will need to switch between processes on the single core.

Multithreading and multiprocessing can be combined for better results.

Full Explanation

Where multiprocessing systems include multiple complete processing units, multithreading aims to increase utilization of a single core by using thread-level as well as instruction-level parallelism. As the two techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores. Multithreading | WIkipedia

Example

A single CPU handles multi-threading in this way.

Let's say that we have two processes A and B which need to run a set of commands. After each command, the threads need the result. Here are the threads and the commands they need to run.

| # | Thread A | Thread B|
|---|----------|---------|
| 1 | 1        | 5       |
| 2 | 3        | 1       |
| 3 | Wait     | 3       |
| 4 | 4        | 2       |

Now lets look at how the CPU would execute those (theoretically)

CPU Starts with thread A

CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
5 1 x x x x (3) # Thread B, command 1 (5)
3 5 1 x x x (4) # Thread A, command 2 (3)
1 3 5 1 x x (5) # Thread B, command 2 (1)
3 1 3 5 1 x (6) # Thread B, command 3 (3)... A is waiting for result of command 2
2 3 1 3 5 1 (7) # Thread B, command 4 (2)
x 2 3 1 3 5 (8)
x x 2 3 1 3 (9)
x x x 2 3 1 (10)
4 x x x 2 3 (11) # Thread A, command 4... A now has the result and can continue.
x 4 x x x 2 (12)
x x 4 x x x (13)
x x x 4 x x (14)
x x x x 4 x (15)
x x x x x 4 (16)
x x x x x x (17)

This is how that would look without multi threading.

CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
3 1 x x x x (3) # Thread A, command 2 (3)... A is waiting for result of command 2
x 3 1 x x x (4)
x x 3 1 x x (5)
x x x 3 1 x (6)
x x x x 3 1 (7)
x x x x x 3 (8)
4 x x x x x (9)  # Thread A, command 4 (4)... A now has the result and can continue
x 4 x x x x (10)
x x 4 x x x (11)
x x x 4 x x (12)
x x x x 4 x (13)
x x x x x 4 (14)
5 x x x x x (15) # Thread B, command 1 (5)
1 5 x x x x (16) # Thread B, command 2 (1)
3 1 5 x x x (17) # Thread B, command 3 (3)
2 3 1 5 x x (18) # Thread B, command 4 (2)
x 2 3 1 5 x (19)
x x 2 3 1 5 (20)
x x x 2 3 1 (21)
x x x x 2 3 (22)
x x x x x 2 (23)
x x x x x x (24)

Thus with multithreading the threads would complete after 17 time steps, without it would take 24.

Questions?

这篇关于单个CPU如何处理多线程和多进程应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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