29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #include <bits/gthr.h>
47 #if defined(_GLIBCXX_HAS_GTHREADS)
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 virtual void _M_run() = 0;
74 typedef __gthread_t native_handle_type;
79 native_handle_type _M_thread;
85 id(native_handle_type __id) : _M_thread(__id) { }
89 friend class hash<thread::
id>;
92 operator==(thread::id __x, thread::id __y)
noexcept;
95 operator<(thread::id __x, thread::id __y) noexcept;
97 template<class _CharT, class _Traits>
98 friend basic_ostream<_CharT, _Traits>&
99 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
108 template<typename _Tp>
109 using __not_same = __not_<is_same<__remove_cvref_t<_Tp>, thread>>;
112 thread() noexcept = default;
114 template<typename _Callable, typename... _Args,
115 typename = _Require<__not_same<_Callable>>>
117 thread(_Callable&& __f, _Args&&... __args)
119 static_assert( __is_invocable<typename decay<_Callable>::type,
120 typename decay<_Args>::type...>::value,
121 "std::thread arguments must be invocable after conversion to rvalues"
124 #ifdef GTHR_ACTIVE_PROXY
126 auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
128 auto __depend = nullptr;
130 _M_start_thread(_S_make_state(
131 __make_invoker(std::forward<_Callable>(__f),
132 std::forward<_Args>(__args)...)),
142 thread(const thread&) = delete;
144 thread(thread&& __t) noexcept
147 thread& operator=(const thread&) = delete;
149 thread& operator=(thread&& __t) noexcept
158 swap(thread& __t) noexcept
159 { std::swap(_M_id, __t._M_id); }
162 joinable() const noexcept
163 { return !(_M_id == id()); }
172 get_id() const noexcept
179 { return _M_id._M_thread; }
183 hardware_concurrency() noexcept;
186 template<typename _Callable>
187 struct _State_impl : public _State
191 _State_impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
195 _M_run() { _M_func(); }
199 _M_start_thread(_State_ptr, void (*)());
201 template<typename _Callable>
203 _S_make_state(_Callable&& __f)
205 using _Impl = _State_impl<_Callable>;
206 return _State_ptr{new _Impl{std::forward<_Callable>(__f)}};
208 #if _GLIBCXX_THREAD_ABI_COMPAT
211 typedef shared_ptr<_Impl_base> __shared_base_type;
214 __shared_base_type _M_this_ptr;
215 virtual ~_Impl_base() = default;
216 virtual void _M_run() = 0;
221 _M_start_thread(__shared_base_type, void (*)());
224 _M_start_thread(__shared_base_type);
229 template<typename _Tuple>
236 template<typename _Fn, typename... _Args>
237 struct __result<tuple<_Fn, _Args...>>
238 : __invoke_result<_Fn, _Args...>
241 template<size_t... _Ind>
242 typename __result<_Tuple>::type
243 _M_invoke(_Index_tuple<_Ind...>)
244 { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
246 typename __result<_Tuple>::type
250 = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
251 return _M_invoke(_Indices());
255 template<typename... _Tp>
256 using __decayed_tuple = tuple<typename decay<_Tp>::type...>;
261 template<typename _Callable, typename... _Args>
262 static _Invoker<__decayed_tuple<_Callable, _Args...>>
263 __make_invoker(_Callable&& __callable, _Args&&... __args)
265 return { __decayed_tuple<_Callable, _Args...>{
266 std::forward<_Callable>(__callable), std::forward<_Args>(__args)...
272 swap(thread& __x, thread& __y) noexcept
276 operator==(thread::id __x, thread::id __y) noexcept
282 return __x._M_thread == __y._M_thread;
286 operator!=(thread::id __x, thread::id __y) noexcept
287 { return !(__x == __y); }
290 operator<(thread::id __x, thread::id __y) noexcept
294 return __x._M_thread < __y._M_thread;
298 operator<=(thread::id __x, thread::id __y) noexcept
299 { return !(__y < __x); }
302 operator>(thread::id __x, thread::id __y) noexcept
303 { return __y < __x; }
306 operator>=(thread::id __x, thread::id __y) noexcept
307 { return !(__x < __y); }
312 struct hash<thread::id>
313 : public __hash_base<size_t, thread::id>
316 operator()(const thread::id& __id) const noexcept
317 { return std::_Hash_impl::hash(__id._M_thread); }
320 template<class _CharT, class _Traits>
321 inline basic_ostream<_CharT, _Traits>&
322 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
324 if (__id == thread::id())
325 return __out << "thread::id of a non-executing thread";
327 return __out << __id._M_thread;
334 namespace this_thread
345 if (!__gthread_active_p())
346 return thread::id(1);
348 return thread::id(__gthread_self());
355 #ifdef _GLIBCXX_USE_SCHED_YIELD
361 __sleep_for(chrono::seconds, chrono::nanoseconds);
364 template<typename _Rep, typename _Period>
366 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
368 if (__rtime <= __rtime.zero())
370 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
371 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
372 #ifdef _GLIBCXX_USE_NANOSLEEP
373 __gthread_time_t __ts =
375 static_cast<std::time_t>(__s.count()),
376 static_cast<long>(__ns.count())
378 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
381 __sleep_for(__s, __ns);
386 template<typename _Clock, typename _Duration>
388 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
390 auto __now = _Clock::now();
391 if (_Clock::is_steady)
394 sleep_for(__atime - __now);
397 while (__now < __atime)
399 sleep_for(__atime - __now);
400 __now = _Clock::now();
407 _GLIBCXX_END_NAMESPACE_VERSION
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
20.7.1.2 unique_ptr for single objects.
Primary class template hash.