VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-create-a-thread-in-cpp/

⇱ How to Create a Thread in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create a Thread in C++?

Last Updated : 23 Jul, 2025

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:

  1. Function Pointers
  2. Functors
  3. Lambda Expressions

C++ Program to Create a New Thread Using Function Pointer


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.


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++


Comment