Boost logo

Boost :

Subject: Re: [boost] [thread] request for interest in new c++1y features
From: Bjorn Reese (breese_at_[hidden])
Date: 2012-10-21 09:05:55


On 2012-10-20 17:47, Vicente J. Botet Escriba wrote:
> Le 20/10/12 15:45, Bjorn Reese a écrit :

>> It is unclear to me, however, what happens if the async call returns
>> before .then is hooked up. Will the .then function be executed in the
>> calling thread?
>>
> Yes, as I understand it then() checks if the future is already ready as
> otherwise it will never be notified of the change and call then to the
> continuation function on the calling thread. Do you see any problems
> with this approach?

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.

Evan pointed out that this can be handled with a custom scheduler,
although I must admit that I am uncertain how. Anyways, having to
define a scheduler seems like a like a complex workaround to address
this problem.

My preferred solution would be to first call std::async with
std::launch::deferred, then set up the future.then continuations, and
finally start the execution. This would require an extension to
std::future (e.g. a future.launch function) that starts the execution
without blocking, as future.wait or future.get does.


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