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
5#pragma once
6
7#include <memory>
8#include <atomic>
9
10#ifdef USE_STD_JTHREAD
11#include <thread>
12#include <stop_token>
13#else
14#include <thread>
15#endif
16
24 class thread_impl {
25 public:
26 #ifdef USE_STD_JTHREAD
27 using thread_type = std::jthread;
28 using stop_token_type = std::stop_token;
29 using stop_source_type = std::stop_source;
30 #else
31 using thread_type = std::thread;
32 using stop_token_type = std::atomic<bool>;
33 using stop_source_type = std::atomic<bool>;
34 #endif
35
36 thread_impl() = default;
37 ~thread_impl() = default;
38
39 thread_impl(const thread_impl&) = delete;
43
47 template<typename F>
48 void start_thread(F&& func) {
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 }
61
65 void request_stop() {
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 }
76
80 [[nodiscard]] bool stop_requested() const {
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 }
87
91 void join() {
92 if (thread_ && thread_->joinable()) {
93 thread_->join();
94 }
95 }
96
100 void detach() {
101 if (thread_ && thread_->joinable()) {
102 thread_->detach();
103 }
104 }
105
109 [[nodiscard]] bool joinable() const {
110 return thread_ && thread_->joinable();
111 }
112
113 private:
114 std::unique_ptr<thread_type> thread_;
115
116 #ifdef USE_STD_JTHREAD
117 std::unique_ptr<stop_source_type> stop_source_;
118 #else
119 std::unique_ptr<stop_token_type> stop_requested_;
120 #endif
121 };
122}
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:91
thread_impl(thread_impl &&)=default
bool stop_requested() const
Check if stop has been requested.
Definition thread_impl.h:80
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:48
void detach()
Detach the thread.
void request_stop()
Request thread to stop.
Definition thread_impl.h:65