29#ifndef _GLIBCXX_THREAD
30#define _GLIBCXX_THREAD 1
33#pragma GCC system_header
38#if __cplusplus < 201103L
42#if __cplusplus > 201703L
50#define __glibcxx_want_jthread
51#define __glibcxx_want_formatters
54#if __cpp_lib_formatters
58namespace std _GLIBCXX_VISIBILITY(default)
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
75#if __cpp_lib_three_way_comparison
76 inline strong_ordering
77 operator<=>(thread::id __x, thread::id __y)
noexcept
78 {
return __x._M_thread <=> __y._M_thread; }
81 operator!=(thread::id __x, thread::id __y)
noexcept
82 {
return !(__x == __y); }
85 operator<(thread::id __x, thread::id __y)
noexcept
89 return __x._M_thread < __y._M_thread;
93 operator<=(thread::id __x, thread::id __y)
noexcept
94 {
return !(__y < __x); }
97 operator>(thread::id __x, thread::id __y)
noexcept
101 operator>=(thread::id __x, thread::id __y)
noexcept
102 {
return !(__x < __y); }
105 template<
class _CharT,
class _Traits>
106 inline basic_ostream<_CharT, _Traits>&
107 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
111 = __conditional_t<is_pointer<thread::native_handle_type>::value,
113 thread::native_handle_type>;
115 if (__id == thread::id())
116 return __out <<
"thread::id of a non-executing thread";
118 return __out << static_cast<__output_type>(__id._M_thread);
122#ifdef __cpp_lib_jthread
125#ifndef __STRICT_ANSI__
126 template<
typename _Callable,
typename... _Args>
127 constexpr bool __pmf_expects_stop_token =
false;
129 template<
typename _Callable,
typename _Obj,
typename... _Args>
130 constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
131 = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
132 is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
153 using id = thread::id;
154 using native_handle_type = thread::native_handle_type;
157 : _M_stop_source{nostopstate}
160 template<
typename _Callable,
typename... _Args,
161 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
164 jthread(_Callable&& __f, _Args&&... __args)
165 : _M_thread{_S_create(_M_stop_source,
std::
forward<_Callable>(__f),
169 jthread(
const jthread&) =
delete;
170 jthread(jthread&&) noexcept = default;
182 operator=(
const jthread&) =
delete;
185 operator=(jthread&& __other)
noexcept
187 std::jthread(
std::move(__other)).swap(*
this);
192 swap(jthread& __other)
noexcept
194 std::swap(_M_stop_source, __other._M_stop_source);
195 std::swap(_M_thread, __other._M_thread);
199 joinable() const noexcept
201 return _M_thread.joinable();
219 return _M_thread.get_id();
222 [[nodiscard]] native_handle_type
225 return _M_thread.native_handle();
228 [[nodiscard]]
static unsigned
229 hardware_concurrency() noexcept
231 return thread::hardware_concurrency();
234 [[nodiscard]] stop_source
235 get_stop_source() noexcept
237 return _M_stop_source;
240 [[nodiscard]] stop_token
241 get_stop_token() const noexcept
243 return _M_stop_source.get_token();
246 bool request_stop() noexcept
248 return _M_stop_source.request_stop();
251 friend void swap(jthread& __lhs, jthread& __rhs)
noexcept
257 template<
typename _Callable,
typename... _Args>
259 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
261#ifndef __STRICT_ANSI__
262 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
263 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
266 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
268 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
269 std::forward<_Args>(__args)...};
272 static_assert(is_invocable_v<decay_t<_Callable>,
274 "std::jthread arguments must be invocable after"
275 " conversion to rvalues");
276 return thread{std::forward<_Callable>(__f),
277 std::forward<_Args>(__args)...};
281#ifndef __STRICT_ANSI__
282 template<
typename _Callable,
typename _Obj,
typename... _Args>
284 _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
287 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
288 std::forward<_Args>(__args)...};
292 stop_source _M_stop_source;
297#ifdef __cpp_lib_formatters
298 template<
typename _CharT>
299 requires is_pointer_v<thread::native_handle_type>
300 || is_integral_v<thread::native_handle_type>
301 class formatter<thread::id, _CharT>
304 constexpr typename basic_format_parse_context<_CharT>::iterator
305 parse(basic_format_parse_context<_CharT>& __pc)
307 __format::_Spec<_CharT> __spec{};
308 const auto __last = __pc.end();
309 auto __first = __pc.begin();
311 auto __finalize = [
this, &__spec] {
315 auto __finished = [&] {
316 if (__first == __last || *__first ==
'}')
327 __first = __spec._M_parse_fill_and_align(__first, __last);
331 __first = __spec._M_parse_width(__first, __last, __pc);
335 __throw_format_error(
"format error: invalid format-spec for "
339 template<
typename _Out>
340 typename basic_format_context<_Out, _CharT>::iterator
341 format(thread::id __id, basic_format_context<_Out, _CharT>& __fc)
const
343 basic_string_view<_CharT> __sv;
344 if constexpr (is_same_v<_CharT, char>)
345 __sv =
"{}thread::id of a non-executing thread";
347 __sv = L
"{}thread::id of a non-executing thread";
348 basic_string<_CharT> __str;
349 if (__id == thread::id())
350 __sv.remove_prefix(2);
353 using _FmtStr = __format::_Runtime_format_string<_CharT>;
356 = __conditional_t<is_pointer_v<thread::native_handle_type>,
358 thread::native_handle_type>;
359 auto __o =
static_cast<__output_type
>(__id._M_thread);
360 __sv = __str = std::format(_FmtStr(__sv.substr(0, 2)), __o);
362 return __format::__write_padded_as_spec(__sv, __sv.size(),
364 __format::_Align_right);
368 __format::_Spec<_CharT> _M_spec;
374_GLIBCXX_END_NAMESPACE_VERSION
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
thread::id get_id() noexcept
The unique identifier of the current thread.