1 // <thread> -*- C++ -*-
3 // Copyright (C) 2008-2022 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/>.
25 /** @file include/thread
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
38 #if __cplusplus > 201703L
39 # include <compare> // std::strong_ordering
40 # include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
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
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 * @defgroup threads Threads
52 * @ingroup concurrency
54 * Classes for thread support.
58 // std::thread is defined in <bits/std_thread.h>
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; }
66 operator!=(thread::id __x, thread::id __y) noexcept
67 { return !(__x == __y); }
70 operator<(thread::id __x, thread::id __y) noexcept
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;
78 operator<=(thread::id __x, thread::id __y) noexcept
79 { return !(__y < __x); }
82 operator>(thread::id __x, thread::id __y) noexcept
86 operator>=(thread::id __x, thread::id __y) noexcept
87 { return !(__x < __y); }
88 #endif // __cpp_lib_three_way_comparison
90 template<class _CharT, class _Traits>
91 inline basic_ostream<_CharT, _Traits>&
92 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
94 if (__id == thread::id())
95 return __out << "thread::id of a non-executing thread";
97 return __out << __id._M_thread;
100 #ifdef __cpp_lib_jthread
102 #ifndef __STRICT_ANSI__
103 template<typename _Callable, typename... _Args>
104 constexpr bool __pmf_expects_stop_token = false;
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;
112 /// A thread that can be requested to stop and automatically joined.
116 using id = thread::id;
117 using native_handle_type = thread::native_handle_type;
120 : _M_stop_source{nostopstate}
123 template<typename _Callable, typename... _Args,
124 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
127 jthread(_Callable&& __f, _Args&&... __args)
128 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
129 std::forward<_Args>(__args)...)}
132 jthread(const jthread&) = delete;
133 jthread(jthread&&) noexcept = default;
145 operator=(const jthread&) = delete;
148 operator=(jthread&& __other) noexcept
150 std::jthread(std::move(__other)).swap(*this);
155 swap(jthread& __other) noexcept
157 std::swap(_M_stop_source, __other._M_stop_source);
158 std::swap(_M_thread, __other._M_thread);
162 joinable() const noexcept
164 return _M_thread.joinable();
180 get_id() const noexcept
182 return _M_thread.get_id();
185 [[nodiscard]] native_handle_type
188 return _M_thread.native_handle();
191 [[nodiscard]] static unsigned
192 hardware_concurrency() noexcept
194 return thread::hardware_concurrency();
197 [[nodiscard]] stop_source
198 get_stop_source() noexcept
200 return _M_stop_source;
203 [[nodiscard]] stop_token
204 get_stop_token() const noexcept
206 return _M_stop_source.get_token();
209 bool request_stop() noexcept
211 return _M_stop_source.request_stop();
214 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
220 template<typename _Callable, typename... _Args>
222 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
224 #ifndef __STRICT_ANSI__
225 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
226 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
229 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
231 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
232 std::forward<_Args>(__args)...};
235 static_assert(is_invocable_v<decay_t<_Callable>,
237 "std::jthread arguments must be invocable after"
238 " conversion to rvalues");
239 return thread{std::forward<_Callable>(__f),
240 std::forward<_Args>(__args)...};
244 #ifndef __STRICT_ANSI__
245 template<typename _Callable, typename _Obj, typename... _Args>
247 _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
250 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
251 std::forward<_Args>(__args)...};
255 stop_source _M_stop_source;
258 #endif // __cpp_lib_jthread
262 _GLIBCXX_END_NAMESPACE_VERSION
265 #endif // _GLIBCXX_THREAD