guts
Loading...
Searching...
No Matches
jthread_alias Class Reference

Fallback RAII "join-on-destruction" thread wrapper. More...

#include <gthreads.h>

Public Member Functions

 jthread_alias () noexcept=default
 Construct an empty (non-joinable) wrapper.
 
template<class F , class... Args>
 jthread_alias (F &&f, Args &&... args)
 Start a new thread by forwarding arguments to the underlying std::thread.
 
 jthread_alias (jthread_alias &&) noexcept=default
 
jthread_aliasoperator= (jthread_alias &&) noexcept=default
 
 jthread_alias (const jthread_alias &)=delete
 
jthread_aliasoperator= (const jthread_alias &)=delete
 
 ~jthread_alias ()
 Join the underlying thread on destruction if still joinable.
 
bool joinable () const noexcept
 Check whether the underlying thread can be joined.
 
void join ()
 Join the underlying thread.
 
void detach ()
 Detach the underlying thread.
 
std::thread::id get_id () const noexcept
 Get the underlying thread id.
 
auto native_handle ()
 Access the native handle of the underlying thread.
 
void swap (jthread_alias &other) noexcept
 Swap the underlying thread with another wrapper.
 

Detailed Description

This class is used when std::jthread is not available in the standard library.

Ownership model:

  • The object owns the underlying std::thread instance.
  • If that thread is joinable at destruction, the destructor joins it.

Differences vs std::jthread:

  • No stop-token / cooperative cancellation support.
  • Only a minimal subset of the thread API is exposed.

Typical usage:

jthread_alias t([]{ do_work(); });
// ... later ...
// joins automatically on scope exit if still joinable
Fallback RAII "join-on-destruction" thread wrapper.
Definition gthreads.h:73
Warning
The destructor joins unconditionally when joinable. This is a safety feature, but it also means a long-running thread may delay scope exit. Ensure the thread function has clear termination conditions.

Definition at line 72 of file gthreads.h.

Constructor & Destructor Documentation

◆ jthread_alias() [1/4]

jthread_alias::jthread_alias ( )
defaultnoexcept

After default construction, joinable() returns false.

◆ jthread_alias() [2/4]

template<class F , class... Args>
jthread_alias::jthread_alias ( F && f,
Args &&... args )
inlineexplicit
Template Parameters
FCallable type.
ArgsArgument pack forwarded to f.
Parameters
fCallable to run in the new thread.
argsArguments passed to f.
Note
This constructor is explicit to avoid accidental implicit thread starts.

Definition at line 101 of file gthreads.h.

◆ jthread_alias() [3/4]

jthread_alias::jthread_alias ( jthread_alias && )
defaultnoexcept

◆ jthread_alias() [4/4]

jthread_alias::jthread_alias ( const jthread_alias & )
delete

◆ ~jthread_alias()

jthread_alias::~jthread_alias ( )
inline

This mirrors the "safe by default" behavior typically sought with std::jthread.

Warning
If the owned thread function can block indefinitely, destruction will also block.

Definition at line 118 of file gthreads.h.

Member Function Documentation

◆ detach()

void jthread_alias::detach ( )
inline

After detaching, the wrapper no longer represents a joinable thread, and the destructor will not join.

Definition at line 142 of file gthreads.h.

◆ get_id()

std::thread::id jthread_alias::get_id ( ) const
inlinenoexcept
Returns
The std::thread::id of the owned thread.

Definition at line 148 of file gthreads.h.

◆ join()

void jthread_alias::join ( )
inline

Preconditions match std::thread::join.

Warning
Calling this when not joinable has undefined behavior per std::thread rules (typically terminating the program).

Definition at line 134 of file gthreads.h.

◆ joinable()

bool jthread_alias::joinable ( ) const
inlinenoexcept
Returns
true if joinable, false otherwise.

Definition at line 124 of file gthreads.h.

◆ native_handle()

auto jthread_alias::native_handle ( )
inline
Returns
Native handle as returned by std::thread::native_handle.
Note
This is provided for integration with low-level platform APIs when needed.

Definition at line 156 of file gthreads.h.

◆ operator=() [1/2]

jthread_alias & jthread_alias::operator= ( const jthread_alias & )
delete

◆ operator=() [2/2]

jthread_alias & jthread_alias::operator= ( jthread_alias && )
defaultnoexcept

◆ swap()

void jthread_alias::swap ( jthread_alias & other)
inlinenoexcept
Parameters
otherWrapper to swap with.

After swapping, each wrapper owns the other's prior thread.

Definition at line 164 of file gthreads.h.


The documentation for this class was generated from the following file: