C++ Thread Library - Function joinable


Advertisements

Description

It returns whether the thread object is joinable.

Declaration

Following is the declaration for std::thread::joinable function.

bool joinable() const noexcept;

C++11

bool joinable() const noexcept;

Parameters

none

Return Value

It returns true if the thread is joinable or else false.

Exceptions

No-throw guarantee − never throws exceptions.

Data races

The object is accessed.

Example

In below example for std::thread::joinable.

#include <iostream>
#include <thread>
#include <chrono>
 
void foo() {
   std::this_thread::sleep_for(std::chrono::seconds(2));
}
 
int main() {
   std::thread t;
   std::cout << "before joinable: " << t.joinable() << '\n';

   t = std::thread(foo);
   std::cout << "after joinable: " << t.joinable() << '\n';

   t.join();
   std::cout << "after joining, joinable: " << t.joinable() << '\n';
}

The output should be like this −

before joinable: 0
after joinable: 1
after joining, joinable: 0
thread.htm
Advertisements