29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
40 #if defined(_GLIBCXX_HAS_GTHREADS)
41 #include <bits/gthr.h>
47 #if __cplusplus > 201703L
52 #ifdef _GLIBCXX_USE_NANOSLEEP
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
81 virtual void _M_run() = 0;
85 typedef __gthread_t native_handle_type;
90 native_handle_type _M_thread;
93 id() noexcept : _M_thread() { }
96 id(native_handle_type __id) : _M_thread(__id) { }
100 friend class hash<id>;
103 operator==(
id __x,
id __y) noexcept;
105 #if __cpp_lib_three_way_comparison
106 friend strong_ordering
107 operator<=>(
id __x,
id __y) noexcept;
110 operator<(
id __x,
id __y) noexcept;
113 template<
class _CharT,
class _Traits>
115 operator<<(basic_ostream<_CharT, _Traits>& __out,
id __id);
124 template<
typename _Tp>
125 using __not_same = __not_<is_same<__remove_cvref_t<_Tp>,
thread>>;
128 thread() noexcept = default;
130 template<typename _Callable, typename... _Args,
131 typename = _Require<__not_same<_Callable>>>
133 thread(_Callable&& __f, _Args&&... __args)
135 static_assert( __is_invocable<
typename decay<_Callable>::type,
136 typename decay<_Args>::type...>::value,
137 "std::thread arguments must be invocable after conversion to rvalues"
140 #ifdef GTHR_ACTIVE_PROXY
142 auto __depend =
reinterpret_cast<void(*)()
>(&pthread_create);
144 auto __depend =
nullptr;
149 _M_start_thread(_S_make_state<_Invoker_type>(
150 std::forward<_Callable>(__f), std::forward<_Args>(__args)...),
160 thread(
const thread&) =
delete;
162 thread(thread&& __t) noexcept
165 thread& operator=(
const thread&) =
delete;
167 thread& operator=(thread&& __t) noexcept
176 swap(thread& __t) noexcept
177 { std::swap(_M_id, __t._M_id); }
180 joinable() const noexcept
181 {
return !(_M_id == id()); }
197 {
return _M_id._M_thread; }
201 hardware_concurrency() noexcept;
204 template<typename _Callable>
205 struct _State_impl : public _State
209 template<
typename... _Args>
210 _State_impl(_Args&&... __args)
211 : _M_func{{std::forward<_Args>(__args)...}}
215 _M_run() { _M_func(); }
219 _M_start_thread(_State_ptr,
void (*)());
221 template<
typename _Callable,
typename... _Args>
223 _S_make_state(_Args&&... __args)
225 using _Impl = _State_impl<_Callable>;
226 return _State_ptr{
new _Impl{std::forward<_Args>(__args)...}};
228 #if _GLIBCXX_THREAD_ABI_COMPAT
231 typedef shared_ptr<_Impl_base> __shared_base_type;
234 __shared_base_type _M_this_ptr;
235 virtual ~_Impl_base() =
default;
236 virtual void _M_run() = 0;
241 _M_start_thread(__shared_base_type,
void (*)());
244 _M_start_thread(__shared_base_type);
249 template<
typename _Tuple>
256 template<
typename _Fn,
typename... _Args>
257 struct __result<tuple<_Fn, _Args...>>
258 : __invoke_result<_Fn, _Args...>
261 template<
size_t... _Ind>
262 typename __result<_Tuple>::type
263 _M_invoke(_Index_tuple<_Ind...>)
264 {
return std::__invoke(std::get<_Ind>(
std::move(_M_t))...); }
266 typename __result<_Tuple>::type
270 =
typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
271 return _M_invoke(_Indices());
275 template<
typename... _Tp>
276 using __decayed_tuple = tuple<typename decay<_Tp>::type...>;
281 template<
typename _Callable,
typename... _Args>
282 static _Invoker<__decayed_tuple<_Callable, _Args...>>
283 __make_invoker(_Callable&& __callable, _Args&&... __args)
285 return { __decayed_tuple<_Callable, _Args...>{
286 std::forward<_Callable>(__callable), std::forward<_Args>(__args)...
292 swap(thread& __x, thread& __y) noexcept
296 operator==(thread::id __x, thread::id __y) noexcept
302 return __x._M_thread == __y._M_thread;
305 #if __cpp_lib_three_way_comparison
306 inline strong_ordering
307 operator<=>(thread::id __x, thread::id __y) noexcept
308 {
return __x._M_thread <=> __y._M_thread; }
311 operator!=(thread::id __x, thread::id __y) noexcept
312 {
return !(__x == __y); }
315 operator<(thread::id __x, thread::id __y) noexcept
319 return __x._M_thread < __y._M_thread;
323 operator<=(thread::id __x, thread::id __y) noexcept
324 {
return !(__y < __x); }
327 operator>(thread::id __x, thread::id __y) noexcept
328 {
return __y < __x; }
331 operator>=(thread::id __x, thread::id __y) noexcept
332 {
return !(__x < __y); }
333 #endif // __cpp_lib_three_way_comparison
339 :
public __hash_base<size_t, thread::id>
342 operator()(
const thread::id& __id)
const noexcept
343 {
return std::_Hash_impl::hash(__id._M_thread); }
346 template<
class _CharT,
class _Traits>
348 operator<<(basic_ostream<_CharT, _Traits>& __out,
thread::id __id)
351 return __out <<
"thread::id of a non-executing thread";
353 return __out << __id._M_thread;
361 namespace this_thread
367 #ifdef _GLIBCXX_NATIVE_THREAD_ID
378 #ifdef _GLIBCXX_USE_SCHED_YIELD
387 template<
typename _Rep,
typename _Period>
391 if (__rtime <= __rtime.zero())
395 #ifdef _GLIBCXX_USE_NANOSLEEP
396 __gthread_time_t __ts =
398 static_cast<std::time_t
>(__s.count()),
399 static_cast<long>(__ns.count())
401 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
404 __sleep_for(__s, __ns);
409 template<
typename _Clock,
typename _Duration>
413 #if __cplusplus > 201703L
414 static_assert(chrono::is_clock_v<_Clock>);
416 auto __now = _Clock::now();
417 if (_Clock::is_steady)
423 while (__now < __atime)
426 __now = _Clock::now();
433 #ifdef __cpp_lib_jthread
438 using id = thread::id;
439 using native_handle_type = thread::native_handle_type;
442 : _M_stop_source{nostopstate}
445 template<
typename _Callable,
typename... _Args,
446 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
449 jthread(_Callable&& __f, _Args&&... __args)
450 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
451 std::forward<_Args>(__args)...)}
454 jthread(
const jthread&) =
delete;
455 jthread(jthread&&) noexcept = default;
467 operator=(
const jthread&) =
delete;
470 operator=(jthread&& __other) noexcept
472 std::jthread(
std::move(__other)).swap(*
this);
477 swap(jthread& __other) noexcept
479 std::swap(_M_stop_source, __other._M_stop_source);
480 std::swap(_M_thread, __other._M_thread);
484 joinable() const noexcept
486 return _M_thread.joinable();
504 return _M_thread.get_id();
507 [[nodiscard]] native_handle_type
510 return _M_thread.native_handle();
513 [[nodiscard]]
static unsigned
514 hardware_concurrency() noexcept
516 return thread::hardware_concurrency();
519 [[nodiscard]] stop_source
520 get_stop_source() noexcept
522 return _M_stop_source;
525 [[nodiscard]] stop_token
526 get_stop_token() const noexcept
528 return _M_stop_source.get_token();
531 bool request_stop() noexcept
533 return _M_stop_source.request_stop();
536 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
542 template<
typename _Callable,
typename... _Args>
544 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
546 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
548 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
549 std::forward<_Args>(__args)...};
552 static_assert(is_invocable_v<decay_t<_Callable>,
554 "std::thread arguments must be invocable after"
555 " conversion to rvalues");
556 return thread{std::forward<_Callable>(__f),
557 std::forward<_Args>(__args)...};
561 stop_source _M_stop_source;
564 #endif // __cpp_lib_jthread
565 _GLIBCXX_END_NAMESPACE_VERSION
567 #endif // _GLIBCXX_HAS_GTHREADS
569 #endif // _GLIBCXX_THREAD
native_handle_type native_handle()
void sleep_for(const chrono::duration< _Rep, _Period > &__rtime)
sleep_for
Primary class template, tuple.
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Primary class template hash.
void terminate() noexcept
thread::id get_id() noexcept
get_id
void sleep_until(const chrono::time_point< _Clock, _Duration > &__atime)
sleep_until
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Template class basic_ostream.
void yield() noexcept
yield
20.7.1.2 unique_ptr for single objects.