Boost logo

Boost :

Subject: Re: [boost] [thread] request for interest in new c++1y features
From: Evan Wallace (onlyone_at_[hidden])
Date: 2012-10-21 09:41:56


Bjorn Reese <breese <at> mail1.stofanet.dk> writes:
> The main problem is the duration of the continuation. If the function
> passed to std::async finishes before we call future.then, then the
> continuation will be executed by the calling thread. This means that
> the calling thread is blocked while the continuation is executed, which
> is a problem if the continuation is slow. Then we might as well call
> the two functions synchroneously.

My understanding is that the continuation will be *launched from* the
calling thread, but the thread on which the continuation is *actually
run* is determined by the launch policy.
Perhaps an example will help to explain what I mean by this:

#include <future>
using namespace std;
int main() {
    //Compute some value on a different thread.
    //The launch policy defaults to launch::async | launch::deferred
    future<int> f1 = async([]() { return 123; });

    //Call wait() to ensure that this example hits the situation
    //where the .then call occurs after the future is already
    //available.
    f1.wait();

    //The continuation will be run (as if) by std::async,
    //and so will not block the current thread if an
    //appropriate launch policy is selected.
    //The launch policy is inherited from the original
    //async call (unless explicitly overridden).
    future<int> f2 = f1.then([](future<int> f) {
        return expensive_computation(f.get());
    });
}

Evan Wallace


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk