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
46namespace std _GLIBCXX_VISIBILITY(default)
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
51 * @defgroup threads Threads
52 * @ingroup concurrency
55 * Classes for thread support.
59 // std::thread is defined in <bits/std_thread.h>
61 /// @relates std::thread::id @{
63#if __cpp_lib_three_way_comparison
64 inline strong_ordering
65 operator<=>(thread::id __x, thread::id __y) noexcept
66 { return __x._M_thread <=> __y._M_thread; }
69 operator!=(thread::id __x, thread::id __y) noexcept
70 { return !(__x == __y); }
73 operator<(thread::id __x, thread::id __y) noexcept
75 // Pthreads doesn't define any way to do this, so we just have to
76 // assume native_handle_type is LessThanComparable.
77 return __x._M_thread < __y._M_thread;
81 operator<=(thread::id __x, thread::id __y) noexcept
82 { return !(__y < __x); }
85 operator>(thread::id __x, thread::id __y) noexcept
89 operator>=(thread::id __x, thread::id __y) noexcept
90 { return !(__x < __y); }
91#endif // __cpp_lib_three_way_comparison
93 template<class _CharT, class _Traits>
94 inline basic_ostream<_CharT, _Traits>&
95 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
97 if (__id == thread::id())
98 return __out << "thread::id of a non-executing thread";
100 return __out << __id._M_thread;
104#ifdef __cpp_lib_jthread
106 /// @cond undocumented
107#ifndef __STRICT_ANSI__
108 template<typename _Callable, typename... _Args>
109 constexpr bool __pmf_expects_stop_token = false;
111 template<typename _Callable, typename _Obj, typename... _Args>
112 constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
113 = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
114 is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
118 /** A thread with cancellation and automatic joining.
120 * Unlike `std::thread`, destroying a joinable `std::jthread` will not
121 * terminate the process. Instead, it will try to request its thread to
122 * stop, then will join it.
124 * A `std::jthread` has a `std::stop_source` member which will be passed
125 * as the first argument to the callable that runs in the new thread
126 * (as long as the callable will accept that argument). That can then
127 * be used to send a stop request that the new thread can test for.
135 using id = thread::id;
136 using native_handle_type = thread::native_handle_type;
139 : _M_stop_source{nostopstate}
142 template<typename _Callable, typename... _Args,
143 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
146 jthread(_Callable&& __f, _Args&&... __args)
147 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
148 std::forward<_Args>(__args)...)}
151 jthread(const jthread&) = delete;
152 jthread(jthread&&) noexcept = default;
164 operator=(const jthread&) = delete;
167 operator=(jthread&& __other) noexcept
169 std::jthread(std::move(__other)).swap(*this);
174 swap(jthread& __other) noexcept
176 std::swap(_M_stop_source, __other._M_stop_source);
177 std::swap(_M_thread, __other._M_thread);
181 joinable() const noexcept
183 return _M_thread.joinable();
199 get_id() const noexcept
201 return _M_thread.get_id();
204 [[nodiscard]] native_handle_type
207 return _M_thread.native_handle();
210 [[nodiscard]] static unsigned
211 hardware_concurrency() noexcept
213 return thread::hardware_concurrency();
216 [[nodiscard]] stop_source
217 get_stop_source() noexcept
219 return _M_stop_source;
222 [[nodiscard]] stop_token
223 get_stop_token() const noexcept
225 return _M_stop_source.get_token();
228 bool request_stop() noexcept
230 return _M_stop_source.request_stop();
233 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
239 template<typename _Callable, typename... _Args>
241 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
243#ifndef __STRICT_ANSI__
244 if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
245 return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
248 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
250 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
251 std::forward<_Args>(__args)...};
254 static_assert(is_invocable_v<decay_t<_Callable>,
256 "std::jthread arguments must be invocable after"
257 " conversion to rvalues");
258 return thread{std::forward<_Callable>(__f),
259 std::forward<_Args>(__args)...};
263#ifndef __STRICT_ANSI__
264 template<typename _Callable, typename _Obj, typename... _Args>
266 _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
269 return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
270 std::forward<_Args>(__args)...};
274 stop_source _M_stop_source;
277#endif // __cpp_lib_jthread
281_GLIBCXX_END_NAMESPACE_VERSION
284#endif // _GLIBCXX_THREAD