libstdc++
thread
Go to the documentation of this file.
1 // <thread> -*- C++ -*-
2 
3 // Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 
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.
15 
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.
19 
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/>.
24 
25 /** @file include/thread
26  * This is a Standard C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
31 
32 #pragma GCC system_header
33 
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
37 
38 #include <chrono>
39 #include <functional>
40 #include <memory>
41 #include <bits/functexcept.h>
42 #include <bits/functional_hash.h>
43 #include <bits/gthr.h>
44 
45 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
46 
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51  /**
52  * @defgroup threads Threads
53  * @ingroup concurrency
54  *
55  * Classes for thread support.
56  * @{
57  */
58 
59  /// thread
60  class thread
61  {
62  public:
63  typedef __gthread_t native_handle_type;
64  struct _Impl_base;
65  typedef shared_ptr<_Impl_base> __shared_base_type;
66 
67  /// thread::id
68  class id
69  {
70  native_handle_type _M_thread;
71 
72  public:
73  id() noexcept : _M_thread() { }
74 
75  explicit
76  id(native_handle_type __id) : _M_thread(__id) { }
77 
78  private:
79  friend class thread;
80  friend class hash<thread::id>;
81 
82  friend bool
83  operator==(thread::id __x, thread::id __y) noexcept
84  { return __gthread_equal(__x._M_thread, __y._M_thread); }
85 
86  friend bool
87  operator<(thread::id __x, thread::id __y) noexcept
88  { return __x._M_thread < __y._M_thread; }
89 
90  template<class _CharT, class _Traits>
91  friend basic_ostream<_CharT, _Traits>&
92  operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
93  };
94 
95  // Simple base type that the templatized, derived class containing
96  // an arbitrary functor can be converted to and called.
97  struct _Impl_base
98  {
99  __shared_base_type _M_this_ptr;
100 
101  inline virtual ~_Impl_base();
102 
103  virtual void _M_run() = 0;
104  };
105 
106  template<typename _Callable>
107  struct _Impl : public _Impl_base
108  {
109  _Callable _M_func;
110 
111  _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
112  { }
113 
114  void
115  _M_run() { _M_func(); }
116  };
117 
118  private:
119  id _M_id;
120 
121  public:
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;
127  thread(const thread&&) = delete;
128 
129  thread(thread&& __t) noexcept
130  { swap(__t); }
131 
132  template<typename _Callable, typename... _Args>
133  explicit
134  thread(_Callable&& __f, _Args&&... __args)
135  {
136 #ifdef GTHR_ACTIVE_PROXY
137  // Create a reference to pthread_create, not just the gthr weak symbol
138  _M_start_thread(_M_make_routine(std::__bind_simple(
139  std::forward<_Callable>(__f),
140  std::forward<_Args>(__args)...)),
141  reinterpret_cast<void(*)()>(&pthread_create));
142 #else
143  _M_start_thread(_M_make_routine(std::__bind_simple(
144  std::forward<_Callable>(__f),
145  std::forward<_Args>(__args)...)));
146 #endif
147  }
148 
149  ~thread()
150  {
151  if (joinable())
152  std::terminate();
153  }
154 
155  thread& operator=(const thread&) = delete;
156 
157  thread& operator=(thread&& __t) noexcept
158  {
159  if (joinable())
160  std::terminate();
161  swap(__t);
162  return *this;
163  }
164 
165  void
166  swap(thread& __t) noexcept
167  { std::swap(_M_id, __t._M_id); }
168 
169  bool
170  joinable() const noexcept
171  { return !(_M_id == id()); }
172 
173  void
174  join();
175 
176  void
177  detach();
178 
179  thread::id
180  get_id() const noexcept
181  { return _M_id; }
182 
183  /** @pre thread is joinable
184  */
185  native_handle_type
186  native_handle()
187  { return _M_id._M_thread; }
188 
189  // Returns a value that hints at the number of hardware thread contexts.
190  static unsigned int
191  hardware_concurrency() noexcept;
192 
193  private:
194  void
195  _M_start_thread(__shared_base_type, void (*)());
196 
197  void
198  _M_start_thread(__shared_base_type);
199 
200  template<typename _Callable>
201  shared_ptr<_Impl<_Callable>>
202  _M_make_routine(_Callable&& __f)
203  {
204  // Create and allocate full data structure, not base.
205  return std::make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
206  }
207  };
208 
209  inline thread::_Impl_base::~_Impl_base() = default;
210 
211  inline void
212  swap(thread& __x, thread& __y) noexcept
213  { __x.swap(__y); }
214 
215  inline bool
216  operator!=(thread::id __x, thread::id __y) noexcept
217  { return !(__x == __y); }
218 
219  inline bool
220  operator<=(thread::id __x, thread::id __y) noexcept
221  { return !(__y < __x); }
222 
223  inline bool
224  operator>(thread::id __x, thread::id __y) noexcept
225  { return __y < __x; }
226 
227  inline bool
228  operator>=(thread::id __x, thread::id __y) noexcept
229  { return !(__x < __y); }
230 
231  // DR 889.
232  /// std::hash specialization for thread::id.
233  template<>
234  struct hash<thread::id>
235  : public __hash_base<size_t, thread::id>
236  {
237  size_t
238  operator()(const thread::id& __id) const noexcept
239  { return std::_Hash_impl::hash(__id._M_thread); }
240  };
241 
242  template<class _CharT, class _Traits>
243  inline basic_ostream<_CharT, _Traits>&
244  operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
245  {
246  if (__id == thread::id())
247  return __out << "thread::id of a non-executing thread";
248  else
249  return __out << __id._M_thread;
250  }
251 
252 _GLIBCXX_END_NAMESPACE_VERSION
253 
254  /** @namespace std::this_thread
255  * @brief ISO C++ 2011 entities sub-namespace for thread.
256  * 30.3.2 Namespace this_thread.
257  */
258  namespace this_thread
259  {
260  _GLIBCXX_BEGIN_NAMESPACE_VERSION
261 
262  /// get_id
263  inline thread::id
264  get_id() noexcept { return thread::id(__gthread_self()); }
265 
266  /// yield
267  inline void
268  yield() noexcept
269  {
270 #ifdef _GLIBCXX_USE_SCHED_YIELD
271  __gthread_yield();
272 #endif
273  }
274 
275  void
276  __sleep_for(chrono::seconds, chrono::nanoseconds);
277 
278  /// sleep_for
279  template<typename _Rep, typename _Period>
280  inline void
281  sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
282  {
283  if (__rtime <= __rtime.zero())
284  return;
285  auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
286  auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
287 #ifdef _GLIBCXX_USE_NANOSLEEP
288  __gthread_time_t __ts =
289  {
290  static_cast<std::time_t>(__s.count()),
291  static_cast<long>(__ns.count())
292  };
293  ::nanosleep(&__ts, 0);
294 #else
295  __sleep_for(__s, __ns);
296 #endif
297  }
298 
299  /// sleep_until
300  template<typename _Clock, typename _Duration>
301  inline void
302  sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
303  {
304  auto __now = _Clock::now();
305  if (__now < __atime)
306  sleep_for(__atime - __now);
307  }
308 
309  _GLIBCXX_END_NAMESPACE_VERSION
310  }
311 
312  // @} group threads
313 
314 } // namespace
315 
316 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
317 
318 #endif // C++11
319 
320 #endif // _GLIBCXX_THREAD