#include #include #include #include #include #include int main() { int sum = 0; { std::jthread worker([&] { for (int i = 1; i <= 100; ++i) sum += i; }); } assert(sum == 5050); std::mutex mutex; std::condition_variable_any changed; bool cancelled = false; std::jthread waiter([&](std::stop_token token) { std::unique_lock lock(mutex); const bool ready = changed.wait(lock, token, [] { return false; }); cancelled = !ready && token.stop_requested(); }); waiter.request_stop(); waiter.join(); assert(cancelled); assert(!waiter.joinable()); std::cout << sum << ' ' << cancelled << '\n'; }