'Where is std::this_thread for jthread?

Can't figure out where is std::this_thread for jthread?

I have a function that theoretically makes a jthread sleep until a cancellation is requested:

template<typename Rep, typename Period>
void sleep_for(const std::chrono::duration<Rep, Period>& d, const std::stop_token& token)
{
    std::condition_variable cv;

    std::mutex mutex;

    std::unique_lock<std::mutex> lock{ mutex };

    std::stop_callback stop_wait{ token, [&cv]()
    {
        cv.notify_one(); }
    };

    cv.wait_for(lock, d, [&token]()
    {
        return token.stop_requested();
    });
}

How do I call it on jthread?

Theoretically the program below exits within 1 second:

int main()
{
    std::jthread t([]()
    {
        //where do I get `stop_token`?
        sleep_for(std::chrono::seconds(5), std::this_jthread::get_stop_token());
    });

    std::this_thread::sleep_for(std::chrono::seconds(1));

    t.request_stop();

    return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source