With C++20 you can use std::jthread (“Cooperatively Interruptible Joining Thread”), which does the stop request and the joining in its destructor.
jthread::~jthread() { if (joinable()) // Not joined/detached yet { request_stop(); // Inform to stop asap join(); // Wait until finished } }