Concept of Multi-threading in C++
Ever wondered about your code that It is taking a lot of time to execute. Your code’s time complexity is so much high, even you have optimized your code. Then a question came in your mind, how can I make my code more efficient fast, and reliable. The answer is yes. Here is the role of multi-threading came. Where you can run several functions at the same time in your code and your running time decrease hell a lot.
Thread:
You don’t have to think a lot to know about a thread. You can understand thread by a simple line. It is a group a bunch of instructions that are packed together to executed. When that thread will be initiated in your code.
void fun() {
std::cout<<"fun is calling"<<std::cout<<endl;
}
int main()
{
std::thread t1(fun);
t1.join();
Explanation
Here you can see we have declared and defined a function
named asfun function pointer We have assign it to the
thread t1.
You have a question in your mind we can do the same task without using the thread. Yes, you are right you can do all of it without creating a new thread. Because your code is running inside the main() thread. In this example, we have used only 1 thread. Now you wanna do multiple works simultaneously then what you will do? for understanding this concept let’s jump to the concept of Multi-threading before It becomes more confusing.
Multi-threading:
When you are running more than one thread at the same time then It is known as multi-threading. Let’s go deep down to understand the concept of threading.
Explanation
In the right flowchart we have used the traditional way of coding and inside our main() thread all function has been called one by one and executed. Let's make the time complexity of a single function is O(1) so in this traditional way of coding your overall time complexity
will be if you have let say ‘ n ‘ of function, O(n).
Now let’s come to the left flowchart where we have used the concept of multi-threading and executed all threads at the same time so if the time complexity of running a single thread is O(1) so if you run ‘n’ simultaneously then time-complexity remain same O(1) because all threads are running in parallel.
Traditional Coding
#include <iostream>
void fun1()
{
std::cout<<"fun1() is calling "<<std::endl;
}
void fun2()
{
std::cout<<"fun2() is calling "<<std::endl;
}
void fun3()
{
std::cout<<"fun3() is calling "<<std::endl;
}
int main()
{
fun1();
fun2();
fun3();
std::cin.get();
}
Using Multithreading
#include <iostream>
#include<thread>
void fun1()
{
std::cout<<"fun1() is calling "<<std::endl;
}
void fun2()
{
std::cout<<"fun2() is calling "<<std::endl;
}
void fun3()
{
std::cout<<"fun3() is calling "<<std::endl;
}
int main()
{
std::thread t1(fun1);
std::thread t2(fun2);
std::thread t3(fun3);
t1.join();
t2.join();
t3.join();
std::cin.get();
}
When to use multi-threading
When you are dealing with a lot of data and performing operations on it. You need a mechanism where you can do multiple operations simultaneously on that data. At that time if you will not use multi-threading then your process will become slower. In the industry, slow means No.
So should you use multi-threading always? No. You should not! It depends on what type of application you are developing. Is every module of your code needed multiple processes at the same time? We also have to check the dependency of processes on each other. Because sometimes a single process is dependent on the result of another process. At that point, we have to take care of it.
We are going to talk about it in more detail in further articles on the multi-threading series. Where we are gonna discuss different scenarios where we need multi-threading and where not. We will discuss all the cool stuff of Multi-threading in upcoming articles.
Post a Comment: