Multithreading in C++ with Examples
What is the Thread?
In C++, a thread is a type of working unit used in a particular process. There are some different processes that are executed simultaneously in the multi-programming operating system.
In the same way, with the help of threads, we can execute the same process multiple times. In this case, each process is associated with a unit called a thread. In multithread processes, there are many threads executed simultaneously, which are independent of one another.
Working of <Thread>
We can create a new thread object with the help of std::thread then we can pass that newly created thread to a callable. Here callable is a type of executable code which is executed during the running of a thread. So whenever there is a need for a thread, we just have to create the object and pass a callable as an argument to its constructer. Once a new thread object is created, then the new thread is launched, and the callable code is executed.
We can define a callable in three ways. These three ways are as follows.
1. Using the function object:
In the thread object, we can use a function object as callable. For implementing that function object, first, we need a class, and inside that class, we have to overload the operator (). When the thread is created, that time the code containing the overloaded function is executed.
With the help of the above command, the thread object is defined. In the process of construction of the thread object, we have to provide the overloaded function, and then we have to specify its argument as the second argument.
2. Using function pointer:
We can define the callable function with the help of the following ways.
Once we define the function, then we have to create the thread object with the callable function. We can do this by the below method.
We can pass the argument to the function after the function name of the object.
3. Using a Lambda expression:
We also have a callable function as a lambda expression. And we can pass the lambda expression to the thread object for the execution of the code.
In the above code, we have to implement the lambda expression; then, we have to pass the lambda expression to the thread object constructer with the help of the following parameter.
std::thread join method, in some cases, we may want to finish the currently executed task before starting a new task.
The best example is when we try to open a GUI application. When we open the application, a thread is loaded, and the GUI is started. We cannot perform any other action during the loading, and initializing is not completed.
Take the following example,
In the above example, the main function will have to wait to continue until thread t1 finishes. In general, the join function of the thread blocks other actions/functionality until the thread calling finishes its execution.
Example of Thread
We present a complete coding example for the creation and execution of the thread in the program shown below.
In the above example, we have created three threads using three different callable, i.e., function pointer, object, and lambda expression. We create two instances of each thread and start them. As shown in the output, three threads operate simultaneously and independently.
Next TopicStatic Object in C++