faabric icon indicating copy to clipboard operation
faabric copied to clipboard

Use `std::jthread` for background threads

Open Shillaker opened this issue 3 years ago • 1 comments

We can use std::jthread with a stop_token:

#include <thread>
#include <iostream>
 
using namespace std::literals::chrono_literals;
 
void f(std::stop_token stop_token, int value)
{
    while (!stop_token.stop_requested()) {
        std::cout << value++ << ' ' << std::flush;
        std::this_thread::sleep_for(200ms);
    }
    std::cout << std::endl;
}
 
int main()
{
    std::jthread thread(f, 5); // prints 5 6 7 8... for approximately 3 seconds
    std::this_thread::sleep_for(3s);
    // The destructor of jthread calls request_stop() and join().
}

from https://en.cppreference.com/w/cpp/thread/stop_token

Shillaker avatar Jan 12 '22 07:01 Shillaker

Partially done in #251

csegarragonz avatar Oct 24 '22 08:10 csegarragonz