How to Create a Thread in C++?
Last Updated :
02 Feb, 2024
A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++.
How to Create a Thread in C++?
In C++, the std::thread is a class template that is used to create and manage threads. Also, while creating a thread, we need to pass the instructions that the thread should execute. These instructions can be passed in three forms:
- Function Pointers
- Functors
- Lambda Expressions
C++ Program to Create a New Thread Using Function Pointer
C++
// C++ Program to Create a new thread using function pointer
// Function
#include <iostream>
#include <thread>
using namespace std;
// Function to be executed by the new thread
void myFunction()
{
cout << "Hello from the new thread!" << endl;
}
int main()
{
// Create a new thread that calls myFunction
thread newThread(myFunction);
// Wait for the new thread to finish execution
newThread.join();
cout << "Hello from the main thread!" << endl;
return 0;
}
Output
Hello from the new thread!
Hello from the main thread!
In this program, we first define a function myFunction that will be executed by the new thread. We then create a new thread newThread and pass the function pointer to its constructor. The join method is called on newThread to wait for the new thread to finish execution before the main thread continues.
C++ Program to Create a New Thread Using Lambda Expression
We can also create this thread using lambda expression.
C++
// C++ Program to Create a Thread with a Lambda Expression
#include <iostream>
#include <thread>
using namespace std;
int main()
{
// Create a new thread that calls a lambda expression
thread newThread([] {
cout << "Hello from the new thread!" << endl;
});
// Wait for the new thread to finish execution
newThread.join();
cout << "Hello from the main thread!" << endl;
return 0;
}
Output
Hello from the new thread!
Hello from the main thread!
In this program, we create a new thread newThread
and pass a lambda expression to its constructor. The lambda expression outputs a message to the console. The join
method is called on newThread
to wait for the new thread to finish execution before the main thread continues.
To know more about multithreading, refer to the article - Multithreading in C++
Similar Reads
How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth
2 min read
How to Detach a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to detach a thread in C++. What does Detaching a Thread mean?Detaching a thread means allowing the thread to
2 min read
How to Join a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to join a thread in C++. How to Join a Thread in C++?Joining a thread is a means to wait for the thread to c
2 min read
How to Add Timed Delay in C++? In C++, there is the functionality of delay or inactive state which allows the execution to be delayed for a specific period of time. This is often referred to as a âtimed delayâ. In this article, we will learn how to add timed delay in C++. Timed Delay in C++To add a timed delay in C++, we can use
2 min read
How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use
2 min read