Where is std::this_thread for jthread?

Dmitriano

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;
}
Oblivion

The jthread constructor accepts a function that takes a std::stop_token as its first argument, which will be passed in by the jthread from its internal stop_source.

Here is an example:

std::jthread t([](std::stop_token stop_token)
{
    while(!stop_token.stop_requested()) {
        //Process data...
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }   
});
std::this_thread::sleep_for(std::chrono::seconds(1));
t.request_stop();

live on Godbolt.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Any reason to use std::thread over std::jthread?

Create new instance of std::thread/std::jthread on every read call

std::this_thread::sleep_for() freezing the program

std::thread class vs std::this_thread namespace in c++?

ThreadSanitizer: double lock of a mutex with std::jthread

What is std::jthread in c++20?

std::condition_variable not working with std::this_thread::sleep_for()

Code generated - usleep vs std::this_thread::sleep_for

Is it okay to use std::this_thread* functions from boost::threads?

What is s in std::this_thread::sleep_for(2s)?

Can std::this_thread::sleep_for() have spurious wakeups?

std::this_thread::sleep_for sleeps for too long

std::this_thread::sleep_for() vs sleep() in main()

Why is `std::this_thread::yield()` 10x slower than `std::this_thread::sleep_for(0s)`?

Printing std::this_thread::get_id() gives "thread::id of a non-executing thread"?

How to make a thread stop excution (eg: std::this_thread::sleep_for) for an accturate interval

std::this_thread::sleep_for sleeps shorter than expected in VS2015

Is std::this_thread::yield any different from sched_yield on linux?

Why doesn't std::this_thread::sleep_for() work in MacOS terminal?

C++11 std::this_thread - How to cancel sleep_until ()?

C++11 std::this_thread::sleep_until() hangs when compiled with GCC 4.8.5

std::this_thread::sleep_until timing is completely off by about a factor of 2, inexplicably

Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for?

Where is the lock for a std::atomic?

Where to use std::span?

How to interrupt boost::this_thread in c++

this_thread::sleep_for sleeps early

Can this_thread::sleep() be interrupted on linux?

Where are std::list elements allocated?