std::jthread::joinable(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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]] and noexcept, 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