29 #ifndef _GLIBCXX_THREAD 30 #define _GLIBCXX_THREAD 1 32 #ifdef _GLIBCXX_SYSHDR 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 58 namespace std _GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
75 #if __cpp_lib_three_way_comparison 76 inline strong_ordering
78 {
return __x._M_thread <=> __y._M_thread; }
82 {
return !(__x == __y); }
89 return __x._M_thread < __y._M_thread;
94 {
return !(__y < __x); }
102 {
return !(__x < __y); }
103 #endif // __cpp_lib_three_way_comparison 105 template<
class _CharT,
class _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>;
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 // C++ >= 20 125 #ifndef __STRICT_ANSI__ 128 template<
typename _Callable,
typename... _Args>
129 constexpr
bool __pmf_expects_stop_token =
false;
131 template<
typename _Callable,
typename _Obj,
typename... _Args>
132 requires is_member_function_pointer_v<remove_reference_t<_Callable>>
133 constexpr
bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
134 = is_invocable_v<_Callable, _Obj,
stop_token, _Args...>;
156 using native_handle_type = thread::native_handle_type;
159 : _M_stop_source{nostopstate}
162 template<
typename _Callable,
typename... _Args,
166 jthread(_Callable&& __f, _Args&&... __args)
167 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
168 std::forward<_Args>(__args)...)}
171 jthread(
const jthread&) =
delete;
172 jthread(jthread&&) noexcept =
default;
184 operator=(
const jthread&) =
delete;
187 operator=(jthread&& __other) noexcept
189 std::jthread(
std::move(__other)).swap(*
this);
194 swap(jthread& __other) noexcept
196 std::swap(_M_stop_source, __other._M_stop_source);
197 std::swap(_M_thread, __other._M_thread);
201 joinable()
const noexcept
203 return _M_thread.joinable();
221 return _M_thread.get_id();
224 [[nodiscard]] native_handle_type
227 return _M_thread.native_handle();
230 [[nodiscard]]
static unsigned 231 hardware_concurrency() noexcept
233 return thread::hardware_concurrency();
237 get_stop_source() noexcept
239 return _M_stop_source;
242 [[nodiscard]] stop_token
243 get_stop_token()
const noexcept
245 return _M_stop_source.get_token();
248 bool request_stop() noexcept
250 return _M_stop_source.request_stop();
253 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
259 template<
typename _Callable,
typename... _Args>
261 _S_create(
stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
263 #ifndef __STRICT_ANSI__ 264 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
265 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
270 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
271 std::forward<_Args>(__args)...};
276 "std::jthread arguments must be invocable after" 277 " conversion to rvalues");
278 return thread{std::forward<_Callable>(__f),
279 std::forward<_Args>(__args)...};
283 #ifndef __STRICT_ANSI__ 284 template<
typename _Callable,
typename _Obj,
typename... _Args>
286 _S_create_pmf(
stop_source& __ssrc, _Callable __f, _Obj&& __obj,
289 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
290 std::forward<_Args>(__args)...};
299 namespace __detail::__variant
301 template<
typename>
struct _Never_valueless_alt;
306 struct _Never_valueless_alt<std::jthread>
311 #endif // __cpp_lib_jthread 313 #ifdef __cpp_lib_formatters // C++ >= 23 316 template<__format::__
char _CharT>
317 requires is_pointer_v<thread::native_handle_type>
318 || is_integral_v<thread::native_handle_type>
319 class formatter<thread::
id, _CharT>
322 constexpr
typename basic_format_parse_context<_CharT>::iterator
323 parse(basic_format_parse_context<_CharT>& __pc)
325 __format::_Spec<_CharT> __spec{};
326 __spec._M_align = __format::_Align_right;
327 const auto __last = __pc.end();
328 auto __first = __pc.begin();
330 auto __finalize = [
this, &__spec] {
334 auto __finished = [&] {
335 if (__first == __last || *__first ==
'}')
346 __first = __spec._M_parse_fill_and_align(__first, __last);
350 __first = __spec._M_parse_width(__first, __last, __pc);
354 std::__throw_format_error(
"format error: invalid format-spec for " 358 template<
typename _Out>
359 typename basic_format_context<_Out, _CharT>::iterator
360 format(
thread::id __id, basic_format_context<_Out, _CharT>& __fc)
const 366 if constexpr (is_same_v<_CharT, char>)
367 __msg =
"thread::id of a non-executing thread";
369 __msg = L
"thread::id of a non-executing thread";
371 __format::__formatter_str<_CharT> __formatter(_M_spec);
372 return __formatter.format(__msg, __fc);
375 using _HandleFormatter
376 = __conditional_t<is_pointer_v<thread::native_handle_type>,
377 __format::__formatter_ptr<_CharT>,
378 __format::__formatter_int<_CharT>>;
380 _HandleFormatter __formatter(_M_spec);
381 return __formatter.format(__id._M_thread, __fc);
385 __format::_Spec<_CharT> _M_spec;
388 #if __glibcxx_print >= 202406L 390 inline constexpr
bool 391 enable_nonlocking_formatter_optimization<thread::id> =
true;
394 #endif // __cpp_lib_formatters 398 _GLIBCXX_END_NAMESPACE_VERSION
401 #endif // _GLIBCXX_THREAD A type that allows a stop request to be made.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
typename decay< _Tp >::type decay_t
Alias template for decay.
Template class basic_ostream.
ISO C++ entities toplevel namespace is std.
thread::id get_id() noexcept
The unique identifier of the current thread.
Allow testing whether a stop request has been made on a stop_source.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.