Thread joinable() function in C++ Last Updated : 26 Oct, 2018 Comments Improve Suggest changes Like Article Like Report 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 Syntax: std::thread::joinable() 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”. 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() { std::this_thread::sleep_for( std::chrono::seconds(1)); } 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"; else 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"; else cout << "NO\n"; // joining t1 t1.join(); // checking joinablity of t1 after calling join() cout << "t1 joinable after join is called? \n"; if (t1.joinable()) cout << "YES\n"; else cout << "NO\n"; return 0; } Output: t1 joinable when default created? NO t1 joinable when put to sleep? YES t1 joinable after join is called? NO Note: The third output will appear 1 sec later because the thread was put to sleep for 1 minute. Comment More infoAdvertise with us Next Article Thread joinable() function in C++ K Kushagra7744 Follow Improve Article Tags : C++ Programs C++ STL CPP-Library cpp-multithreading +1 More Practice Tags : CPPSTL Similar Reads 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 Useful Inbuilt Functions in C++ In-built functions in C++ are those functions that are part of C++ standard libraries. The purpose of inbuilt functions is to provide common and essential functionality that is frequently required in the programming. In this article, we will look at some of the commonly used inbuilt functions in C++ 7 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 std::thread::join() in C++ 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 it 3 min read Function Pointer to Member Function in C++ In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio 3 min read Like