Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
StephanTLavavej opened this issue Jun 8, 2021 · 2 comments · Fixed by #1966
Closed

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

StephanTLavavej opened this issue Jun 8, 2021 · 2 comments · Fixed by #1966
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@StephanTLavavej
Copy link
Member

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 .

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Jun 8, 2021
@bhardwajs
Copy link
Contributor

Do you already have a taker? If not, I can send a PR.

@StephanTLavavej
Copy link
Member Author

No takers yet - go ahead! 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants