#include #include #include #include using namespace std::literals; const auto T = 250ms; 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::osyncstream(std::cout) << "[" << j.get_id() << "]: " << i << "\n"; std::this_thread::sleep_for(T); } } int main() { std::jthread d{f, std::ref(d), "0123456789"}, a{f, std::ref(a), "abcdefghij"}; std::this_thread::sleep_for(2 * T); d.request_stop(); a.join(); }