1 // <thread> -*- C++ -*-
3 // Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/thread
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
41 #include <bits/functexcept.h>
42 #include <bits/functional_hash.h>
43 #include <bits/gthr.h>
45 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * @defgroup threads Threads
53 * @ingroup concurrency
55 * Classes for thread support.
63 typedef __gthread_t native_handle_type;
65 typedef shared_ptr<_Impl_base> __shared_base_type;
70 native_handle_type _M_thread;
73 id() noexcept : _M_thread() { }
76 id(native_handle_type __id) : _M_thread(__id) { }
80 friend class hash<thread::id>;
83 operator==(thread::id __x, thread::id __y) noexcept
84 { return __gthread_equal(__x._M_thread, __y._M_thread); }
87 operator<(thread::id __x, thread::id __y) noexcept
88 { return __x._M_thread < __y._M_thread; }
90 template<class _CharT, class _Traits>
91 friend basic_ostream<_CharT, _Traits>&
92 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
95 // Simple base type that the templatized, derived class containing
96 // an arbitrary functor can be converted to and called.
99 __shared_base_type _M_this_ptr;
101 inline virtual ~_Impl_base();
103 virtual void _M_run() = 0;
106 template<typename _Callable>
107 struct _Impl : public _Impl_base
111 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
115 _M_run() { _M_func(); }
122 thread() noexcept = default;
123 // _GLIBCXX_RESOLVE_LIB_DEFECTS
124 // 2097. packaged_task constructors should be constrained
125 thread(thread&) = delete;
126 thread(const thread&) = delete;
128 thread(thread&& __t) noexcept
131 template<typename _Callable, typename... _Args>
133 thread(_Callable&& __f, _Args&&... __args)
135 #ifdef GTHR_ACTIVE_PROXY
136 __asm ("" : : "r" (&pthread_create));
138 _M_start_thread(_M_make_routine(std::__bind_simple(
139 std::forward<_Callable>(__f),
140 std::forward<_Args>(__args)...)));
149 thread& operator=(const thread&) = delete;
151 thread& operator=(thread&& __t) noexcept
160 swap(thread& __t) noexcept
161 { std::swap(_M_id, __t._M_id); }
164 joinable() const noexcept
165 { return !(_M_id == id()); }
174 get_id() const noexcept
177 /** @pre thread is joinable
181 { return _M_id._M_thread; }
183 // Returns a value that hints at the number of hardware thread contexts.
185 hardware_concurrency() noexcept;
189 _M_start_thread(__shared_base_type);
191 template<typename _Callable>
192 shared_ptr<_Impl<_Callable>>
193 _M_make_routine(_Callable&& __f)
195 // Create and allocate full data structure, not base.
196 return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
200 inline thread::_Impl_base::~_Impl_base() = default;
203 swap(thread& __x, thread& __y) noexcept
207 operator!=(thread::id __x, thread::id __y) noexcept
208 { return !(__x == __y); }
211 operator<=(thread::id __x, thread::id __y) noexcept
212 { return !(__y < __x); }
215 operator>(thread::id __x, thread::id __y) noexcept
216 { return __y < __x; }
219 operator>=(thread::id __x, thread::id __y) noexcept
220 { return !(__x < __y); }
223 /// std::hash specialization for thread::id.
225 struct hash<thread::id>
226 : public __hash_base<size_t, thread::id>
229 operator()(const thread::id& __id) const noexcept
230 { return std::_Hash_impl::hash(__id._M_thread); }
233 template<class _CharT, class _Traits>
234 inline basic_ostream<_CharT, _Traits>&
235 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
237 if (__id == thread::id())
238 return __out << "thread::id of a non-executing thread";
240 return __out << __id._M_thread;
243 _GLIBCXX_END_NAMESPACE_VERSION
245 /** @namespace std::this_thread
246 * @brief ISO C++ 2011 entities sub-namespace for thread.
247 * 30.3.2 Namespace this_thread.
249 namespace this_thread
251 _GLIBCXX_BEGIN_NAMESPACE_VERSION
255 get_id() noexcept { return thread::id(__gthread_self()); }
261 #ifdef _GLIBCXX_USE_SCHED_YIELD
267 __sleep_for(chrono::seconds, chrono::nanoseconds);
270 template<typename _Rep, typename _Period>
272 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
274 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
275 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
276 #ifdef _GLIBCXX_USE_NANOSLEEP
277 __gthread_time_t __ts =
279 static_cast<std::time_t>(__s.count()),
280 static_cast<long>(__ns.count())
282 ::nanosleep(&__ts, 0);
284 __sleep_for(__s, __ns);
289 template<typename _Clock, typename _Duration>
291 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
292 { sleep_for(__atime - _Clock::now()); }
294 _GLIBCXX_END_NAMESPACE_VERSION
301 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
305 #endif // _GLIBCXX_THREAD