Boost logo

Boost :

Subject: Re: [boost] [thread] boost::future::then
From: Vladimir Prus (vladimir.prus_at_[hidden])
Date: 2015-10-10 01:57:19


On 09-Oct-15 10:49 PM, Vicente J. Botet Escriba wrote:
> Le 09/10/15 15:44, Vladimir Prus a écrit :
>> On 09-Oct-15 4:02 PM, Mikael Olenfalk wrote:
>>> On Fri, Oct 9, 2015 at 9:43 AM, Vladimir Prus <vladimir.prus_at_[hidden]>
>>> wrote:
>>>
>>>>
>>>> In particular, suppose I have this:
>>>>
>>>> // Thread 1
>>>>
>>>> boost::promise<int> pi;
>>>> boost::future<int> fi = pi.get_future();
>>>>
>>>> // pass pi to thread 2 somehow
>>>>
>>>> fi.then(....);
>>>>
>>>>
>>>> // Thread 2
>>>>
>>>> pi.set_value(100);
>>>>
>>>>
>>>> And I want continuation to be executed in thread 1, assuming that thread 1
>>>> runs Qt event loop,
>>>> allowing me to use QTimer::singleShot, or queued signal, or similar
>>>> mechanism. How would I go
>>>> about making boost::future do it? Has anybody done so?
>> ...
>>> I might have misunderstood the question but I would assume a solution would
>>> be to build boost.thread with executors enabled[1]. And then wrap the QT
>>> event loop in an executor interface[2] and use the
>>> boost::future<T>::then(Executor&, ...) overload.
>>
>> Mikael,
>>
>> thanks for the response. I suppose it would work - although if I need to pass this
>> executor any time I call 'then', it become rather awkward rather quickly.
> Well this is the current interface :)
>> It would
>> be nicer if promise could have an executor, and pass it to future and then
>> to futures returned by 'then', so that I only need to to specify custom behaviour
>> when creating a promise - that can be easily wrapped in a function.

> If your future is created with
> auto f = async(ex, fct)
>
> then the call to
>
> f.then(fct2);
>
> would use the same executor ex.

Vicente,

thanks, that's good to know. What if I don't want to use async, but rather create promise/future
directly? Can I specified executor when creating future?

Suppose I'm in UI thread, and I ask backend thread to do something. I would like for the work to
be done in backend thread, and then for all continuations to be executed in UI thread. Or, more
generically, in whatever thread has initialized the operation.

So, if I implement a "run in UI thread" executor, I don't want async(ex, fct) to execute fct in
UI thread. So, the function must be executed in different thread from all the continuations,
and it would seem I'd need to something set executor on promise for that to work?

- Volodya


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