File indexing completed on 2026-05-03 08:13:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___THREAD_JTHREAD_H
0011 #define _LIBCPP___CXX03___THREAD_JTHREAD_H
0012
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__functional/invoke.h>
0015 #include <__cxx03/__stop_token/stop_source.h>
0016 #include <__cxx03/__stop_token/stop_token.h>
0017 #include <__cxx03/__thread/support.h>
0018 #include <__cxx03/__thread/thread.h>
0019 #include <__cxx03/__type_traits/decay.h>
0020 #include <__cxx03/__type_traits/is_constructible.h>
0021 #include <__cxx03/__type_traits/is_same.h>
0022 #include <__cxx03/__type_traits/remove_cvref.h>
0023 #include <__cxx03/__utility/forward.h>
0024 #include <__cxx03/__utility/move.h>
0025
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 # pragma GCC system_header
0028 #endif
0029
0030 _LIBCPP_PUSH_MACROS
0031 #include <__cxx03/__undef_macros>
0032
0033 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
0034
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036
0037 class _LIBCPP_AVAILABILITY_SYNC jthread {
0038 public:
0039
0040 using id = thread::id;
0041 using native_handle_type = thread::native_handle_type;
0042
0043
0044 _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
0045
0046 template <class _Fun, class... _Args>
0047 _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
0048 requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
0049 : __stop_source_(),
0050 __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
0051 static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
0052 static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
0053 static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
0054 is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
0055 }
0056
0057 _LIBCPP_HIDE_FROM_ABI ~jthread() {
0058 if (joinable()) {
0059 request_stop();
0060 join();
0061 }
0062 }
0063
0064 jthread(const jthread&) = delete;
0065
0066 _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
0067
0068 jthread& operator=(const jthread&) = delete;
0069
0070 _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
0071 if (this != &__other) {
0072 if (joinable()) {
0073 request_stop();
0074 join();
0075 }
0076 __stop_source_ = std::move(__other.__stop_source_);
0077 __thread_ = std::move(__other.__thread_);
0078 }
0079
0080 return *this;
0081 }
0082
0083
0084 _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
0085 std::swap(__stop_source_, __other.__stop_source_);
0086 std::swap(__thread_, __other.__thread_);
0087 }
0088
0089 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
0090
0091 _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
0092
0093 _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
0094
0095 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
0096
0097 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
0098
0099
0100 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
0101
0102 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
0103
0104 _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
0105
0106
0107 _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
0108
0109
0110 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
0111 return thread::hardware_concurrency();
0112 }
0113
0114 private:
0115 template <class _Fun, class... _Args>
0116 _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
0117 if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
0118 return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
0119 } else {
0120 return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
0121 }
0122 }
0123
0124 stop_source __stop_source_;
0125 thread __thread_;
0126 };
0127
0128 _LIBCPP_END_NAMESPACE_STD
0129
0130 #endif
0131
0132 _LIBCPP_POP_MACROS
0133
0134 #endif