29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
38 #if __cplusplus > 201703L
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 #if __cpp_lib_three_way_comparison
61 inline strong_ordering
62 operator<=>(thread::id __x, thread::id __y) noexcept
63 {
return __x._M_thread <=> __y._M_thread; }
66 operator!=(thread::id __x, thread::id __y) noexcept
67 {
return !(__x == __y); }
70 operator<(thread::id __x, thread::id __y) noexcept
74 return __x._M_thread < __y._M_thread;
78 operator<=(thread::id __x, thread::id __y) noexcept
79 {
return !(__y < __x); }
82 operator>(thread::id __x, thread::id __y) noexcept
86 operator>=(thread::id __x, thread::id __y) noexcept
87 {
return !(__x < __y); }
88 #endif // __cpp_lib_three_way_comparison
90 template<
class _CharT,
class _Traits>
91 inline basic_ostream<_CharT, _Traits>&
92 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
94 if (__id == thread::id())
95 return __out <<
"thread::id of a non-executing thread";
97 return __out << __id._M_thread;
100 #ifdef __cpp_lib_jthread
102 #ifndef __STRICT_ANSI__
103 template<
typename _Callable,
typename... _Args>
104 constexpr
bool __pmf_expects_stop_token =
false;
106 template<
typename _Callable,
typename _Obj,
typename... _Args>
107 constexpr
bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
108 = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
109 is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
117 using native_handle_type = thread::native_handle_type;
120 : _M_stop_source{nostopstate}
123 template<
typename _Callable,
typename... _Args,
127 jthread(_Callable&& __f, _Args&&... __args)
128 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
129 std::forward<_Args>(__args)...)}
132 jthread(
const jthread&) =
delete;
133 jthread(jthread&&) noexcept =
default;
145 operator=(
const jthread&) =
delete;
148 operator=(jthread&& __other) noexcept
155 swap(jthread& __other) noexcept
157 std::swap(_M_stop_source, __other._M_stop_source);
158 std::swap(_M_thread, __other._M_thread);
162 joinable()
const noexcept
164 return _M_thread.joinable();
180 get_id()
const noexcept
182 return _M_thread.get_id();
185 [[nodiscard]] native_handle_type
191 [[nodiscard]]
static unsigned
192 hardware_concurrency() noexcept
194 return thread::hardware_concurrency();
198 get_stop_source() noexcept
200 return _M_stop_source;
204 get_stop_token()
const noexcept
206 return _M_stop_source.get_token();
209 bool request_stop() noexcept
211 return _M_stop_source.request_stop();
214 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
220 template<
typename _Callable,
typename... _Args>
222 _S_create(
stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
224 #ifndef __STRICT_ANSI__
225 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
226 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
231 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
232 std::forward<_Args>(__args)...};
237 "std::jthread arguments must be invocable after"
238 " conversion to rvalues");
239 return thread{std::forward<_Callable>(__f),
240 std::forward<_Args>(__args)...};
244 #ifndef __STRICT_ANSI__
245 template<
typename _Callable,
typename _Obj,
typename... _Args>
247 _S_create_pmf(
stop_source& __ssrc, _Callable __f, _Obj&& __obj,
250 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
251 std::forward<_Args>(__args)...};
258 #endif // __cpp_lib_jthread
262 _GLIBCXX_END_NAMESPACE_VERSION
265 #endif // _GLIBCXX_THREAD
native_handle_type native_handle()
Allow testing whether a stop request has been made on a stop_source.
A thread that can be requested to stop and automatically joined.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
A type that allows a stop request to be made.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
typename decay< _Tp >::type decay_t
Alias template for decay.