Skip to content

<thread>: jthread should provide native_handle() #1963

@StephanTLavavej

Description

@StephanTLavavej

jthread defines native_handle_type but not a native_handle() member function:

STL/stl/inc/thread

Lines 291 to 377 in 4c862ee

class jthread {
public:
using id = thread::id;
using native_handle_type = thread::native_handle_type;
jthread() noexcept : _Impl{}, _Ssource{nostopstate} {}
template <class _Fn, class... _Args, enable_if_t<!is_same_v<remove_cvref_t<_Fn>, jthread>, int> = 0>
_NODISCARD_CTOR explicit jthread(_Fn&& _Fx, _Args&&... _Ax) {
if constexpr (is_invocable_v<decay_t<_Fn>, stop_token, decay_t<_Args>...>) {
_Impl._Start(_STD forward<_Fn>(_Fx), _Ssource.get_token(), _STD forward<_Args>(_Ax)...);
} else {
_Impl._Start(_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...);
}
}
~jthread() {
_Try_cancel_and_join();
}
jthread(const jthread&) = delete;
jthread(jthread&&) noexcept = default;
jthread& operator=(const jthread&) = delete;
jthread& operator=(jthread&& _Other) noexcept {
// note: the standard specifically disallows making self-move-assignment a no-op here
// N4861 [thread.jthread.cons]/13
// Effects: If joinable() is true, calls request_stop() and then join(). Assigns the state
// of x to *this and sets x to a default constructed state.
_Try_cancel_and_join();
_Impl = _STD move(_Other._Impl);
_Ssource = _STD move(_Other._Ssource);
return *this;
}
void swap(jthread& _Other) noexcept {
_Impl.swap(_Other._Impl);
_Ssource.swap(_Other._Ssource);
}
_NODISCARD bool joinable() const noexcept {
return _Impl.joinable();
}
void join() {
_Impl.join();
}
void detach() {
_Impl.detach();
}
_NODISCARD id get_id() const noexcept {
return _Impl.get_id();
}
_NODISCARD stop_source get_stop_source() noexcept {
return _Ssource;
}
_NODISCARD stop_token get_stop_token() const noexcept {
return _Ssource.get_token();
}
bool request_stop() noexcept {
return _Ssource.request_stop();
}
friend void swap(jthread& _Lhs, jthread& _Rhs) noexcept {
_Lhs.swap(_Rhs);
}
_NODISCARD static unsigned int hardware_concurrency() noexcept {
return thread::hardware_concurrency();
}
private:
void _Try_cancel_and_join() noexcept {
if (_Impl.joinable()) {
_Ssource.request_stop();
_Impl.join();
}
}
thread _Impl;
stop_source _Ssource;
};

While it's implementation-defined as to whether native_handle() is provided, we should do so - jthread wraps thread so we can just return its native_handle().

Reported as DevCom-1389817 and Microsoft-internal VSO-1306513 / AB#1306513 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions