very new to posting here, so apologies if it’s in the wrong place.
I’m a hobby coder, so my experience is limited and while I learned C++ some years ago, I have to re-learn it periodically. Now is such a time.
I’m trying to understand jthread and it’s associated stop_token mechanism.
I’ve finally managed to get the threads working (using VS 2022 with language standard set to C++ 20). Now though I can’t get them to stop. I can’t work out what I’m doing wrong here as the main thread runs to the end, but the stop_request calls seem to do nothing:
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
class Testing {
int updates{ 0 };
public:
void update(std::stop_token s, int i, char l) {
while (!s.stop_requested()) {
updates++;
std::cout << updates << " " << l << "n";
std::this_thread::sleep_for(std::chrono::milliseconds(i));
}
std::cout << l << " stoppedn";
}
};
int main() {
Testing test_1, test_2;
std::stop_token s_1, s_2;
std::jthread t_1(&Testing::update, &test_1, s_1, 2300, 'A');
std::jthread t_2(&Testing::update, &test_2, s_2, 700, 'B');
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
std::cout << "Main thread donen";
t_1.request_stop();
if (t_1.joinable()) { t_1.join(); }
t_2.request_stop();
if (t_2.joinable()) { t_2.join(); }
return 0;
}
If anyone can help me here I’d be most grateful!