Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-05-25 14:02:37


Johan Torp:
> Frank Mori Hess wrote:
>>
>> 1) In order to wait on an arbitrary number of futures, determined at
>> runtime,
>> we need some kind of container.
>
> Yes, but it needn't be exposed to users. As you suggested, it can be built
> up with free functions or expressive templates, similar to Gaskill's comb.

Suppose that the programmer wants to spawn n tasks, where n is not a
compile-time constant, and is only interested in whatever task returns a
value first. Something like:

    T f( int x );

    // ...

    vector<future<T>> v;

    for( int i = 0; i < n; ++i )
    {
        v.push_back( async( f, i ) );
    }

    // wait for any of v[i] to complete, get the T

How could this be rephrased with the container being an implementation
detail?

Here's one way:

    future<T> ft;

    for( int i = 0; i < n; ++i )
    {
        ft = ft || async( f, i );
    }

    T t = ft.get();

This however relies on:

    future<T> operator|| ( future<T>, future<T> );

You can't use 'comb' as the return value.


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