libstdc++
std_thread.h
Go to the documentation of this file.
1 // std::thread declarations -*- C++ -*-
2 
3 // Copyright (C) 2008-2022 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 bits/std_thread.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{thread}
28  */
29 
30 #ifndef _GLIBCXX_THREAD_H
31 #define _GLIBCXX_THREAD_H 1
32 
33 #pragma GCC system_header
34 
35 #if __cplusplus >= 201103L
36 #include <bits/c++config.h>
37 
38 #include <iosfwd> // std::basic_ostream
39 #include <tuple> // std::tuple
40 #include <bits/functional_hash.h> // std::hash
41 #include <bits/invoke.h> // std::__invoke
42 #include <bits/refwrap.h> // not required, but helpful to users
43 #include <bits/unique_ptr.h> // std::unique_ptr
44 
45 #ifdef _GLIBCXX_HAS_GTHREADS
46 # include <bits/gthr.h>
47 #else
48 # include <errno.h>
49 # include <bits/functexcept.h>
50 #endif
51 
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 
56  /** @addtogroup threads
57  * @{
58  */
59 
60  /// thread
61  class thread
62  {
63  public:
64 #ifdef _GLIBCXX_HAS_GTHREADS
65  // Abstract base class for types that wrap arbitrary functors to be
66  // invoked in the new thread of execution.
67  struct _State
68  {
69  virtual ~_State();
70  virtual void _M_run() = 0;
71  };
73 
74  using native_handle_type = __gthread_t;
75 #else
76  using native_handle_type = int;
77 #endif
78 
79  /// thread::id
80  class id
81  {
82  native_handle_type _M_thread;
83 
84  public:
85  id() noexcept : _M_thread() { }
86 
87  explicit
88  id(native_handle_type __id) : _M_thread(__id) { }
89 
90  private:
91  friend class thread;
92  friend struct hash<id>;
93 
94  friend bool
95  operator==(id __x, id __y) noexcept;
96 
97 #if __cpp_lib_three_way_comparison
98  friend strong_ordering
99  operator<=>(id __x, id __y) noexcept;
100 #else
101  friend bool
102  operator<(id __x, id __y) noexcept;
103 #endif
104 
105  template<class _CharT, class _Traits>
108  };
109 
110  private:
111  id _M_id;
112 
113  // _GLIBCXX_RESOLVE_LIB_DEFECTS
114  // 2097. packaged_task constructors should be constrained
115  // 3039. Unnecessary decay in thread and packaged_task
116  template<typename _Tp>
117  using __not_same = __not_<is_same<__remove_cvref_t<_Tp>, thread>>;
118 
119  public:
120  thread() noexcept = default;
121 
122 #ifdef _GLIBCXX_HAS_GTHREADS
123  template<typename _Callable, typename... _Args,
124  typename = _Require<__not_same<_Callable>>>
125  explicit
126  thread(_Callable&& __f, _Args&&... __args)
127  {
128  static_assert( __is_invocable<typename decay<_Callable>::type,
129  typename decay<_Args>::type...>::value,
130  "std::thread arguments must be invocable after conversion to rvalues"
131  );
132 
133 #ifdef GTHR_ACTIVE_PROXY
134  // Create a reference to pthread_create, not just the gthr weak symbol.
135  auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
136 #else
137  auto __depend = nullptr;
138 #endif
139  using _Wrapper = _Call_wrapper<_Callable, _Args...>;
140  // Create a call wrapper with DECAY_COPY(__f) as its target object
141  // and DECAY_COPY(__args)... as its bound argument entities.
142  _M_start_thread(_State_ptr(new _State_impl<_Wrapper>(
143  std::forward<_Callable>(__f), std::forward<_Args>(__args)...)),
144  __depend);
145  }
146 #endif // _GLIBCXX_HAS_GTHREADS
147 
148  ~thread()
149  {
150  if (joinable())
151  std::__terminate();
152  }
153 
154  thread(const thread&) = delete;
155 
156  thread(thread&& __t) noexcept
157  { swap(__t); }
158 
159  thread& operator=(const thread&) = delete;
160 
161  thread& operator=(thread&& __t) noexcept
162  {
163  if (joinable())
164  std::__terminate();
165  swap(__t);
166  return *this;
167  }
168 
169  void
170  swap(thread& __t) noexcept
171  { std::swap(_M_id, __t._M_id); }
172 
173  bool
174  joinable() const noexcept
175  { return !(_M_id == id()); }
176 
177  void
178  join();
179 
180  void
181  detach();
182 
183  id
184  get_id() const noexcept
185  { return _M_id; }
186 
187  /** @pre thread is joinable
188  */
189  native_handle_type
191  { return _M_id._M_thread; }
192 
193  // Returns a value that hints at the number of hardware thread contexts.
194  static unsigned int
195  hardware_concurrency() noexcept;
196 
197 #ifdef _GLIBCXX_HAS_GTHREADS
198  private:
199  template<typename _Callable>
200  struct _State_impl : public _State
201  {
202  _Callable _M_func;
203 
204  template<typename... _Args>
205  _State_impl(_Args&&... __args)
206  : _M_func(std::forward<_Args>(__args)...)
207  { }
208 
209  void
210  _M_run() { _M_func(); }
211  };
212 
213  void
214  _M_start_thread(_State_ptr, void (*)());
215 
216 #if _GLIBCXX_THREAD_ABI_COMPAT
217  public:
218  struct _Impl_base;
219  typedef shared_ptr<_Impl_base> __shared_base_type;
220  struct _Impl_base
221  {
222  __shared_base_type _M_this_ptr;
223  virtual ~_Impl_base() = default;
224  virtual void _M_run() = 0;
225  };
226 
227  private:
228  void
229  _M_start_thread(__shared_base_type, void (*)());
230 
231  void
232  _M_start_thread(__shared_base_type);
233 #endif
234 
235  private:
236  // A call wrapper that does INVOKE(forwarded tuple elements...)
237  template<typename _Tuple>
238  struct _Invoker
239  {
240  template<typename... _Args>
241  explicit
242  _Invoker(_Args&&... __args)
243  : _M_t(std::forward<_Args>(__args)...)
244  { }
245 
246  _Tuple _M_t;
247 
248  template<typename>
249  struct __result;
250  template<typename _Fn, typename... _Args>
251  struct __result<tuple<_Fn, _Args...>>
252  : __invoke_result<_Fn, _Args...>
253  { };
254 
255  template<size_t... _Ind>
256  typename __result<_Tuple>::type
257  _M_invoke(_Index_tuple<_Ind...>)
258  { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
259 
260  typename __result<_Tuple>::type
261  operator()()
262  {
263  using _Indices
264  = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
265  return _M_invoke(_Indices());
266  }
267  };
268 
269  public:
270  template<typename... _Tp>
271  using _Call_wrapper = _Invoker<tuple<typename decay<_Tp>::type...>>;
272 #endif // _GLIBCXX_HAS_GTHREADS
273  };
274 
275 #ifndef _GLIBCXX_HAS_GTHREADS
276  inline void thread::join() { std::__throw_system_error(EINVAL); }
277  inline void thread::detach() { std::__throw_system_error(EINVAL); }
278  inline unsigned int thread::hardware_concurrency() noexcept { return 0; }
279 #endif
280 
281  inline void
282  swap(thread& __x, thread& __y) noexcept
283  { __x.swap(__y); }
284 
285  inline bool
286  operator==(thread::id __x, thread::id __y) noexcept
287  {
288  // pthread_equal is undefined if either thread ID is not valid, so we
289  // can't safely use __gthread_equal on default-constructed values (nor
290  // the non-zero value returned by this_thread::get_id() for
291  // single-threaded programs using GNU libc). Assume EqualityComparable.
292  return __x._M_thread == __y._M_thread;
293  }
294 
295  // N.B. other comparison operators are defined in <thread>
296 
297  // DR 889.
298  /// std::hash specialization for thread::id.
299  template<>
300  struct hash<thread::id>
301  : public __hash_base<size_t, thread::id>
302  {
303  size_t
304  operator()(const thread::id& __id) const noexcept
305  { return std::_Hash_impl::hash(__id._M_thread); }
306  };
307 
308  namespace this_thread
309  {
310  /// this_thread::get_id
311  inline thread::id
312  get_id() noexcept
313  {
314 #ifndef _GLIBCXX_HAS_GTHREADS
315  return thread::id(1);
316 #elif defined _GLIBCXX_NATIVE_THREAD_ID
317  return thread::id(_GLIBCXX_NATIVE_THREAD_ID);
318 #else
319  return thread::id(__gthread_self());
320 #endif
321  }
322 
323  /// this_thread::yield
324  inline void
325  yield() noexcept
326  {
327 #if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
328  __gthread_yield();
329 #endif
330  }
331 
332  } // namespace this_thread
333 
334  /// @}
335 
336 _GLIBCXX_END_NAMESPACE_VERSION
337 } // namespace
338 #endif // C++11
339 
340 #endif // _GLIBCXX_THREAD_H
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition: invoke.h:90
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1540
void yield() noexcept
this_thread::yield
Definition: std_thread.h:325
thread::id get_id() noexcept
this_thread::get_id
Definition: std_thread.h:312
Template class basic_ostream.
Definition: ostream:59
Primary class template hash.
thread
Definition: std_thread.h:62
native_handle_type native_handle()
Definition: std_thread.h:190
thread::id
Definition: std_thread.h:81
A move-only smart pointer that manages unique ownership of a resource.
Definition: unique_ptr.h:270