CPU Multithreading || Why we use multithreading || 10% beneficiary to Zero to Infinity

in Zero to Infinity3 years ago (edited)

processor-g77c0c72a4_1920.jpg
Source

Hello, guys today I will talk about CPU multi-threading. I am going to explain what is multithreading and why we want to use it. Multi-threading sounds really scary in reality but it is a very practical and straightforward concept. In short, multi-threading enables us to do parallel computing efficiently. let's start our explanation of multi-threading by first discussing how a process works.

WhatsApp Image 2022-04-01 at 5.14.55 AM.jpeg

When you start a program it is first loaded and given an address space in the main memory from your hard disk. After being loaded in memory it is then scheduled into an available CPU. When a process is scheduled on to a CPU the process instructions are executed sequentially.


ooo.PNG

Output:

0000.PNG

Take for example this simple program that prints out "Hello World!" to the console. When we run this program the CPU will execute each line of the program sequentially and it will keep track of which instruction it is about to execute by using what's known as a program counter.

The program counter represents the heart of an execution context. An execution context is a current state of a running process that includes data like, what instruction the CPU is currently executing. If you have multiple CPUs and even in the case of a single CPU, you could have multiple execution contexts within a single process. We refer to these execution contexts in an abstract data structure called a thread.


WhatsApp Image 2022-04-01 at 5.14.54 AM.jpeg

In this diagram, we see that process two has several threads. There is also a variable X that processes to create all three of the threads and process to have the ability to read and write to the memory location where variable X is located. If variable X were to be incremented every time we completed a for loop iteration within process two, there's a possibility that each of these threads may increment the variable.

As the programmer we have no control over which thread accesses variable X and when it accesses variable X. The reason for this is that the operating system scheduler schedules processes and threads according to its scheduling algorithm. Whatever scheduling algorithms on our operating system that's what we have to use. This can cause really nasty issues in multi-threaded programs. In order to implement multi-threaded programs we almost certainly have to use what's known as a synchronization construct.


Multi-threading allows us to perform parallel computing efficiently. We could have multiple processes running on multiple CPUs. In doing so we can avoid the problem of synchronization between threads and a multi-threaded process because each process has its own address space that other processes are not allowed to write to and the operating system actively prevents them from doing this.

However, address spaces can be very large and we may only need to run a portion of the program rather than all of it each time we run it.

Multiserver.png
Source

One of the most common examples of multi-threading is in the case of a web server. If we have a web server that is serving some HTML files and when the web server receives an HTTP GET to request it will provide those files to the requesting client. The web server like any program will execute its instructions sequentially when it receives a request.

However, the webserver may receive multiple requests for those HTML files at the same time. If it does those requests are queued and have to wait for the server to complete requests that it has already received. Thankfully most web servers today are multi-threaded and the server will either create a pool of threads or dynamically create new threads when an HTTP GET request is received. That's why when you visit sites like Facebook and Amazon you typically receive a response almost immediately. Otherwise, if those servers weren't multi-threaded you would literally have to wait in a queue until the server handled everyone's requests who visited the site before you.


Conclusion

Multithreading helps us to execute many I/O requests in just one single process. We can execute multiple processes for computation and I/O. We can use any application with more responsiveness. The application does not freeze more often with multithreading. CPU has more communication with the other hardware. CPU multithreading helps simplify other instructions.

Hope you love my post about CPU Multithreading. If you have anything to ask please feel free to ask me any question in the comment sections below.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 60216.66
ETH 2326.87
USDT 1.00
SBD 2.48