1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |         template<class T, class V> struct Accum  {  // simple accumulator function object
            T* b;
            T* e;
            V val;
            Accum(T* bb, T* ee, const V& v) : b{bb}, e{ee}, val{vv} {}
            V operator() () { return std::accumulate(b,e,val); }
        };
        double comp(vector<double>& v)
            // spawn many tasks if v is large enough
        {
            if (v.size()<10000) return std::accumulate(v.begin(),v.end(),0.0);
            auto f0 {async(Accum{&v[0],&v[v.size()/4],0.0})};
            auto f1 {async(Accum{&v[v.size()/4],&v[v.size()/2],0.0})};
            auto f2 {async(Accum{&v[v.size()/2],&v[v.size()*3/4],0.0})};
            auto f3 {async(Accum{&v[v.size()*3/4],&v[v.size()],0.0})};
            return f0.get()+f1.get()+f2.get()+f3.get();
        } | 
Partager