1 // <thread> -*- C++ -*-
3 // Copyright (C) 2008, 2009 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/>.
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <c++0x_warning.h>
42 #include <condition_variable>
44 #include <bits/functexcept.h>
45 #include <bits/gthr.h>
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
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() : _M_thread() { }
76 id(native_handle_type __id) : _M_thread(__id) { }
82 operator==(thread::id __x, thread::id __y)
83 { return __gthread_equal(__x._M_thread, __y._M_thread); }
86 operator<(thread::id __x, thread::id __y)
87 { return __x._M_thread < __y._M_thread; }
89 template<class _CharT, class _Traits>
90 friend basic_ostream<_CharT, _Traits>&
91 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
94 // Simple base type that the templatized, derived class containing
95 // an arbitrary functor can be converted to and called.
98 __shared_base_type _M_this_ptr;
100 virtual ~_Impl_base() = default;
102 virtual void _M_run() = 0;
105 template<typename _Callable>
106 struct _Impl : public _Impl_base
110 _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
114 _M_run() { _M_func(); }
122 thread(const thread&) = delete;
127 template<typename _Callable>
128 explicit thread(_Callable __f)
129 { _M_start_thread(_M_make_routine<_Callable>(__f)); }
131 template<typename _Callable, typename... _Args>
132 thread(_Callable&& __f, _Args&&... __args)
133 { _M_start_thread(_M_make_routine(std::bind(__f, __args...))); }
141 thread& operator=(const thread&) = delete;
143 thread& operator=(thread&& __t)
153 { std::swap(_M_id, __t._M_id); }
157 { return !(_M_id == id()); }
169 /** @pre thread is joinable
173 { return _M_id._M_thread; }
175 // Returns a value that hints at the number of hardware thread contexts.
177 hardware_concurrency()
182 _M_start_thread(__shared_base_type);
184 template<typename _Callable>
185 shared_ptr<_Impl<_Callable>>
186 _M_make_routine(_Callable&& __f)
188 // Create and allocate full data structure, not base.
189 return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
194 swap(thread& __x, thread& __y)
198 operator!=(thread::id __x, thread::id __y)
199 { return !(__x == __y); }
202 operator<=(thread::id __x, thread::id __y)
203 { return !(__y < __x); }
206 operator>(thread::id __x, thread::id __y)
207 { return __y < __x; }
210 operator>=(thread::id __x, thread::id __y)
211 { return !(__x < __y); }
213 template<class _CharT, class _Traits>
214 inline basic_ostream<_CharT, _Traits>&
215 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
217 if (__id == thread::id())
218 return __out << "thread::id of a non-executing thread";
220 return __out << __id._M_thread;
223 /** @namespace std::this_thread
224 * @brief ISO C++ 0x entities sub namespace for thread.
225 * 30.2.2 Namespace this_thread.
227 namespace this_thread
231 get_id() { return thread::id(__gthread_self()); }
233 #ifdef _GLIBCXX_USE_SCHED_YIELD
237 { __gthread_yield(); }
240 #ifdef _GLIBCXX_USE_NANOSLEEP
242 template<typename _Clock, typename _Duration>
244 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
245 { sleep_for(__atime - _Clock::now()); }
248 template<typename _Rep, typename _Period>
250 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
252 chrono::seconds __s =
253 chrono::duration_cast<chrono::seconds>(__rtime);
255 chrono::nanoseconds __ns =
256 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
258 __gthread_time_t __ts =
260 static_cast<std::time_t>(__s.count()),
261 static_cast<long>(__ns.count())
264 ::nanosleep(&__ts, 0);
272 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
274 #endif // __GXX_EXPERIMENTAL_CXX0X__
276 #endif // _GLIBCXX_THREAD