What is std::async and how does it differ from creating a std::thread directly?
Last updated
std::asyncruns a callable and returns astd::future. Callingget()yields the result or rethrows the task's exception. A rawstd::threadhas no return channel and an uncaught exception callsstd::terminate.Launch policy:
std::launch::asyncforces a new thread, whilestd::launch::deferredruns lazily atget(). The default may pick either, so be explicit.A
std::threadmust bejoin()ed ordetach()ed or the program terminates. The async future instead blocks in its destructor until the task finishes.