std::jthread

cppreference.com

Example

foo: () = {
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
bar: () = {
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

main: () = {
    std::cout << "starting first helper...\n";
    helper1: std::jthread = (foo);
 
    std::cout << "starting second helper...\n";
    helper2: std::jthread = (bar);
 
    std::cout << "waiting for helpers to finish..." << std::endl;
    helper1.join();
    helper2.join();
 
    std::cout << "done!\n";
}

Output

starting first helper...
starting second helper...
waiting for helpers to finish...
done!

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.