Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
std::thread::join() in C++
Next article icon

std::thread::join() in C++

Last Updated : 20 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::thread::join() is a standard library function in C++ that is used to block the current thread until the thread identified by *this finishes its execution. It means that it will hold the current thread at the point of its call until the thread associated with that join() function finishes its execution.

It is defined in the <thread> header file as a member function of the std::thread class.

Syntax of std::thread::join()

void join();

Parameters:

This method does not accept any parameters.

Return Value:

This function does not return a value.

Example of std::thread::join()

The below example demonstrates how to use the std::thread::join() function to join the threads.

C
// C++ program to illustrate // std::thread::join() function in C++ #include <iostream> #include <thread> using namespace std;  void thread_function() {     for (int i = 0; i < 2; i++)         cout << "Thread function Executing" << endl; }  int main() {     thread threadObj(thread_function);      for (int i = 0; i < 10; i++)         cout << "Display From Main Thread" << endl;      threadObj.join();     cout << "Exit of Main function" << endl;     return 0; } 


Output

Display From Main Thread
Display From Main Thread
Thread function Executing
Thread function Executing
Exit of Main function

Important Points:

  1. The std::thread::join() function blocks the current thread until the thread identified by *this finishes its execution. If the thread has already finished execution, then join() returns immediately.
  2. The join() function is used to ensure that a thread has completed its execution before the program exits. If join() is not called before the main function finishes, and the thread is still executing, then the program will terminate abruptly.
  3. A thread can be joined only once, and once it is joined, the thread becomes unjoinable. If you attempt to join a thread more than once, the program will terminate.
  4. If you want to make the thread unjoinable without blocking the current thread, you can use the detach() function instead of join(). However, you must be careful when using detach(), because if the main thread finishes execution before the detached thread, the program will terminate, and any resources used by the detached thread will not be cleaned up.

std::thread::joinable() Function

The thread has to be joinable for the join() function to work. We can check whether the thread is joinable using the joinable() member function of std::thread class.

Example of std::thread::joinable()

C++
// C++ program to illustrate // std::thread::joinable() function in C++ #include <iostream> #include <thread> using namespace std;  // thread callable void thread_function() {     for (int i = 0; i < 2; i++)         cout << "Thread function Executing" << endl; }  int main() {     // creating object of std::thread class     thread threadObj(thread_function);      for (int i = 0; i < 10; i++)         cout << "Display From Main Thread" << endl;      // detaching thread     threadObj.detach();      // checking if the thread is joinable.     if (threadObj.joinable()) {         threadObj.join();         cout << "Thread is Joined" << endl;     }     else {         cout << "Thread is not joinable." << endl;     }      cout << "Exit of Main function" << endl;      return 0; } 


Output

Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Display From Main Thread
Thread is not joinable.
Exit of Main function



Next Article
std::thread::join() in C++

S

subramanyasmgm
Improve
Article Tags :
  • C++ Programs
  • C++
  • CPP-Functions
  • cpp-multithreading
Practice Tags :
  • CPP

Similar Reads

    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
    Thread joinable() function in C++
    Thread::joinable is an in-built function in C++ std::thread. It is an observer function which means it observes a state and then returns the corresponding output and checks whether the thread object is joinable or not. A thread object is said to be joinable if it identifies/represent an active threa
    2 min read
    Thread get_id() function in C++
    Thread::get_id() is an in-built function in C++ std::thread. It is an observer function which means it observes a state and then returns the corresponding output. This function returns the value of std::thread::id thus identifying the thread associated with *this.Syntax:   thread_name.get_id(); Para
    2 min read
    How to Create a Thread in C++?
    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 t
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences