Definition of 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. In order to start a thread, a new thread object has to be created and it has to be passed to the executing code that has to be called. Once the linked threads object is constructed, the execution of thread starts from top-level method that delivered as constructor agreement. Let us see more on this topic in the below sections.
How thread() Function works in C++?
As already mentioned above, in order to create a new thread, std:: thread is needed and the thread has to be callable. A callable is an executable program that has to be executed when the thread runs. That is, if a new thread is needed, an object has to be created for std:: thread, and a callable has to be passed as an argument to the constructor. Once this is done, the created new thread starts, and the code offered by callable gets executed.
Defining a Callable
In order to define a callable, different ways can be used. Let us see more on it below.
- Callable Using Function Object
- Callable Using Function Pointer
- Callable Using Lambda Expression
1. Callable Using Function Object
In this technique, a function object can be set as callable. For that, a class is needed and inside that class, operator () has to be overloaded. The overloaded methods contain the program that has to be executed when the thread gets 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 )
Here, look at the way iin which the definition of thread object is done. An overloaded function is passed as the first parameter to the thread object and then arguments (params) are mentioned as the second argument.
2. Callable Using Function Pointer
In this technique, a function pointer can be set as callable. It can be defined as mentioned below.

4.5 (8,820 ratings)
View Course
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 Function Pointer
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 ) ;
Here, a lambda expression lm is defined and passed as the first argument to the constructor of the thread object followed by its params or parameters as the second argument.
Note: Normally, we have to wait for the termination of thread before any action is done, once a thread has begun. For example, suppose we are giving a task to a thread to initialize the Graphical User Interface of an application. So, we have to wait for the thread to stop to make sure that the Graphical User Interface has loaded correctly.
In order to wait for a thread, 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, in order 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 has to wait till 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( )
Let us see some sample programs on thread in C++.
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. Main thread waits for the new thread to stop execution and as a result, its own execution gets blocked. On executing the code, thread gets called and the message displayed as shown below.
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 thread is created. On executing the code, it gets called 4 times, and the message gets displayed four times as the number given as argument is 4.
Recommended Articles
This is a guide to C++ thread( ). Here we also discuss the definition and how thread() function works in c++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –