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;
82 id() noexcept : _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);
106 thread() noexcept = default;
109 thread(thread&) = delete;
110 thread(const thread&) = delete;
111 thread(const thread&&) = delete;
113 thread(thread&& __t) noexcept
116 template<typename _Callable, typename... _Args>
118 thread(_Callable&& __f, _Args&&... __args)
120 #ifdef GTHR_ACTIVE_PROXY
122 auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
124 auto __depend = nullptr;
126 _M_start_thread(_S_make_state(
127 __make_invoker(std::forward<_Callable>(__f),
128 std::forward<_Args>(__args)...)),
138 thread& operator=(const thread&) = delete;
140 thread& operator=(thread&& __t) noexcept
149 swap(thread& __t) noexcept
150 { std::swap(_M_id, __t._M_id); }
153 joinable() const noexcept
154 { return !(_M_id == id()); }
163 get_id() const noexcept
170 { return _M_id._M_thread; }
174 hardware_concurrency() noexcept;
177 template<typename _Callable>
178 struct _State_impl : public _State
182 _State_impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
186 _M_run() { _M_func(); }
190 _M_start_thread(_State_ptr, void (*)());
192 template<typename _Callable>
194 _S_make_state(_Callable&& __f)
196 using _Impl = _State_impl<_Callable>;
197 return _State_ptr{new _Impl{std::forward<_Callable>(__f)}};
199 #if _GLIBCXX_THREAD_ABI_COMPAT
202 typedef shared_ptr<_Impl_base> __shared_base_type;
205 __shared_base_type _M_this_ptr;
206 virtual ~_Impl_base() = default;
207 virtual void _M_run() = 0;
212 _M_start_thread(__shared_base_type, void (*)());
215 _M_start_thread(__shared_base_type);
220 template<typename _Tuple>
225 template<size_t _Index>
226 static __tuple_element_t<_Index, _Tuple>&&
229 template<size_t... _Ind>
231 _M_invoke(_Index_tuple<_Ind...>)
232 noexcept(noexcept(std::__invoke(_S_declval<_Ind>()...)))
233 -> decltype(std::__invoke(_S_declval<_Ind>()...))
234 { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
237 = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
241 noexcept(noexcept(std::declval<_Invoker&>()._M_invoke(_Indices())))
242 -> decltype(std::declval<_Invoker&>()._M_invoke(_Indices()))
243 { return _M_invoke(_Indices()); }
247 template<typename... _Tp>
249 = _Invoker<decltype(std::make_tuple(std::declval<_Tp>()...))>;
254 template<typename _Callable, typename... _Args>
255 static __invoker_type<_Callable, _Args...>
256 __make_invoker(_Callable&& __callable, _Args&&... __args)
259 std::make_tuple(std::forward<_Callable>(__callable),
260 std::forward<_Args>(__args)...)
266 swap(thread& __x, thread& __y) noexcept
270 operator==(thread::id __x, thread::id __y) noexcept
276 return __x._M_thread == __y._M_thread;
280 operator!=(thread::id __x, thread::id __y) noexcept
281 { return !(__x == __y); }
284 operator<(thread::id __x, thread::id __y) noexcept
288 return __x._M_thread < __y._M_thread;
292 operator<=(thread::id __x, thread::id __y) noexcept
293 { return !(__y < __x); }
296 operator>(thread::id __x, thread::id __y) noexcept
297 { return __y < __x; }
300 operator>=(thread::id __x, thread::id __y) noexcept
301 { return !(__x < __y); }
306 struct hash<thread::id>
307 : public __hash_base<size_t, thread::id>
310 operator()(const thread::id& __id) const noexcept
311 { return std::_Hash_impl::hash(__id._M_thread); }
314 template<class _CharT, class _Traits>
315 inline basic_ostream<_CharT, _Traits>&
316 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
318 if (__id == thread::id())
319 return __out << "thread::id of a non-executing thread";
321 return __out << __id._M_thread;
324 _GLIBCXX_END_NAMESPACE_VERSION
330 namespace this_thread
332 _GLIBCXX_BEGIN_NAMESPACE_VERSION
343 if (!__gthread_active_p())
344 return thread::id(1);
346 return thread::id(__gthread_self());
353 #ifdef _GLIBCXX_USE_SCHED_YIELD
359 __sleep_for(chrono::seconds, chrono::nanoseconds);
362 template<typename _Rep, typename _Period>
364 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
366 if (__rtime <= __rtime.zero())
368 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
369 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
370 #ifdef _GLIBCXX_USE_NANOSLEEP
371 __gthread_time_t __ts =
373 static_cast<std::time_t>(__s.count()),
374 static_cast<long>(__ns.count())
376 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
379 __sleep_for(__s, __ns);
384 template<typename _Clock, typename _Duration>
386 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
388 auto __now = _Clock::now();
389 if (_Clock::is_steady)
392 sleep_for(__atime - __now);
395 while (__now < __atime)
397 sleep_for(__atime - __now);
398 __now = _Clock::now();
402 _GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
20.7.1.2 unique_ptr for single objects.
Primary class template hash.