Threading in CPP

asheesh kumar singhal
3 min readDec 17, 2021

Hello Everyone!! In this blog I will discuss about concurrency and multi-threading in C++. This blog doesn’t talks about the basic concepts of threads and process but will talk need of multi threading, about some of the features of threading in C++and how to achieve this.

Threading and Concurrency concepts were added to CPP11. CPP 11 offers stand alone module and that supports starting, controlling and managing threads. This module is thread module.

#include <thread>

Concurrency and multi threading are methods by which a program/ application can execute multiple process simultaneously/at the same time. This helps to improve the efficiency of CPU utilization.When we are building a large application which will have multiple components and these components need to be executed simultaneously, we do this by creating multiple threads of these sub components.

  1. need of multiple threads
    Consider a situation where you want to download a data, may be an image from the server, but that takes time. It takes time to download an image because of the internet speed and request handling of the server. Now, if this long running task is given to main thread, main thread will be blocked until download is complete. So this would mean that user will not be able to give input to the application until download is complete. Thus it may go into “Not Responding” State, if we try to make any action over it.
  2. We need to understand that main thread is resp is for all operations in the application. It is also responsible to take inputs from user. If this thread is blocked and then it will not be able to take input from the user and thus it will froze the application. Thus to give a smooth user experience we also try to use threads.

2) Creation of threads in CPP11:

we create threads in cpp using std::thread class. this class is available in thread module and can be used by including the module #include “thread”.
The constructor returns as soon as its created and does not wait for the thread to start.

to create a new thread:

  1. create an instance of thread class
  2. pass the address of the method to be execucted as a separate process in the constructor of the thread instance
  3. if its a method of the class, then pass the method address using namespace and the instance of the class whose method you wish to call
  4. At this point a new thread will be created and download function will be executed in that thread and execution of main will continue.
  5. So now two threads are running in parallel, One is primary thread the main thread and another is secondary thread which is running a long running parallel task.

Since it is possible that main (primary thread ) is not a long running thread and may end first then the secondary (component ) thread, once the main thread completes its execution then it will call function to exit the application. this will lead to killing of worker thread in between of its process. hence its a good choice to call join on the thread so that main thread waits for the completion of the execution of worker thread.

Detached thread is a thread that is no longer associated with the main thread and will run in background as an independent process or a daemon process. This thread is not joinable. Remember a thread which is not detached is joinable and once the thread becomes detached, its no longer joinable. (Joinable means primary thread /main thread) will not wait for worker thread to finish.

The thread class constructor is a varidiac template.It can take any number of arguments. Also by default the arguments are passed by value to the constructor rather than pass by reference. hence, if we want only one instance of the object we pass to the argument list of the Thread object, then we should use std::ref() and std::cref() (for constants) so that there is no copy of arguments passed and only one argument exists.

///insert code

--

--