29#ifndef _GLIBCXX_THREAD
30#define _GLIBCXX_THREAD 1
32#pragma GCC system_header
36#if __cplusplus < 201103L
40#if __cplusplus > 201703L
48#define __glibcxx_want_jthread
49#define __glibcxx_want_formatters
52#if __cpp_lib_formatters
56namespace std _GLIBCXX_VISIBILITY(default)
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
73#if __cpp_lib_three_way_comparison
74 inline strong_ordering
75 operator<=>(thread::id __x, thread::id __y)
noexcept
76 {
return __x._M_thread <=> __y._M_thread; }
79 operator!=(thread::id __x, thread::id __y)
noexcept
80 {
return !(__x == __y); }
83 operator<(thread::id __x, thread::id __y)
noexcept
87 return __x._M_thread < __y._M_thread;
91 operator<=(thread::id __x, thread::id __y)
noexcept
92 {
return !(__y < __x); }
95 operator>(thread::id __x, thread::id __y)
noexcept
99 operator>=(thread::id __x, thread::id __y)
noexcept
100 {
return !(__x < __y); }
103 template<
class _CharT,
class _Traits>
104 inline basic_ostream<_CharT, _Traits>&
105 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
109 = __conditional_t<is_pointer<thread::native_handle_type>::value,
111 thread::native_handle_type>;
113 if (__id == thread::id())
114 return __out <<
"thread::id of a non-executing thread";
116 return __out << static_cast<__output_type>(__id._M_thread);
120#ifdef __cpp_lib_jthread
123#ifndef __STRICT_ANSI__
124 template<
typename _Callable,
typename... _Args>
125 constexpr bool __pmf_expects_stop_token =
false;
127 template<
typename _Callable,
typename _Obj,
typename... _Args>
128 constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
129 = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
130 is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
151 using id = thread::id;
152 using native_handle_type = thread::native_handle_type;
155 : _M_stop_source{nostopstate}
158 template<
typename _Callable,
typename... _Args,
159 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
162 jthread(_Callable&& __f, _Args&&... __args)
163 : _M_thread{_S_create(_M_stop_source,
std::
forward<_Callable>(__f),
167 jthread(
const jthread&) =
delete;
168 jthread(jthread&&) noexcept = default;
180 operator=(
const jthread&) =
delete;
183 operator=(jthread&& __other)
noexcept
185 std::jthread(
std::move(__other)).swap(*
this);
190 swap(jthread& __other)
noexcept
192 std::swap(_M_stop_source, __other._M_stop_source);
193 std::swap(_M_thread, __other._M_thread);
197 joinable() const noexcept
199 return _M_thread.joinable();
217 return _M_thread.get_id();
220 [[nodiscard]] native_handle_type
223 return _M_thread.native_handle();
226 [[nodiscard]]
static unsigned
227 hardware_concurrency() noexcept
229 return thread::hardware_concurrency();
232 [[nodiscard]] stop_source
233 get_stop_source() noexcept
235 return _M_stop_source;
238 [[nodiscard]] stop_token
239 get_stop_token() const noexcept
241 return _M_stop_source.get_token();
244 bool request_stop() noexcept
246 return _M_stop_source.request_stop();
249 friend void swap(jthread& __lhs, jthread& __rhs)
noexcept
255 template<
typename _Callable,
typename... _Args>
257 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
259#ifndef __STRICT_ANSI__
260 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
261 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
264 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
266 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
267 std::forward<_Args>(__args)...};
270 static_assert(is_invocable_v<decay_t<_Callable>,
272 "std::jthread arguments must be invocable after"
273 " conversion to rvalues");
274 return thread{std::forward<_Callable>(__f),
275 std::forward<_Args>(__args)...};
279#ifndef __STRICT_ANSI__
280 template<
typename _Callable,
typename _Obj,
typename... _Args>
282 _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
285 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
286 std::forward<_Args>(__args)...};
290 stop_source _M_stop_source;
295#ifdef __cpp_lib_formatters
296 template<
typename _CharT>
297 requires is_pointer_v<thread::native_handle_type>
298 || is_integral_v<thread::native_handle_type>
299 class formatter<thread::id, _CharT>
302 constexpr typename basic_format_parse_context<_CharT>::iterator
303 parse(basic_format_parse_context<_CharT>& __pc)
305 __format::_Spec<_CharT> __spec{};
306 const auto __last = __pc.end();
307 auto __first = __pc.begin();
309 auto __finalize = [
this, &__spec] {
313 auto __finished = [&] {
314 if (__first == __last || *__first ==
'}')
325 __first = __spec._M_parse_fill_and_align(__first, __last);
329 __first = __spec._M_parse_width(__first, __last, __pc);
333 __throw_format_error(
"format error: invalid format-spec for "
337 template<
typename _Out>
338 typename basic_format_context<_Out, _CharT>::iterator
339 format(thread::id __id, basic_format_context<_Out, _CharT>& __fc)
const
341 basic_string_view<_CharT> __sv;
342 if constexpr (is_same_v<_CharT, char>)
343 __sv =
"{}thread::id of a non-executing thread";
345 __sv = L
"{}thread::id of a non-executing thread";
346 basic_string<_CharT> __str;
347 if (__id == thread::id())
348 __sv.remove_prefix(2);
351 using _FmtStr = __format::_Runtime_format_string<_CharT>;
354 = __conditional_t<is_pointer_v<thread::native_handle_type>,
356 thread::native_handle_type>;
357 auto __o =
static_cast<__output_type
>(__id._M_thread);
358 __sv = __str = std::format(_FmtStr(__sv.substr(0, 2)), __o);
360 return __format::__write_padded_as_spec(__sv, __sv.size(),
362 __format::_Align_right);
366 __format::_Spec<_CharT> _M_spec;
372_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.