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
12// 2) Use std::jthread when the library actually ships it
13#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
14using jthread_alias = std::jthread;
15
16#else
17// Fallback: RAII "join-on-destruction" wrapper.
18// Use composition (safer than inheriting from std::thread).
20 std::thread t_;
21public:
22 jthread_alias() noexcept = default;
23
24 template<class F, class... Args>
25 explicit jthread_alias(F&& f, Args&&... args)
26 : t_(std::forward<F>(f), std::forward<Args>(args)...) {}
27
28 jthread_alias(jthread_alias&&) noexcept = default;
29 jthread_alias& operator=(jthread_alias&&) noexcept = default;
30
31 jthread_alias(const jthread_alias&) = delete;
32 jthread_alias& operator=(const jthread_alias&) = delete;
33
34 ~jthread_alias() { if (t_.joinable()) t_.join(); }
35
36 // minimal forwarding API
37 bool joinable() const noexcept { return t_.joinable(); }
38 void join() { t_.join(); }
39 void detach() { t_.detach(); }
40 std::thread::id get_id() const noexcept { return t_.get_id(); }
41 auto native_handle() { return t_.native_handle(); }
42 void swap(jthread_alias& other) noexcept { t_.swap(other.t_); }
43};
44#endif
void detach()
Definition gthreads.h:39
void join()
Definition gthreads.h:38
jthread_alias(jthread_alias &&) noexcept=default
jthread_alias() noexcept=default
std::thread::id get_id() const noexcept
Definition gthreads.h:40
bool joinable() const noexcept
Definition gthreads.h:37
auto native_handle()
Definition gthreads.h:41
void swap(jthread_alias &other) noexcept
Definition gthreads.h:42