std::jthread::joinable(3)
π std::jthread::joinable(3)
okay so bestie, welcome to the <std::jthread> corner of the C++ Standard Library β this is page std::jthread::joinable(3) and it is, no cap, one of the shortest little baddies in the whole header. literally a vibe check for your thread. π¦
β¨ NAME β¨
std::jthread::joinable β yes queen, the name is the name, it's std::jthread::joinable π
πΈ Synopsis πΈ
[[nodiscard]] bool joinable() const noexcept; (since C++20)
so what does it do? it checks if the std::jthread object identifies an active thread of execution. that's the whole assignment, and it ate. π
specifically (and this is the part u need for the exam): it returns true if get_id() != std::jthread::id(). which means a default constructed jthread is not joinable. obvious but like, important. π
and here's the sneaky lil detail everyone forgets: a thread that has finished executing code, but has not yet been joined, is still considered an active thread of execution and is therefore joinable. yes fr fr. done β gone. π
π Parameters π
(none) β she takes nothing, she needs nothing, main character energy β¨
β Return value β
true if the jthread object identifies an active thread of execution, false otherwise. simple as that, periodt. π
π Example π
// Run this code
#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';
}
and the output, bestie, she speaks for herself: π«Ά
before starting, joinable: false
after starting, joinable: true
after joining, joinable: false
after detaching, joinable: false
look at that journey. default β not joinable. started β joinable. joined β not joinable. detached β not joinable. character development fr. πΈ
π₯ References π₯
- C++23 standard (ISO/IEC 14882:2023):
33.4.4.3 Members [thread.jthread.mem]
- C++20 standard (ISO/IEC 14882:2020):
32.4.3.2 Members [thread.jthread.mem]
(the receipts. always cite ur sources, iykyk π)
π See also π
get_idβ returns the id of the thread (public member function)joinβ waits for the thread to finish its execution (public member function)detachβ permits the thread to execute independently from the thread handle (public member function)
lowkey a reminder:joinable()is[[nodiscard]]andnoexcept, so don't ignore her return value and don't expect her to throw. she's chill like that. π§
okay that's the whole page, 10/10, no notes. this masterpiece is courtesy of http://cppreference.com, version 2024.06.10, dated std::jthread::joinable(3) β mwah, slay, bye bestie πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms