libstdc++
thread
Go to the documentation of this file.
1 // <thread> -*- 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 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 #if __cplusplus > 201703L
39 # include <compare> // std::strong_ordering
40 # include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
41 #endif
42 
43 #include <bits/std_thread.h> // std::thread, get_id, yield
44 #include <bits/this_thread_sleep.h> // std::this_thread::sleep_for, sleep_until
45 
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 
50  /**
51  * @defgroup threads Threads
52  * @ingroup concurrency
53  *
54  * Classes for thread support.
55  * @{
56  */
57 
58  // std::thread is defined in <bits/std_thread.h>
59 
60 #if __cpp_lib_three_way_comparison
61  inline strong_ordering
62  operator<=>(thread::id __x, thread::id __y) noexcept
63  { return __x._M_thread <=> __y._M_thread; }
64 #else
65  inline bool
66  operator!=(thread::id __x, thread::id __y) noexcept
67  { return !(__x == __y); }
68 
69  inline bool
70  operator<(thread::id __x, thread::id __y) noexcept
71  {
72  // Pthreads doesn't define any way to do this, so we just have to
73  // assume native_handle_type is LessThanComparable.
74  return __x._M_thread < __y._M_thread;
75  }
76 
77  inline bool
78  operator<=(thread::id __x, thread::id __y) noexcept
79  { return !(__y < __x); }
80 
81  inline bool
82  operator>(thread::id __x, thread::id __y) noexcept
83  { return __y < __x; }
84 
85  inline bool
86  operator>=(thread::id __x, thread::id __y) noexcept
87  { return !(__x < __y); }
88 #endif // __cpp_lib_three_way_comparison
89 
90  template<class _CharT, class _Traits>
91  inline basic_ostream<_CharT, _Traits>&
92  operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
93  {
94  if (__id == thread::id())
95  return __out << "thread::id of a non-executing thread";
96  else
97  return __out << __id._M_thread;
98  }
99 
100 #ifdef __cpp_lib_jthread
101 
102 #ifndef __STRICT_ANSI__
103  template<typename _Callable, typename... _Args>
104  constexpr bool __pmf_expects_stop_token = false;
105 
106  template<typename _Callable, typename _Obj, typename... _Args>
107  constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
108  = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
109  is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
110 #endif
111 
112  /// A thread that can be requested to stop and automatically joined.
113  class jthread
114  {
115  public:
116  using id = thread::id;
117  using native_handle_type = thread::native_handle_type;
118 
119  jthread() noexcept
120  : _M_stop_source{nostopstate}
121  { }
122 
123  template<typename _Callable, typename... _Args,
124  typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
125  jthread>>>
126  explicit
127  jthread(_Callable&& __f, _Args&&... __args)
128  : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
129  std::forward<_Args>(__args)...)}
130  { }
131 
132  jthread(const jthread&) = delete;
133  jthread(jthread&&) noexcept = default;
134 
135  ~jthread()
136  {
137  if (joinable())
138  {
139  request_stop();
140  join();
141  }
142  }
143 
144  jthread&
145  operator=(const jthread&) = delete;
146 
147  jthread&
148  operator=(jthread&& __other) noexcept
149  {
150  std::jthread(std::move(__other)).swap(*this);
151  return *this;
152  }
153 
154  void
155  swap(jthread& __other) noexcept
156  {
157  std::swap(_M_stop_source, __other._M_stop_source);
158  std::swap(_M_thread, __other._M_thread);
159  }
160 
161  [[nodiscard]] bool
162  joinable() const noexcept
163  {
164  return _M_thread.joinable();
165  }
166 
167  void
168  join()
169  {
170  _M_thread.join();
171  }
172 
173  void
174  detach()
175  {
176  _M_thread.detach();
177  }
178 
179  [[nodiscard]] id
180  get_id() const noexcept
181  {
182  return _M_thread.get_id();
183  }
184 
185  [[nodiscard]] native_handle_type
186  native_handle()
187  {
188  return _M_thread.native_handle();
189  }
190 
191  [[nodiscard]] static unsigned
192  hardware_concurrency() noexcept
193  {
194  return thread::hardware_concurrency();
195  }
196 
197  [[nodiscard]] stop_source
198  get_stop_source() noexcept
199  {
200  return _M_stop_source;
201  }
202 
203  [[nodiscard]] stop_token
204  get_stop_token() const noexcept
205  {
206  return _M_stop_source.get_token();
207  }
208 
209  bool request_stop() noexcept
210  {
211  return _M_stop_source.request_stop();
212  }
213 
214  friend void swap(jthread& __lhs, jthread& __rhs) noexcept
215  {
216  __lhs.swap(__rhs);
217  }
218 
219  private:
220  template<typename _Callable, typename... _Args>
221  static thread
222  _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
223  {
224 #ifndef __STRICT_ANSI__
225  if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
226  return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
227  else
228 #endif
229  if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
230  decay_t<_Args>...>)
231  return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
232  std::forward<_Args>(__args)...};
233  else
234  {
235  static_assert(is_invocable_v<decay_t<_Callable>,
236  decay_t<_Args>...>,
237  "std::jthread arguments must be invocable after"
238  " conversion to rvalues");
239  return thread{std::forward<_Callable>(__f),
240  std::forward<_Args>(__args)...};
241  }
242  }
243 
244 #ifndef __STRICT_ANSI__
245  template<typename _Callable, typename _Obj, typename... _Args>
246  static thread
247  _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
248  _Args&&... __args)
249  {
250  return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
251  std::forward<_Args>(__args)...};
252  }
253 #endif
254 
255  stop_source _M_stop_source;
256  thread _M_thread;
257  };
258 #endif // __cpp_lib_jthread
259 
260  /// @} group threads
261 
262 _GLIBCXX_END_NAMESPACE_VERSION
263 } // namespace
264 #endif // C++11
265 #endif // _GLIBCXX_THREAD