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
52namespace 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 _Tuple _M_t;
241
242 template<typename>
243 struct __result;
244 template<typename _Fn, typename... _Args>
245 struct __result<tuple<_Fn, _Args...>>
246 : __invoke_result<_Fn, _Args...>
247 { };
248
249 template<size_t... _Ind>
250 typename __result<_Tuple>::type
251 _M_invoke(_Index_tuple<_Ind...>)
252 { return std::__invoke(std::get<_Ind>(std::move(_M_t))...); }
253
254 typename __result<_Tuple>::type
255 operator()()
256 {
257 using _Indices
258 = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type;
259 return _M_invoke(_Indices());
260 }
261 };
262
263 public:
264 template<typename... _Tp>
265 using _Call_wrapper = _Invoker<tuple<typename decay<_Tp>::type...>>;
266#endif // _GLIBCXX_HAS_GTHREADS
267 };
268
269#ifndef _GLIBCXX_HAS_GTHREADS
270 inline void thread::join() { std::__throw_system_error(EINVAL); }
271 inline void thread::detach() { std::__throw_system_error(EINVAL); }
272 inline unsigned int thread::hardware_concurrency() noexcept { return 0; }
273#endif
274
275 inline void
276 swap(thread& __x, thread& __y) noexcept
277 { __x.swap(__y); }
278
279 inline bool
280 operator==(thread::id __x, thread::id __y) noexcept
281 {
282 // pthread_equal is undefined if either thread ID is not valid, so we
283 // can't safely use __gthread_equal on default-constructed values (nor
284 // the non-zero value returned by this_thread::get_id() for
285 // single-threaded programs using GNU libc). Assume EqualityComparable.
286 return __x._M_thread == __y._M_thread;
287 }
288
289 // N.B. other comparison operators are defined in <thread>
290
291 // DR 889.
292 /// std::hash specialization for thread::id.
293 template<>
294 struct hash<thread::id>
295 : public __hash_base<size_t, thread::id>
296 {
297 size_t
298 operator()(const thread::id& __id) const noexcept
299 { return std::_Hash_impl::hash(__id._M_thread); }
300 };
301
302 namespace this_thread
303 {
304 /// this_thread::get_id
305 inline thread::id
306 get_id() noexcept
307 {
308#ifndef _GLIBCXX_HAS_GTHREADS
309 return thread::id(1);
310#elif defined _GLIBCXX_NATIVE_THREAD_ID
311 return thread::id(_GLIBCXX_NATIVE_THREAD_ID);
312#else
313 return thread::id(__gthread_self());
314#endif
315 }
316
317 /// this_thread::yield
318 inline void
319 yield() noexcept
320 {
321#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
322 __gthread_yield();
323#endif
324 }
325
326 } // namespace this_thread
327
328 /// @}
329
330_GLIBCXX_END_NAMESPACE_VERSION
331} // namespace
332#endif // C++11
333
334#endif // _GLIBCXX_THREAD_H
constexpr bool operator==(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition: chrono.h:744
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
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
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
void swap(shared_lock< _Mutex > &__x, shared_lock< _Mutex > &__y) noexcept
Swap specialization for shared_lock.
Definition: shared_mutex:851
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:319
thread::id get_id() noexcept
this_thread::get_id
Definition: std_thread.h:306
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