std::jthread::joinable

Checks if the std::jthread object identifies an active thread of execution. Specifically, returns true if get_id() != std::jthread::id(). So a default constructed jthread is not joinable.

# Declarations

bool joinable() const noexcept;

(since C++20)

# Return value

true if the std::jthread object identifies an active thread of execution, false otherwise.

# Example

#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
 
void foo()
{
    std::this_thread::sleep_for(500ms);
}
 
int main()
{
    std::cout << std::boolalpha;
 
    std::jthread t;
    std::cout << "before starting, joinable: " << t.joinable() << '\n';
 
    t = std::jthread{foo};
    std::cout << "after starting, joinable: " << t.joinable() << '\n';
 
    t.join();
    std::cout << "after joining, joinable: " << t.joinable() << '\n';
 
    t = std::jthread{foo};
    t.detach();
    std::cout << "after detaching, joinable: " << t.joinable() << '\n';
 
}

# See also