Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::detail::thread_impl Class Reference

Thread implementation abstraction that handles differences between std::jthread and std::thread. More...

#include <thread_impl.h>

Collaboration diagram for kcenon::thread::detail::thread_impl:
Collaboration graph

Public Types

using thread_type = std::thread
 
using stop_token_type = std::atomic<bool>
 
using stop_source_type = std::atomic<bool>
 

Public Member Functions

 thread_impl ()=default
 
 ~thread_impl ()=default
 
 thread_impl (const thread_impl &)=delete
 
thread_imploperator= (const thread_impl &)=delete
 
 thread_impl (thread_impl &&)=default
 
thread_imploperator= (thread_impl &&)=default
 
template<typename F >
void start_thread (F &&func)
 Create and start a thread with the given function.
 
void request_stop ()
 Request thread to stop.
 
bool stop_requested () const
 Check if stop has been requested.
 
void join ()
 Join the thread.
 
void detach ()
 Detach the thread.
 
bool joinable () const
 Check if thread is joinable.
 
 thread_impl ()=default
 
 ~thread_impl ()=default
 
 thread_impl (const thread_impl &)=delete
 
thread_imploperator= (const thread_impl &)=delete
 
 thread_impl (thread_impl &&)=default
 
thread_imploperator= (thread_impl &&)=default
 
template<typename F >
void start_thread (F &&func)
 Create and start a thread with the given function.
 
void request_stop ()
 Request thread to stop.
 
bool stop_requested () const
 Check if stop has been requested.
 
void join ()
 Join the thread.
 
void detach ()
 Detach the thread.
 
bool joinable () const
 Check if thread is joinable.
 

Private Attributes

std::unique_ptr< thread_typethread_
 
std::unique_ptr< stop_token_typestop_requested_
 

Detailed Description

Thread implementation abstraction that handles differences between std::jthread and std::thread.

This class encapsulates the conditional compilation complexity for thread management, providing a unified interface regardless of C++20 jthread availability.

Definition at line 31 of file thread_impl.h.

Member Typedef Documentation

◆ stop_source_type

typedef std::atomic< bool > kcenon::thread::detail::thread_impl::stop_source_type = std::atomic<bool>

Definition at line 33 of file thread_impl.h.

◆ stop_token_type

typedef std::atomic< bool > kcenon::thread::detail::thread_impl::stop_token_type = std::atomic<bool>

Definition at line 32 of file thread_impl.h.

◆ thread_type

typedef std::thread kcenon::thread::detail::thread_impl::thread_type = std::thread

Definition at line 31 of file thread_impl.h.

Constructor & Destructor Documentation

◆ thread_impl() [1/6]

kcenon::thread::detail::thread_impl::thread_impl ( )
default

◆ ~thread_impl() [1/2]

kcenon::thread::detail::thread_impl::~thread_impl ( )
default

◆ thread_impl() [2/6]

kcenon::thread::detail::thread_impl::thread_impl ( const thread_impl & )
delete

◆ thread_impl() [3/6]

kcenon::thread::detail::thread_impl::thread_impl ( thread_impl && )
default

◆ thread_impl() [4/6]

kcenon::thread::detail::thread_impl::thread_impl ( )
default

◆ ~thread_impl() [2/2]

kcenon::thread::detail::thread_impl::~thread_impl ( )
default

◆ thread_impl() [5/6]

kcenon::thread::detail::thread_impl::thread_impl ( const thread_impl & )
delete

◆ thread_impl() [6/6]

kcenon::thread::detail::thread_impl::thread_impl ( thread_impl && )
default

Member Function Documentation

◆ detach() [1/2]

void kcenon::thread::detail::thread_impl::detach ( )
inline

Detach the thread.

Definition at line 100 of file thread_impl.h.

100 {
101 if (thread_ && thread_->joinable()) {
102 thread_->detach();
103 }
104 }
std::unique_ptr< thread_type > thread_

References thread_.

◆ detach() [2/2]

void kcenon::thread::detail::thread_impl::detach ( )
inline

Detach the thread.

Definition at line 107 of file thread_impl.h.

107 {
108 if (thread_ && thread_->joinable()) {
109 thread_->detach();
110 }
111 }

References thread_.

◆ join() [1/2]

void kcenon::thread::detail::thread_impl::join ( )
inline

Join the thread.

Definition at line 91 of file thread_impl.h.

91 {
92 if (thread_ && thread_->joinable()) {
93 thread_->join();
94 }
95 }

References thread_.

◆ join() [2/2]

void kcenon::thread::detail::thread_impl::join ( )
inline

Join the thread.

Definition at line 98 of file thread_impl.h.

98 {
99 if (thread_ && thread_->joinable()) {
100 thread_->join();
101 }
102 }

References thread_.

◆ joinable() [1/2]

bool kcenon::thread::detail::thread_impl::joinable ( ) const
inlinenodiscard

Check if thread is joinable.

Definition at line 109 of file thread_impl.h.

109 {
110 return thread_ && thread_->joinable();
111 }

References thread_.

◆ joinable() [2/2]

bool kcenon::thread::detail::thread_impl::joinable ( ) const
inlinenodiscard

Check if thread is joinable.

Definition at line 116 of file thread_impl.h.

116 {
117 return thread_ && thread_->joinable();
118 }

References thread_.

◆ operator=() [1/4]

thread_impl & kcenon::thread::detail::thread_impl::operator= ( const thread_impl & )
delete

◆ operator=() [2/4]

thread_impl & kcenon::thread::detail::thread_impl::operator= ( const thread_impl & )
delete

◆ operator=() [3/4]

thread_impl & kcenon::thread::detail::thread_impl::operator= ( thread_impl && )
default

◆ operator=() [4/4]

thread_impl & kcenon::thread::detail::thread_impl::operator= ( thread_impl && )
default

◆ request_stop() [1/2]

void kcenon::thread::detail::thread_impl::request_stop ( )
inline

Request thread to stop.

Definition at line 65 of file thread_impl.h.

65 {
66 #ifdef USE_STD_JTHREAD
67 if (stop_source_) {
68 stop_source_->request_stop();
69 }
70 #else
71 if (stop_requested_) {
72 stop_requested_->store(true);
73 }
74 #endif
75 }
std::unique_ptr< stop_token_type > stop_requested_

References stop_requested_.

◆ request_stop() [2/2]

void kcenon::thread::detail::thread_impl::request_stop ( )
inline

Request thread to stop.

Definition at line 72 of file thread_impl.h.

72 {
73 #ifdef USE_STD_JTHREAD
74 if (stop_source_) {
75 stop_source_->request_stop();
76 }
77 #else
78 if (stop_requested_) {
79 stop_requested_->store(true);
80 }
81 #endif
82 }

References stop_requested_.

◆ start_thread() [1/2]

template<typename F >
void kcenon::thread::detail::thread_impl::start_thread ( F && func)
inline

Create and start a thread with the given function.

Definition at line 48 of file thread_impl.h.

48 {
49 #ifdef USE_STD_JTHREAD
50 stop_source_ = std::make_unique<stop_source_type>();
51 thread_ = std::make_unique<thread_type>([func = std::forward<F>(func), this](std::stop_token st) {
52 func(st);
53 });
54 #else
55 stop_requested_ = std::make_unique<stop_token_type>(false);
56 thread_ = std::make_unique<thread_type>([func = std::forward<F>(func), this]() {
57 func(*stop_requested_);
58 });
59 #endif
60 }

References stop_requested_, and thread_.

◆ start_thread() [2/2]

template<typename F >
void kcenon::thread::detail::thread_impl::start_thread ( F && func)
inline

Create and start a thread with the given function.

Definition at line 55 of file thread_impl.h.

55 {
56 #ifdef USE_STD_JTHREAD
57 stop_source_ = std::make_unique<stop_source_type>();
58 thread_ = std::make_unique<thread_type>([func = std::forward<F>(func), this](std::stop_token st) {
59 func(st);
60 });
61 #else
62 stop_requested_ = std::make_unique<stop_token_type>(false);
63 thread_ = std::make_unique<thread_type>([func = std::forward<F>(func), this]() {
64 func(*stop_requested_);
65 });
66 #endif
67 }

References stop_requested_, and thread_.

◆ stop_requested() [1/2]

bool kcenon::thread::detail::thread_impl::stop_requested ( ) const
inlinenodiscard

Check if stop has been requested.

Definition at line 80 of file thread_impl.h.

80 {
81 #ifdef USE_STD_JTHREAD
82 return stop_source_ && stop_source_->stop_requested();
83 #else
84 return stop_requested_ && stop_requested_->load();
85 #endif
86 }

References stop_requested_.

◆ stop_requested() [2/2]

bool kcenon::thread::detail::thread_impl::stop_requested ( ) const
inlinenodiscard

Check if stop has been requested.

Definition at line 87 of file thread_impl.h.

87 {
88 #ifdef USE_STD_JTHREAD
89 return stop_source_ && stop_source_->stop_requested();
90 #else
91 return stop_requested_ && stop_requested_->load();
92 #endif
93 }

References stop_requested_.

Member Data Documentation

◆ stop_requested_

std::unique_ptr< stop_token_type > kcenon::thread::detail::thread_impl::stop_requested_
private

Definition at line 119 of file thread_impl.h.

Referenced by request_stop(), start_thread(), and stop_requested().

◆ thread_

std::unique_ptr< thread_type > kcenon::thread::detail::thread_impl::thread_
private

Definition at line 114 of file thread_impl.h.

Referenced by detach(), join(), joinable(), and start_thread().


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