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) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
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);
107 template<typename _Tp>
108 using __not_same = __not_<is_same<
109 typename remove_cv<typename remove_reference<_Tp>::type>::type,
113 thread() noexcept = default;
115 template<typename _Callable, typename... _Args,
116 typename = _Require<__not_same<_Callable>>>
118 thread(_Callable&& __f, _Args&&... __args)
120 static_assert( __is_invocable<typename decay<_Callable>::type,
121 typename decay<_Args>::type...>::value,
122 "std::thread arguments must be invocable after conversion to rvalues"
125 #ifdef GTHR_ACTIVE_PROXY
127 auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
129 auto __depend = nullptr;
131 _M_start_thread(_S_make_state(
132 __make_invoker(std::forward<_Callable>(__f),
133 std::forward<_Args>(__args)...)),
143 thread(const thread&) = delete;
145 thread(thread&& __t) noexcept
148 thread& operator=(const thread&) = delete;
150 thread& operator=(thread&& __t) noexcept
159 swap(thread& __t) noexcept
160 { std::swap(_M_id, __t._M_id); }
163 joinable() const noexcept
164 { return !(_M_id == id()); }
173 get_id() const noexcept
180 { return _M_id._M_thread; }
184 hardware_concurrency() noexcept;
187 template<typename _Callable>
188 struct _State_impl : public _State
192 _State_impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
196 _M_run() { _M_func(); }
200 _M_start_thread(_State_ptr, void (*)());
202 template<typename _Callable>
204 _S_make_state(_Callable&& __f)
206 using _Impl = _State_impl<_Callable>;
207 return _State_ptr{new _Impl{std::forward<_Callable>(__f)}};
209 #if _GLIBCXX_THREAD_ABI_COMPAT
212 typedef shared_ptr<_Impl_base> __shared_base_type;
215 __shared_base_type _M_this_ptr;
216 virtual ~_Impl_base() = default;
217 virtual void _M_run() = 0;
222 _M_start_thread(__shared_base_type, void (*)());
225 _M_start_thread(__shared_base_type);
230 template<typename _Tuple>
235 template<size_t _Index>
236 static __tuple_element_t<_Index, _Tuple>&&
239 template<size_t... _Ind>
241 _M_invoke(_Index_tuple<_Ind...>)
242 noexcept(noexcept(std::__invoke(_S_declval<_Ind>()...)))
243 -> decltype(std::__invoke(_S_declval<_Ind>()...))
244 { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
247 = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
251 noexcept(noexcept(std::declval<_Invoker&>()._M_invoke(_Indices())))
252 -> decltype(std::declval<_Invoker&>()._M_invoke(_Indices()))
253 { return _M_invoke(_Indices()); }
256 template<typename... _Tp>
257 using __decayed_tuple = tuple<typename std::decay<_Tp>::type...>;
262 template<typename _Callable, typename... _Args>
263 static _Invoker<__decayed_tuple<_Callable, _Args...>>
264 __make_invoker(_Callable&& __callable, _Args&&... __args)
266 return { __decayed_tuple<_Callable, _Args...>{
267 std::forward<_Callable>(__callable), std::forward<_Args>(__args)...
273 swap(thread& __x, thread& __y) noexcept
277 operator==(thread::id __x, thread::id __y) noexcept
283 return __x._M_thread == __y._M_thread;
287 operator!=(thread::id __x, thread::id __y) noexcept
288 { return !(__x == __y); }
291 operator<(thread::id __x, thread::id __y) noexcept
295 return __x._M_thread < __y._M_thread;
299 operator<=(thread::id __x, thread::id __y) noexcept
300 { return !(__y < __x); }
303 operator>(thread::id __x, thread::id __y) noexcept
304 { return __y < __x; }
307 operator>=(thread::id __x, thread::id __y) noexcept
308 { return !(__x < __y); }
313 struct hash<thread::id>
314 : public __hash_base<size_t, thread::id>
317 operator()(const thread::id& __id) const noexcept
318 { return std::_Hash_impl::hash(__id._M_thread); }
321 template<class _CharT, class _Traits>
322 inline basic_ostream<_CharT, _Traits>&
323 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
325 if (__id == thread::id())
326 return __out << "thread::id of a non-executing thread";
328 return __out << __id._M_thread;
335 namespace this_thread
346 if (!__gthread_active_p())
347 return thread::id(1);
349 return thread::id(__gthread_self());
356 #ifdef _GLIBCXX_USE_SCHED_YIELD
362 __sleep_for(chrono::seconds, chrono::nanoseconds);
365 template<typename _Rep, typename _Period>
367 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
369 if (__rtime <= __rtime.zero())
371 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
372 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
373 #ifdef _GLIBCXX_USE_NANOSLEEP
374 __gthread_time_t __ts =
376 static_cast<std::time_t>(__s.count()),
377 static_cast<long>(__ns.count())
379 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
382 __sleep_for(__s, __ns);
387 template<typename _Clock, typename _Duration>
389 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
391 auto __now = _Clock::now();
392 if (_Clock::is_steady)
395 sleep_for(__atime - __now);
398 while (__now < __atime)
400 sleep_for(__atime - __now);
401 __now = _Clock::now();
408 _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.