https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100612
--- Comment #1 from Jonathan O'Connor <jonathan.oconnor at protonmail dot com>
---
The std::jthread constructor does not support taking a pointer to a member
function that has, as a first argument, a std::stop_token.
In C++20, the new jthread class can accept a std::stop_token to aid in stopping
a thread cooperatively. Unfortunately, the library code in gcc 10.2.0 and all
later versions (according to godbolt.org) do not allow a member function taking
a stop_token to be used to initialize a jthread.
The problem is due to the std::jthread::_S_create() method which can't handle
my desired case.
In a private email discussion with Nico Josuttis, his opinion was this should
be allowed by the standard:
"well, my rough understanding is that as INVOKE is called
(see 20.9.2 Requirements [func.require] in the Standard),
you can always pass a member function with the object to call the member
function for as next argument.
That should work for both thread and jthread."
The fix should be relatively simple, involving a further if constexpr check for
the Callable being a member function. I'll try and make a patch, and append it
here.
#include <thread>
struct ThreadObj {
void withoutStopToken() {}
void withStopToken(std::stop_token st) {}
};
int main() {
ThreadObj obj;
// The following line causes an error. The other example compile fine.
std::jthread t1{&ThreadObj::withStopToken, &obj};
std::jthread t2{&ThreadObj::withoutStopToken, &obj};
return 0;
}