Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_impl.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include <memory>
15#include <atomic>
16
17#ifdef USE_STD_JTHREAD
18#include <thread>
19#include <stop_token>
20#else
21#include <thread>
22#endif
23
24namespace kcenon::thread::detail {
32 public:
33 #ifdef USE_STD_JTHREAD
34 using thread_type = std::jthread;
35 using stop_token_type = std::stop_token;
36 using stop_source_type = std::stop_source;
37 #else
38 using thread_type = std::thread;
39 using stop_token_type = std::atomic<bool>;
40 using stop_source_type = std::atomic<bool>;
41 #endif
42
43 thread_impl() = default;
44 ~thread_impl() = default;
45
46 thread_impl(const thread_impl&) = delete;
50
54 template<typename F>
55 void start_thread(F&& func) {
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 }
68
72 void request_stop() {
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 }
83
87 [[nodiscard]] bool stop_requested() const {
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 }
94
98 void join() {
99 if (thread_ && thread_->joinable()) {
100 thread_->join();
101 }
102 }
103
107 void detach() {
108 if (thread_ && thread_->joinable()) {
109 thread_->detach();
110 }
111 }
112
116 [[nodiscard]] bool joinable() const {
117 return thread_ && thread_->joinable();
118 }
119
120 private:
121 std::unique_ptr<thread_type> thread_;
122
123 #ifdef USE_STD_JTHREAD
124 std::unique_ptr<stop_source_type> stop_source_;
125 #else
126 std::unique_ptr<stop_token_type> stop_requested_;
127 #endif
128 };
129}
Thread implementation abstraction that handles differences between std::jthread and std::thread.
Definition thread_impl.h:31
void join()
Join the thread.
Definition thread_impl.h:98
thread_impl(thread_impl &&)=default
bool stop_requested() const
Check if stop has been requested.
Definition thread_impl.h:87
thread_impl(const thread_impl &)=delete
std::atomic< bool > stop_source_type
Definition thread_impl.h:33
thread_impl & operator=(thread_impl &&)=default
thread_impl & operator=(const thread_impl &)=delete
std::unique_ptr< thread_type > thread_
bool joinable() const
Check if thread is joinable.
std::unique_ptr< stop_token_type > stop_requested_
std::atomic< bool > stop_token_type
Definition thread_impl.h:32
void start_thread(F &&func)
Create and start a thread with the given function.
Definition thread_impl.h:55
void detach()
Detach the thread.
void request_stop()
Request thread to stop.
Definition thread_impl.h:72