#include #include #include #include using namespace std::literals; auto T = 100ms; void f(const std::jthread &j, std::string_view s) { for (auto i : s) if (j.get_stop_token().stop_requested()) { std::this_thread::yield(); } else { std::ostringstream oss; oss << "[" << j.get_id() << "]: " << i << "\n"; std::cout << oss.str(); std::this_thread::sleep_for(T); } } int main() { std::jthread j[2] = {std::jthread(f, std::ref(j[0]), "01234"), std::jthread(f, std::ref(j[1]), "abcde")}; std::this_thread::sleep_for(2 * T); j[0].request_stop(); j[1].join(); }