Reputation: 1205
I am trying to do something like this
#include <iostream>
#include <thread>
#include <chrono>
void doWork(char a, std::stop_token st = {}) {
while (!st.stop_requested()) {
std::cout << a << '\n';
}
}
int main() {
std::jthread t1{doWork, 'A'}, // error
t2{doWork, 'B'}; // error
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
But it won't compile on gcc trunk with -std=c++2a
:
/opt/compiler-explorer/gcc-trunk-20200219/include/c++/10.0.1/thread:537:20: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
537 | static_assert(is_invocable_v<decay_t<_Callable>,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
538 | decay_t<_Args>...>,
| ~~~~~~~~~~~~~~~~~~
Is it even possible to do something like this?
I assumed that it should be possible, because I've seen a similar example with lambda (here):
//...
std::jthread t([&ready, &readyMutex, &readyCV] (std::stop_token st) {
while (!stoken.stop_requested()) {
//...
}
});
Upvotes: 3
Views: 1194
Reputation: 170153
You are almost correct in your implementation. The constraint on the invokable is that it accepts a std::stop_token
as a first argument. So switching the order in the declaration of doWork
will sort it out.
void doWork(std::stop_token st, char a) {
while (!st.stop_requested()) {
std::cout << a << '\n';
}
}
The token comes from the library implementation, so its construction is not meant to worry you anyway.
Upvotes: 5