Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
ThreadWrapper Struct Reference

#include "lib/thread.hpp"

Description

wraps the C++ thread handle and provides some implementation details, which are then combined by the policy template

Definition at line 165 of file thread.hpp.

Public Attributes

const string threadID_
 
std::thread threadImpl_
 

Static Public Member Functions

static string decorate_with_global_count (string const &)
 Helper to create a suffix to the thread-ID with running count.
 

Public Member Functions

bool isLive () const
 
 ThreadWrapper ()
 
 ThreadWrapper (string const &threadID)
 
template<class... INVO>
void launchThread (tuple< INVO... > &&invocation)
 
bool invokedWithinThread () const
 detect if the currently executing code runs within this thread
 
void markThreadStart ()
 
void markThreadEnd ()
 
void setThreadName ()
 
void waitGracePeriod () noexcept
 
void handle_begin_thread ()
 called immediately at start of thread
 
void handle_after_thread ()
 called immediately before end of thread
 
void handle_loose_thread ()
 called when destroying wrapper on still running thread
 
void detach_thread_from_wrapper ()
 allow to detach explicitly — independent from thread-function's state.
 

Additional Inherited Members

- Protected Member Functions inherited from MoveOnly
 ~MoveOnly ()=default
 
 MoveOnly ()=default
 
 MoveOnly (MoveOnly &&)=default
 
 MoveOnly (MoveOnly const &)=delete
 
MoveOnlyoperator= (MoveOnly &&)=delete
 
MoveOnlyoperator= (MoveOnly const &)=delete
 

Constructor & Destructor Documentation

◆ ThreadWrapper() [1/2]

ThreadWrapper ( )
inline

derived classes may create an inactive thread

Definition at line 175 of file thread.hpp.

◆ ThreadWrapper() [2/2]

ThreadWrapper ( string const &  threadID)
inline

Definition at line 180 of file thread.hpp.

Member Function Documentation

◆ isLive()

bool isLive ( ) const
inline

Definition at line 171 of file thread.hpp.

References ThreadWrapper::threadImpl_.

Referenced by ThreadWrapper::detach_thread_from_wrapper(), and ThreadWrapper::launchThread().

+ Here is the caller graph for this function:

◆ launchThread()

template<class... INVO>
void launchThread ( tuple< INVO... > &&  invocation)
inline

actually launch the new thread. Deliberately the threadImpl_ is created empty, to allow for complete initialisation of all the combined policy classes stacked on top

Warning
Start of the new thread syncs-with the return from std::thread ctor. Thus – in theory – initialising members of derived classes after constructing a non-empty std::thread object would be undefined behaviour. In practice however, this is more of a „theoretical“ problem, since the OS scheduler has a considerable latency, so that the code within the new thread typically starts executing with an delay of at least 100µs
Remarks
non the less, the thread-wrapper framework circumvents this possible undefined behaviour, by first creating the threadImpl_ empty, and only later move-assigning the std::thread.
Parameters
invocationa tuple holding some invokable and possible arguments, together forming the threadFunction to be executed in the new thread.

Definition at line 201 of file thread.hpp.

References ThreadWrapper::isLive(), and ThreadWrapper::threadImpl_.

+ Here is the call graph for this function:

◆ invokedWithinThread()

bool invokedWithinThread ( ) const

detect if the currently executing code runs within this thread

Note
implies get_id() != std::thread::id{} ==> it is running

Definition at line 65 of file thread.cpp.

References ThreadWrapper::threadImpl_.

◆ markThreadStart()

void markThreadStart ( )

Definition at line 72 of file thread.cpp.

References ThreadWrapper::setThreadName(), and ThreadWrapper::threadID_.

+ Here is the call graph for this function:

◆ markThreadEnd()

void markThreadEnd ( )

Definition at line 80 of file thread.cpp.

References ThreadWrapper::threadID_.

◆ setThreadName()

void setThreadName ( )

Definition at line 87 of file thread.cpp.

References ThreadWrapper::threadID_, and ThreadWrapper::threadImpl_.

Referenced by ThreadWrapper::markThreadStart().

+ Here is the caller graph for this function:

◆ waitGracePeriod()

void waitGracePeriod ( )
noexcept

◆ handle_begin_thread()

void handle_begin_thread ( )
inline

called immediately at start of thread

Definition at line 217 of file thread.hpp.

◆ handle_after_thread()

void handle_after_thread ( )
inline

called immediately before end of thread

Definition at line 218 of file thread.hpp.

◆ handle_loose_thread()

void handle_loose_thread ( )
inline

called when destroying wrapper on still running thread

Definition at line 219 of file thread.hpp.

◆ decorate_with_global_count()

string decorate_with_global_count ( string const &  rawID)
static

Helper to create a suffix to the thread-ID with running count.

Definition at line 56 of file thread.cpp.

◆ detach_thread_from_wrapper()

void detach_thread_from_wrapper ( )
inline

allow to detach explicitly — independent from thread-function's state.

Warning
this function is borderline dangerous; it might be acceptable in a situation where the thread totally manages itself and the thread object is maintained in a unique_ptr. You must ensure that the thread function does not touch anything in the wrapper object after that point and only uses storage within its own scope.

Definition at line 231 of file thread.hpp.

References ThreadWrapper::isLive(), and ThreadWrapper::threadImpl_.

Referenced by SchedulerCommutator_test::blockGroomingToken(), ThreadWrapperLifecycle_test::demonstrateExplicitThreadLifecycle(), and lib::launchDetached().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ threadID_

◆ threadImpl_

+ Inheritance diagram for ThreadWrapper:
+ Collaboration diagram for ThreadWrapper:

The documentation for this struct was generated from the following files: