3// Copyright (C) 2008-2021 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
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 /// A thread that can be requested to stop and automatically joined.
106 using id = thread::id;
107 using native_handle_type = thread::native_handle_type;
110 : _M_stop_source{nostopstate}
113 template<typename _Callable, typename... _Args,
114 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
117 jthread(_Callable&& __f, _Args&&... __args)
118 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
119 std::forward<_Args>(__args)...)}
122 jthread(const jthread&) = delete;
123 jthread(jthread&&) noexcept = default;
135 operator=(const jthread&) = delete;
138 operator=(jthread&& __other) noexcept
140 std::jthread(std::move(__other)).swap(*this);
145 swap(jthread& __other) noexcept
147 std::swap(_M_stop_source, __other._M_stop_source);
148 std::swap(_M_thread, __other._M_thread);
152 joinable() const noexcept
154 return _M_thread.joinable();
170 get_id() const noexcept
172 return _M_thread.get_id();
175 [[nodiscard]] native_handle_type
178 return _M_thread.native_handle();
181 [[nodiscard]] static unsigned
182 hardware_concurrency() noexcept
184 return thread::hardware_concurrency();
187 [[nodiscard]] stop_source
188 get_stop_source() noexcept
190 return _M_stop_source;
193 [[nodiscard]] stop_token
194 get_stop_token() const noexcept
196 return _M_stop_source.get_token();
199 bool request_stop() noexcept
201 return _M_stop_source.request_stop();
204 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
210 template<typename _Callable, typename... _Args>
212 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
214 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
216 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
217 std::forward<_Args>(__args)...};
220 static_assert(is_invocable_v<decay_t<_Callable>,
222 "std::thread arguments must be invocable after"
223 " conversion to rvalues");
224 return thread{std::forward<_Callable>(__f),
225 std::forward<_Args>(__args)...};
229 stop_source _M_stop_source;
232#endif // __cpp_lib_jthread
236_GLIBCXX_END_NAMESPACE_VERSION
239#endif // _GLIBCXX_THREAD