Open In App

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

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads