C++ Logo

std-proposals

Advanced search

[std-proposals] Make std::thread not joinable after thread function ends.

From: Smith, Jim <jim.smith_at_[hidden]>
Date: Sun, 18 Jun 2023 20:25:51 +0000
Hi All,

It would be convenient if std::thread would automatically become not joinable after the thread function ends.

In the below code snippet, thread1 is still joinable after its function ends.

std::thread thread1([] () {
std::cout << "Thread1 ended\n";
});

std::thread thread2([] () {
for(long l = 0; l < 99999; ++l) { /* nothing */ }
std::cout << "Thread2 ended\n";
});

thread2.join();

if(thread1.joinable()) {
std::cout << "Thread1 is joinable\n";
}
else {
std::cout << "Thread1 is not joinable\n";
}

thread1.join();

Thanks,
- James S

Received on 2023-06-18 20:26:08