Basics of Multithreading
Multithreading is a topic dreaded by many programmers. Probably its due to the fact that, if not written properly, multithreaded programs can cause bigger blunders than single-threaded programs and the damage done by multiple threads can be harder to assess. Some people find it difficult to understand, while others don’t bother studying it. Personally, I find it to be one of the most fascinating features of a programming language.
Through this blog, I strive to explain the nuances of multithreaded programming in simplest terms. Let’s start with the basic concepts of CPU cores, computer processes and threads, and then try and understand the advantages of multithreaded programming.
Computer Process VS Thread
Simply put, a process is a program in execution. When we execute a program or an application, a process is initiated. Each process comprises of one or more threads.
A thread is nothing but a segment of a process. Threads are execution entities which perform tasks, the executed application is created for. A process completes when all threads finish execution.
Role of CPU Cores
Every thread in a process is a task for the CPU to complete. Most processors today have the ability to have one core run two tasks at a time by creating an extra virtual core.This is called simultaneous-multi threading, or hyperthreading if it’s an intel CPU. These processors are called multi-core processors. So a dual core processor essentially has 4 cores; two physical and two virtual. Each core can execute only one thread at a time.
Why Multithreading?
As discussed above, one process has multiple threads and one CPU core can execute a single thread at a time. If we write a program, that runs threads serially, i.e., queues execution to just one core of the CPU, we are not leveraging the full potential of our multicore processor. The other cores are just sitting idle, while there exist tasks which need to be executed. If we write our program in such a way that it creates multiple threads, for time consuming independent functions, then we will be able to utilize the other CPU cores which were otherwise idle. We will be able to execute our threads in parallel hence reducing execution time of the process.
Let’s get our hands dirty!
We will write a program wherein we will run two functions (execute two tasks). First, we will execute the functions sequentially using the same thread and then we will create separate threads for each. We will note the execution times for both the approaches and see the magic!
Summation1 and Summation2 are our two dummy classes that we need to execute. This could be any business logic that you want to implement. I have just added random loops to increase execution time so that it helps me showcase the results better.
Surprise Surprise!!! (Not really!)
The execution time reduced to one third on using threads as against executing the functions serially. This is the power of multithreading.
This was just a simple example to help you understand the use of multithreading. There are infinite use-cases where you can make remarkable improvements in the way your application executes. I implore you to explore (see what I did there 😉) scenarios where you can apply multithreading, to make the most out of your computing resources.
I believe understanding the basics of something is the best way to learn it. I hope this blog helped you gain clear understanding of the need and utility of multithreaded programming.
There is a lot more to learn about threads; thread lifecycle, synchronicity issues and how to handle them, etc. You can follow my other blog for that.
Practice makes Perfect!
Checkout my GitHub for the code of this blog, and other projects.