This code implements a version of std::async
, but it behaves as if async
were always called with the deferred
launch policy. This function also does not have async
's special future
behavior; the returned future
can be destroyed without ever acquiring its value.
template<typename F>
auto async_deferred(F&& func) -> std::future<decltype(func())>
{
using result_type = decltype(func());
auto promise = std::promise<result_type>();
auto future = promise.get_future();
std::thread(std::bind([=](std::promise<result_type>& promise)
{
try
{
promise.set_value(func());
// Note: Will not work with std::promise<void>. Needs some meta-template programming which is out of scope for this example.
}
catch(...)
{
promise.set_exception(std::current_exception());
}
}, std::move(promise))).detach();
return future;
}