c++11visual-studio-2015stdasyncstd-future

Wake a deferred task object without invoking future<T>.get()


What happens when you call std::future::wait_for on a deferred task object?

Ideally I would like to wake a deferred task but not take the hit of processing the task in the current thread.


Solution

  • A call to async with the launch::deferred policy will not execute the deferred task until the first call to a non-timed waiting function (one that blocks like get). This is spelled out in the language spec, [futures.async] section, paragraph 3.2:

    The first call to a non-timed waiting function (33.6.5) on an asynchronous return object referring to this shared state shall invoke the deferred function in the thread that called the waiting function.

    So this will run the task in the current thread. I suppose you could create another thread, and pass it the task to run.