thread 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // <thread> -*- C++ -*-
  2. // Copyright (C) 2008-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/thread
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_THREAD
  24. #define _GLIBCXX_THREAD 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #if __cplusplus > 201703L
  30. # include <compare> // std::strong_ordering
  31. # include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
  32. #endif
  33. #include <bits/std_thread.h> // std::thread, get_id, yield
  34. #include <bits/this_thread_sleep.h> // std::this_thread::sleep_for, sleep_until
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. /**
  39. * @defgroup threads Threads
  40. * @ingroup concurrency
  41. *
  42. * Classes for thread support.
  43. * @{
  44. */
  45. // std::thread is defined in <bits/std_thread.h>
  46. #if __cpp_lib_three_way_comparison
  47. inline strong_ordering
  48. operator<=>(thread::id __x, thread::id __y) noexcept
  49. { return __x._M_thread <=> __y._M_thread; }
  50. #else
  51. inline bool
  52. operator!=(thread::id __x, thread::id __y) noexcept
  53. { return !(__x == __y); }
  54. inline bool
  55. operator<(thread::id __x, thread::id __y) noexcept
  56. {
  57. // Pthreads doesn't define any way to do this, so we just have to
  58. // assume native_handle_type is LessThanComparable.
  59. return __x._M_thread < __y._M_thread;
  60. }
  61. inline bool
  62. operator<=(thread::id __x, thread::id __y) noexcept
  63. { return !(__y < __x); }
  64. inline bool
  65. operator>(thread::id __x, thread::id __y) noexcept
  66. { return __y < __x; }
  67. inline bool
  68. operator>=(thread::id __x, thread::id __y) noexcept
  69. { return !(__x < __y); }
  70. #endif // __cpp_lib_three_way_comparison
  71. template<class _CharT, class _Traits>
  72. inline basic_ostream<_CharT, _Traits>&
  73. operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
  74. {
  75. if (__id == thread::id())
  76. return __out << "thread::id of a non-executing thread";
  77. else
  78. return __out << __id._M_thread;
  79. }
  80. #ifdef __cpp_lib_jthread
  81. /// A thread that can be requested to stop and automatically joined.
  82. class jthread
  83. {
  84. public:
  85. using id = thread::id;
  86. using native_handle_type = thread::native_handle_type;
  87. jthread() noexcept
  88. : _M_stop_source{nostopstate}
  89. { }
  90. template<typename _Callable, typename... _Args,
  91. typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
  92. jthread>>>
  93. explicit
  94. jthread(_Callable&& __f, _Args&&... __args)
  95. : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
  96. std::forward<_Args>(__args)...)}
  97. { }
  98. jthread(const jthread&) = delete;
  99. jthread(jthread&&) noexcept = default;
  100. ~jthread()
  101. {
  102. if (joinable())
  103. {
  104. request_stop();
  105. join();
  106. }
  107. }
  108. jthread&
  109. operator=(const jthread&) = delete;
  110. jthread&
  111. operator=(jthread&& __other) noexcept
  112. {
  113. std::jthread(std::move(__other)).swap(*this);
  114. return *this;
  115. }
  116. void
  117. swap(jthread& __other) noexcept
  118. {
  119. std::swap(_M_stop_source, __other._M_stop_source);
  120. std::swap(_M_thread, __other._M_thread);
  121. }
  122. [[nodiscard]] bool
  123. joinable() const noexcept
  124. {
  125. return _M_thread.joinable();
  126. }
  127. void
  128. join()
  129. {
  130. _M_thread.join();
  131. }
  132. void
  133. detach()
  134. {
  135. _M_thread.detach();
  136. }
  137. [[nodiscard]] id
  138. get_id() const noexcept
  139. {
  140. return _M_thread.get_id();
  141. }
  142. [[nodiscard]] native_handle_type
  143. native_handle()
  144. {
  145. return _M_thread.native_handle();
  146. }
  147. [[nodiscard]] static unsigned
  148. hardware_concurrency() noexcept
  149. {
  150. return thread::hardware_concurrency();
  151. }
  152. [[nodiscard]] stop_source
  153. get_stop_source() noexcept
  154. {
  155. return _M_stop_source;
  156. }
  157. [[nodiscard]] stop_token
  158. get_stop_token() const noexcept
  159. {
  160. return _M_stop_source.get_token();
  161. }
  162. bool request_stop() noexcept
  163. {
  164. return _M_stop_source.request_stop();
  165. }
  166. friend void swap(jthread& __lhs, jthread& __rhs) noexcept
  167. {
  168. __lhs.swap(__rhs);
  169. }
  170. private:
  171. template<typename _Callable, typename... _Args>
  172. static thread
  173. _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
  174. {
  175. if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
  176. decay_t<_Args>...>)
  177. return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
  178. std::forward<_Args>(__args)...};
  179. else
  180. {
  181. static_assert(is_invocable_v<decay_t<_Callable>,
  182. decay_t<_Args>...>,
  183. "std::thread arguments must be invocable after"
  184. " conversion to rvalues");
  185. return thread{std::forward<_Callable>(__f),
  186. std::forward<_Args>(__args)...};
  187. }
  188. }
  189. stop_source _M_stop_source;
  190. thread _M_thread;
  191. };
  192. #endif // __cpp_lib_jthread
  193. /// @} group threads
  194. _GLIBCXX_END_NAMESPACE_VERSION
  195. } // namespace
  196. #endif // C++11
  197. #endif // _GLIBCXX_THREAD