std::thread(3cxx)

bestie, you’ve landed on std::thread(3cxx) from the libstdc++ manual. It’s giving C++ thread class main character energy, and we’re gonna get through every single line together. πŸ«ΆπŸ’–

πŸ’– NAME πŸ’–

std::thread β€” that’s the name, bestie. Short, iconic, no notes. πŸ’…

✨ SYNOPSIS ✨

#include <thread>

πŸŽ€ Classes πŸŽ€

  • class id

πŸ¦‹ Public Types πŸ¦‹

  • using native_handle_type = __gthread_t

🌸 Public Member Functions 🌸

Okay so the templated constructor is highkey the star of the show, and it’s exactly this:

template<typename _Callable , typename... _Args, typename  =
           _Require<__not_same<_Callable>>> thread (_Callable &&__f, _Args
           &&... __args)

The rest of the public member function roster, fr fr:

  • thread (const thread &)=delete
  • thread (thread &&__t) noexcept
  • void detach ()
  • id get_id () const noexcept
  • void join ()
  • bool joinable () const noexcept
  • native_handle_type native_handle ()
  • thread & operator= (const thread &)=delete
  • thread & operator= (thread &&__t) noexcept
  • void swap (thread &__t) noexcept

🌈 Static Public Member Functions 🌈

  • static unsigned int hardware_concurrency () noexcept
Note that these are not member symbols. 🫢
  • void swap (thread &__x, thread &__y) noexcept

πŸ’« Detailed Description πŸ’«

Okay so a std::thread represents a new thread of execution. That’s the whole vibe. 🧡

The default constructor creates an object that does not own a thread. The thread(F&&, Args&&...) constructor invokes a callable in a new thread, and owns that new thread. A std::thread that owns a thread is joinable. Joining a thread waits for it to finish executing, which happens when the callable running in that thread returns. ⏳

A std::thread cannot be copied, but can be moved. Moving a joinable object transfers ownership of its thread to another object. πŸ” Not me being obsessed with ownership transfer, but here we are.

A joinable std::thread must be explicitly joined (or detached) before it is destroyed or assigned to. Attempting to destroy a joinable thread will terminate the whole process. Not a vibe check you want to fail, bestie β€” that’s a whole-process termination. πŸ’€

🌟 Since 🌟

C++11


🌸 Member Function Documentation 🌸

🫢 native_handle_type std::thread::native_handle () [inline] 🫢

πŸŽ€ Precondition πŸŽ€

thread is joinable β€” that’s the condition, no cap.


πŸ‘‘ Author πŸ‘‘

Generated automatically by Doxygen for libstdc++ from the source code.

Yup, the robots generated it β€” we’re just giving it the group-chat remix. πŸ€–πŸ’–


footer check, bestie: this is from libstdc++, and the manual page is std::thread(3cxx). The original footer doesn’t list a date, so I’m not inventing one β€” periodt. πŸ’–βœ¨

πŸ“¦ where this page comes from