Boost logo

Boost :

Subject: [boost] [thread] How to return a lazy future ?
From: Ivan Le Lann (ivan.lelann_at_[hidden])
Date: 2011-10-12 12:35:48


Hi,

Boost.Thread documentation has a section regarding the creation of lazy futures.
http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.futures.lazy_futures

The code on this page runs fine, but I can't find a way to return such future.
Naïve code below exits with a boost::broken_promise in unique_future::get().

boost::unique_future <int> create_future()
{
    boost::packaged_task<int> task(calculate_the_answer_to_life_the_universe_and_everything);
    task.set_wait_callback(invoke_lazy_task);
    return task.get_future();
}

int main()
{
        assert(create_future().get()==42);
}

Exceptional finish is marked in packaged_task destructor.
And regardless of exceptions, since set_wait_callback want something
that operates on packaged_task and not on pointed task_base,
the code is doomed to do an access violation.

So, is there any way to return lazy futures ?

Regards,
Ivan


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