Concept of Multi-threading in Programming


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. Answer is yes. Here is role of multi threading came. Where you can run several functions at a 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 group of bunch of instruction 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 as fun in our program file and the using function pointer We have assign it to the thread t1.

You have 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 main() thread. In this example we have used only 1 thread. Now you wanna do multiple work 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 then one thread at 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 traditional way of coding and inside our main() thread all function has been called one by one and executed. Lets make 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 a same time so if 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 Multi-threading

#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 operation simultaneously on that data. At that time if you will not use multi-threading then your process will become more slow. 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 same time. We also have to check dependency of processes on each other. Because some time 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 details in further articles on multi-threading series. Where we are gonna discuss different scenario where we need multi-threading and where not. We will discuss all the cool stuff of Multi-threading in upcoming articles.

Subscribe to our YouTube channel

To get explanation of each and every article of coderca. Go on our official Youtube channel.Check it out from given link,


Best C++ Books on Amazon

This Post Has 11 Comments

  1. Pradyuman Mani Tripathi

    Excellent

  2. Pradyuman Mani Tripathi

    Everything is very well

  3. Lillion

    Crisp and clear explanation on Multithreading. There are some points that are not covered regarding multithreading in this article.
    Hope you gonna write about them soon.

    1. Anmol Dwivedi

      Thanks.
      Yeah I am planning to write more on multi-threading.I’m in research mode.

  4. shipra singh

    I have doubt.
    In the code How can you pass function the fun() as parameter in threads since it is function?
    Can you elaborate more?

    1. Anmol Dwivedi

      We can pass function as parameter. Study concept of function pointer you will understand everything.
      🙂

  5. Animesh

    good one.

  6. annie chill

    very informative article.

Leave a Reply