1 // <thread> -*- C++ -*-
3 // Copyright (C) 2008-2020 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 #include <chrono> // std::chrono::*
40 #if __cplusplus > 201703L
41 # include <compare> // std::strong_ordering
42 # include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
45 #include <bits/std_thread.h> // std::thread, get_id, yield
46 #include <bits/functional_hash.h> // std::hash
48 #ifdef _GLIBCXX_USE_NANOSLEEP
49 # include <cerrno> // errno, EINTR
50 # include <time.h> // nanosleep
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 * @defgroup threads Threads
59 * @ingroup concurrency
61 * Classes for thread support.
65 // std::thread is defined in <bits/std_thread.h>
67 #if __cpp_lib_three_way_comparison
68 inline strong_ordering
69 operator<=>(thread::id __x, thread::id __y) noexcept
70 { return __x._M_thread <=> __y._M_thread; }
73 operator!=(thread::id __x, thread::id __y) noexcept
74 { return !(__x == __y); }
77 operator<(thread::id __x, thread::id __y) noexcept
79 // Pthreads doesn't define any way to do this, so we just have to
80 // assume native_handle_type is LessThanComparable.
81 return __x._M_thread < __y._M_thread;
85 operator<=(thread::id __x, thread::id __y) noexcept
86 { return !(__y < __x); }
89 operator>(thread::id __x, thread::id __y) noexcept
93 operator>=(thread::id __x, thread::id __y) noexcept
94 { return !(__x < __y); }
95 #endif // __cpp_lib_three_way_comparison
98 /// std::hash specialization for thread::id.
100 struct hash<thread::id>
101 : public __hash_base<size_t, thread::id>
104 operator()(const thread::id& __id) const noexcept
105 { return std::_Hash_impl::hash(__id._M_thread); }
108 template<class _CharT, class _Traits>
109 inline basic_ostream<_CharT, _Traits>&
110 operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
112 if (__id == thread::id())
113 return __out << "thread::id of a non-executing thread";
115 return __out << __id._M_thread;
118 /** @namespace std::this_thread
119 * @brief ISO C++ 2011 namespace for interacting with the current thread
121 * C++11 30.3.2 [thread.thread.this] Namespace this_thread.
123 namespace this_thread
126 __sleep_for(chrono::seconds, chrono::nanoseconds);
128 /// this_thread::sleep_for
129 template<typename _Rep, typename _Period>
131 sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
133 if (__rtime <= __rtime.zero())
135 auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
136 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
137 #ifdef _GLIBCXX_USE_NANOSLEEP
138 struct ::timespec __ts =
140 static_cast<std::time_t>(__s.count()),
141 static_cast<long>(__ns.count())
143 while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
146 __sleep_for(__s, __ns);
150 /// this_thread::sleep_until
151 template<typename _Clock, typename _Duration>
153 sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
155 #if __cplusplus > 201703L
156 static_assert(chrono::is_clock_v<_Clock>);
158 auto __now = _Clock::now();
159 if (_Clock::is_steady)
162 sleep_for(__atime - __now);
165 while (__now < __atime)
167 sleep_for(__atime - __now);
168 __now = _Clock::now();
173 #ifdef __cpp_lib_jthread
175 /// A thread that can be requested to stop and automatically joined.
179 using id = thread::id;
180 using native_handle_type = thread::native_handle_type;
183 : _M_stop_source{nostopstate}
186 template<typename _Callable, typename... _Args,
187 typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
190 jthread(_Callable&& __f, _Args&&... __args)
191 : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
192 std::forward<_Args>(__args)...)}
195 jthread(const jthread&) = delete;
196 jthread(jthread&&) noexcept = default;
208 operator=(const jthread&) = delete;
211 operator=(jthread&& __other) noexcept
213 std::jthread(std::move(__other)).swap(*this);
218 swap(jthread& __other) noexcept
220 std::swap(_M_stop_source, __other._M_stop_source);
221 std::swap(_M_thread, __other._M_thread);
225 joinable() const noexcept
227 return _M_thread.joinable();
243 get_id() const noexcept
245 return _M_thread.get_id();
248 [[nodiscard]] native_handle_type
251 return _M_thread.native_handle();
254 [[nodiscard]] static unsigned
255 hardware_concurrency() noexcept
257 return thread::hardware_concurrency();
260 [[nodiscard]] stop_source
261 get_stop_source() noexcept
263 return _M_stop_source;
266 [[nodiscard]] stop_token
267 get_stop_token() const noexcept
269 return _M_stop_source.get_token();
272 bool request_stop() noexcept
274 return _M_stop_source.request_stop();
277 friend void swap(jthread& __lhs, jthread& __rhs) noexcept
283 template<typename _Callable, typename... _Args>
285 _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
287 if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
289 return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
290 std::forward<_Args>(__args)...};
293 static_assert(is_invocable_v<decay_t<_Callable>,
295 "std::thread arguments must be invocable after"
296 " conversion to rvalues");
297 return thread{std::forward<_Callable>(__f),
298 std::forward<_Args>(__args)...};
302 stop_source _M_stop_source;
305 #endif // __cpp_lib_jthread
309 _GLIBCXX_END_NAMESPACE_VERSION
312 #endif // _GLIBCXX_THREAD