guts
Loading...
Searching...
No Matches
gthreads.h
Go to the documentation of this file.
1#pragma once
2
3// 1) Prefer to see feature macros if available
4#if defined(__has_include)
5# if __has_include(<version>)
6# include <version>
7# endif
8#endif
9
10#include <thread>
11
30// 2) Use std::jthread when the library actually ships it
31#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
32
43using jthread_alias = std::jthread;
44
45#else
46
73{
80 std::thread t_;
81
82public:
88 jthread_alias() noexcept = default;
89
100 template <class F, class... Args>
101 explicit jthread_alias(F&& f, Args&&... args)
102 : t_(std::forward<F>(f), std::forward<Args>(args)...) {
103 }
104
105 jthread_alias(jthread_alias&&) noexcept = default;
106 jthread_alias& operator=(jthread_alias&&) noexcept = default;
107
108 jthread_alias(const jthread_alias&) = delete;
109 jthread_alias& operator=(const jthread_alias&) = delete;
110
118 ~jthread_alias() { if (t_.joinable()) t_.join(); }
119
124 bool joinable() const noexcept { return t_.joinable(); }
125
134 void join() { t_.join(); }
135
142 void detach() { t_.detach(); }
143
148 std::thread::id get_id() const noexcept { return t_.get_id(); }
149
156 auto native_handle() { return t_.native_handle(); }
157
164 void swap(jthread_alias& other) noexcept { t_.swap(other.t_); }
165};
166#endif
Fallback RAII "join-on-destruction" thread wrapper.
Definition gthreads.h:73
void detach()
Detach the underlying thread.
Definition gthreads.h:142
void join()
Join the underlying thread.
Definition gthreads.h:134
jthread_alias(jthread_alias &&) noexcept=default
jthread_alias() noexcept=default
Construct an empty (non-joinable) wrapper.
std::thread::id get_id() const noexcept
Get the underlying thread id.
Definition gthreads.h:148
bool joinable() const noexcept
Check whether the underlying thread can be joined.
Definition gthreads.h:124
auto native_handle()
Access the native handle of the underlying thread.
Definition gthreads.h:156
void swap(jthread_alias &other) noexcept
Swap the underlying thread with another wrapper.
Definition gthreads.h:164