Detached async example

suggest change

This code starts the work on a detached thread immediately. It does not use std::launch::deferred, which would run the function on the thread that waits for the result. The returned future can be destroyed without waiting, unlike a future from std::async.

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;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents