Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-05-20 10:10:15

Hash: SHA1

On Monday 19 May 2008 22:22 pm, Dean Michael Berris wrote:
> For the special container types I've been pondering, aside from
> automatically waiting on contained futures there's the possibility of
> making standard algorithms more specialized for these container types
> (or via tag dispatch).
> Perhaps having a special std::transform() implementation (the five
> argument version) that does a 'wait on any' future in the container
> and in a non-deterministic order apply a function on the contained
> future value that becomes available, and the result mapped to the
> appropriate location in the resulting container.

That's interesting. I can do that with ordinary containers/std::transform by
using a poet::active_function from libpoet as the binary function passed to
the transform algorithm. The active function's scheduler performs each add
operation as the futures become ready. Example code attached.

Version: GnuPG v1.4.6 (GNU/Linux)


Content-Type: text/x-c++src;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;

// Copyright (C) Frank Mori Hess 2007-2008
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at

#include <cmath>
#include <functional>
#include <iostream>
#include <poet/active_function.hpp>
#include <vector>

int main()
        unsigned i;
        const unsigned vector_length = 10;
        // input data, assigned actual values later
        std::vector<poet::promise<double> > x_promises;
        std::vector<poet::promise<double> > y_promises;
        // we push the promises on onto the std::vector one at a time to make
        // sure the std::vector doesn't copy-construct them from each other.
        for(i = 0; i < vector_length; ++i)

        // active_functions
        std::plus<double> passive_adder;
        poet::active_function<double (double, double)> active_adder(passive_adder);

        std::vector<poet::future<double> > x;
        std::copy(x_promises.begin(), x_promises.end(), std::back_inserter(x));
        std::vector<poet::future<double> > y;
        std::copy(y_promises.begin(), y_promises.end(), std::back_inserter(y));
        std::vector<poet::future<double> > sum;

        std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(sum), active_adder);

        // fulfill input promises
        for(i = 0; i < x_promises.size(); ++i)
       % 4);
       % 3);

        // wait for futures to become ready and convert them to values
        for(i = 0; i < sum.size(); ++i)
                std::cout << "sum " << i << " = " << << "\n";

        return 0;


Boost list run by bdawes at, gregod at, cpdaniel at, john at