How does a single CPU handle Multi-threaded and multi-process applications

cpumultithreading

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.

Best Answer

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?