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 thread of execution.

A thread is not joinable if:

  • It was default-constructed
  • If either of its member join or detach has been called
  • It has been moved elsewhere



Parameters: This function does not accepts any parameters.

Return Value: It is a boolean type function and returns true when the thread object is
joinable. It returns false if the thread object is not joinable.

The following program demonstrate the use of std::thread::joinable()

Note: On the online IDE this program will show error. To compile this, use the flag “-pthread” on g++ compilers compilation with the help of command “g++ –std=c++14 -pthread file.cpp”.





// C++ program to demonstrate the use of
// std::thread::joinable()
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
// function to put thread to sleep
void threadFunc()
int main()
    std::thread t1; // declaring the thread
    cout << "t1 joinable when default created? \n";
    // checking if it is joinable
    if (t1.joinable())
        cout << "YES\n";
        cout << "NO\n";
    // calling the function threadFunc
    // to put thread to sleep
    t1 = std::thread(threadFunc);
    cout << "t1 joinable when put to sleep? \n";
    // checking if t1 is joinable
    if (t1.joinable())
        cout << "YES\n";
        cout << "NO\n";
    // joining t1
    // checking joinablity of t1 after calling join()
    cout << "t1 joinable after join is called? \n";
    if (t1.joinable())
        cout << "YES\n";
        cout << "NO\n";
    return 0;



t1 joinable when default created? 
t1 joinable when put to sleep? 
t1 joinable after join is called? 

Note: The third output will appear 1 sec later because the thread was put to sleep for 1 minute.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.