Updated June 28, 2023
Introduction to C++ thread( )
In C++, class thread denotes a single thread of execution. It permits the execution of several functions at the same time. The class that denotes the thread class in C++ is std::thread. To start a thread, you need to create a new thread object and pass it to the code that will be executed. The execution of the thread begins from the top-level method specified in the constructor of the thread object.
How thread() Function Works in C++?
As previously mentioned, to create a new thread, you need to use std::thread in C++, and the thread should be associated with a callable object.
Defining a Callable
In order to define a callable, different ways can be used.
1. Callable Using Function Object
In this technique, a function object can be set as callable. For that, a class is needed, and operator () has to be overloaded inside that class. The overloaded methods contain the program that must be executed when the thread is created.
Code:
// Function object class definition
class fn_object {
// Overload ( ) operator
void operator ( ) ( params )
{
// write your code
}
}
// Thread object creation
std::thread th_obj( fn_object( ) , params )
In this code, the thread object is defined by passing an overloaded function as the first parameter to the thread constructor, and the arguments (params) are specified as the second parameter.
2. Callable Using Function Pointer
In this technique, a function pointer can be set as callable. It can be defined as mentioned below.
Code:
void funct( params )
{
// write your code
}
When this function is defined, a thread can be created using this function funct as callable, as shown below.
std::thread th_obj ( funct , params ) ;
Here, the arguments or params passed to the method is provided next to the name of the function in the thread object.
3. Callable Using Lambda Expression
In addition to the above methods, a callable can also be created using a lambda expression. It is done by passing it to the thread object for the purpose of execution. Below is the sample code snippet for the same.
Code:
// Lambda expression definition
auto lm = [ ] ( params )
{
// write your code
};
std::thread th_obj( lm , params ) ;
In order to wait for a thread, the std::thread::join( ) function has to be used. This method makes the present thread wait till the thread recognized by *this has stopped executing. For example, to block the main thread till thread th1 has terminated, the following code can be used.
Code:
int main( )
{
std::thread th1( callable_code ) ;
. . . .
th1.join( );
. . . . .
}
In this example, the main method must wait until the thread th1 stops. That is, the join method of the thread blocks other activities or functionality till the thread calling stops its execution.
Examples of C++ thread( )
Given below are the examples mentioned:
Example #1
Code:
//C++ program to implement thread
#include <string>
#include <iostream>
#include <thread>
using namespace std;
// function that has to be executed on the thread
void func(string txt)
{
cout << " function func displays : " << txt;
}
int main()
{
// A new thread is created and executed
thread th(func, "Happy weekend . . . ");
// Main thread waits for the new thread th to stop execution and as a result, its own execution gets blocked
th.join();
}
Output:
In this program, the function that has to be executed on the thread is created. In this program, the main thread waits for the new thread to complete its execution before continuing further. This means that the main thread’s execution gets blocked until the new thread finishes.
Example #2
Code:
// CPP program to implement thread using object as callables.
#include <iostream>
#include <thread>
using namespace std;
// Callable object
class th_obj {
public:
void operator()(int num)
{
for (int i = 0; i < num; i++)
cout << "Thread that uses function object as callable is working :" << i << "\n";
}
};
int main()
{
// Thread starts by using function object as callable
thread t2(th_obj(), 4);
// Waiting for finishing of thread t2
t2.join();
return 0;
}
Output:
In this program, a callable object is created using a function object, and a thread is subsequently created. When the code is executed, the callable object’s function is called four times, resulting in the message being displayed four times. The number of times the message is displayed corresponds to the value passed as an argument, which in this case is 4.
Recommended Articles
This is a guide to C++ thread( ). Here we discuss the introduction and how thread() function works in C++, along with different examples. You may also have a look at the following articles to learn more –