Thread is a basic unit of execution so each program may have a number of processes associated with it and each process can have a number of threads executing in it so the thread is a basic unit of execution or a basic unit of CPU utilization so that is how we can define a thread.
What is a Thread?
It is a basic unit of CPU utilization now let us see what comprises a thread so a thread comprises of a thread ID a program counter a register set and a stack so each thread comprises of these four unique items and apart from this it shares with other threads belonging to the same process. it’s Code section data section and other operating system resources such as open files and signals so these are the things that comprise a thread and apart from that the thread shares these things like the Code section data section and other operating system resources are shared between threads belonging to the same process.
So as I told you a process can contain different threads so the threads belonging to the same process will share the resources slight open files signals Code section data section and other things so that is how a thread is basically made up so a traditional or heavyweight process has a single thread of control but if a process has multiple threads of control it can perform more than one task at a time so a traditional process also known as a heavyweight process. Had only a single thread of control that means it can have only one thread but this is not a very efficient system but if a process is having more than one threads then each thread will be assigned a different task and hence that process can perform more than one task at a time because it is having multiple and multiple threats will be performing multiple actions so there we see how treads become useful and how using threads makes the system much more efficient.
It is a single-threaded process which we refer to as a traditional or a heavyweight process over here so here we are having only a single thread so this whole block is considered as a process and this process contains only one thread and this is the code section the data section the files the registers and the stack belonging to this process so there is only one thread that means this process can perform only one task at a time.
A multi-threaded process so it is a single process and it is having multiple threads over here so if you see there are three threads in this process and each of these threads they have their own stack and registers so all these. Three threads they have their own stack and registers and if you see on top the code section the data section and the files belonging to this process are shared by these three threads and each thread they have their own stacks and registers so this is a diagram of a multi-threaded process so in this case, this process can perform multiple tasks at a time because each thread will be performing a different task and hence multiple tasks can be performed at a time by the help of this multi-threaded process so from that itself we must have understood that it is much more efficient as compared to the single-threaded process and it will make our computation faster and more efficient so in most of the systems.
For performing a different task so in that way the browser can do more than one task at a time so let’s say for example one of the processes of this browser chrome load exe is used for displaying the web page on the browser window and let’s say that another thread is used for downloading something from the internet so we know that when we are downloading something we can also view the page at the same time so these two things are happening simultaneously that is possible because we are having multiple threads one thread is taking care of displaying and the other thread is taking care of downloading so if we were having a single-threaded process then while the downloading was happening the page could not be displayed or while the page is being displayed the download will not happen so that is this a simple example that I want to take in order to make us understand the usage of multi-threaded processes so different threads perform different tasks and hence multiple tasks can be performed in the same time hence improving our efficiency and speed of computation so we have seen a visual example of how multi thread processes are there in our system now let us see what are the benefits of having multi-threaded processes so from the example that I have taken you must have already understood the benefits of using multi-threaded processes but let us understand these benefits in more detail so the benefits of multi-threaded programming can be broken down into four major categories so let’s see what they are one-by-one so the first benefit is responsiveness multi-threading.
An interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation thereby increasing responsiveness to the user so we are having a better responsiveness if we have multi-threading so I can explain this using the same example that I took about the web browser since we said that we are having different threads one for displaying the web page and another one for downloading and let’s say that another one for the user to interact with the web page so if only one thread was there then until and unless one of the tasks completes the other task cannot be done so the user have to wait for one of the tasks to be complete in order to go to the next task but in case of multi-threading since different threads are simultaneously performing different tasks the responsiveness to the user increases so that is the first benefit of multi-threaded programming.