What is MultiThreading

Adil Wadhwania
5 min readMar 26, 2023

--

In this article you will understand about multithreading, how it works, context switching and benefits of multithreading

Multithreading preview image

Multithreading

  • Using Multithreading in an application means executing different parts of the code, at the same time.
  • Each thread will execute a task independent to other thread, it will increase the performance and efficiency of the application.
  • A thread consists of a series of instructions that can be executed independently of the other code.
  • Simple real world example of multithreading that we use every single day is Microsoft offices apps, for instance take the “Microsoft Word”, the typing is done by one thread, the spelling mistakes are detected by other thread and saving the document is done by another thread.
  • Each task is executed independently and the user don’t need to wait for the task to complete, it will be executed concurrently(each task of the thread assigned make progress in quick succession).
  • Suppose “Microsoft word” was single threaded application, then the thread that is used for typing task will check the spelling, so until the thread checks spelling user will wait or get stuck for the response and would not be able to type for that period of time, this could be a problem because user is waiting for long time.
  • And even when you press “ctrl+s” to save the document while typing then the thread that is typing will go to save the content written in the document, until that user will not able to type anything, it means again user is stucked or is in waiting condition.
  • So In this type of cases it is beneficial to use multithreading that will improve the efficiency and user does not need to wait.

No Of Threads

  • Now we are done understanding what is multithreading so the next question that strikes in our mind is how many threads we can create.
  • You can create as many thread as you want but all the threads cannot be executed concurrently, that means you can create 100 threads but cannot execute 100 threads at a time.
  • The no of threads that can be executed depends on the no of cores present in the processor.
  • A core in the CPU can only execute a single thread at any given point of time.
  • That means if you have a single CPU PC with 8 cores in that then parallely it can execute only 8 threads at a time(each core executes single thread).
  • The next question would be like if we have 8 cores then only 8 threads can be executed at a time and all other threads are waiting for the resources to execute, that particular period of time the tasks assigned to the threads that are waiting won’t be executed and due to that the app may become unresponsive or gets stuck, right?
  • As you know in our windows OS we have multiple process running in background and there is a possibility that this processes use the CPU cores to execute, so ideally we cannot even say that when we execute our multithreaded application we will have all the 8 cores available.
  • But we had never face the unresponsiveness when we start no of applications(apps) more than that of our CPU cores in our PC, this is because the processes running in our PC are managed by OS and scheduling of threads execution for each process is also done by system’s scheduler.
  • The OS is designed in a way that it can schedule more than one thread on a core to execute but here the execution would be concurrent(each task of the thread assigned to this core make progress in quick succession).
  • The OS uses context switching technique to execute more than one thread in a core, but the constraint remains unchanged, only one thread will be executed at a time in a core even if more than one thread are scheduled on a CPU’s core by the OS.
  • So the OS can execute more threads than available cores or resources using the context-switching, that happens at core-level in a CPU.

Simply multithreading means running multiple threads(each executing a task independent to other) that are assigned by the system’s scheduler in the available cores of CPU .

Context Switching

  • Context switching is a process that occurs when a OS switches the CPU from one task to another.
  • Here the “context” is the state of the variables that is stored and restored while context switching.
  • The context switching occurs at core-level, a CPU may contain multiple cores, the CPU is a microprocessor chip used to perform arithmetic, logic, I/O and other basic computing operations and the CPU can use the core to carry out tasks.
  • We can execute multiple threads in a core using context switching, the decision of scheduling this threads on a core is done by system’s scheduler.
  • We are done with all the information we need, now let us narrow down to how context switching works, suppose we have two threads assigned to execute in a core then the thread 1 executes for few milliseconds before it switches to thread 2, that will also executes for few milliseconds, this will continue until the both tasks are completed.
  • The CPU saves the current state of the running thread in the registers, load the state of the next thread from the registers to be executed and then resumes execution on the new thread this is how context switching maintains the state of variables.
  • This is how the CPU time and resources are shared across multiple threads to achieve context switching.

Benefits of MultiThreading

  1. Improved Performance
  • By use of multithreading the performance of an application can be improved as the task assigned to multiple threads work independently to each other.
  • The tasks that are divided across multiple threads which adds up to the performance and reduces the time of execution as well.
  • This multiple threads will be executed parallely as well as concurrently based on the resources available.
  • “Parallely as well as concurrently” means, different threads executing in different core then that behaviour is parallel execution because each threads is independent here, and if multiple threads executing in a single core using context-switching that we have discussed is the concurrent behaviour.

2. Resource Utilization

  • By the use of multithreading in our app it will use all the available resources such as CPU core and memory, that would have been idle if we use single threaded application.
  • In this way we can use all the available resources and achieve the desirable performance at lower costs and less hardware.

3. Responsiveness

  • In the starting we have discussed the example of “microsoft word” for multithreading, so that thing applies here. Suppose we have single threaded application and that single thread is reading a file of “1GB”, that will make the user wait for long time to get the response and the application can prompt the unresponsive error also.
  • So it is better to use multithreading application here, if we create a thread to read that big file and let the main thread continue its work, this will help us to prevent from the unresponsiveness error.

“Follow me to stay updated for new topics”

ADIL WADHWANIA :)

--

--

Adil Wadhwania
Adil Wadhwania

Written by Adil Wadhwania

Tech-savvy writer sharing insights on programming,current affairs,sports,great personalities, and new technologies. Follow for engaging and informative content.

Responses (1)