packaged_task:
double accum(double∗ beg, double ∗ end, double init)
// compute the sum of [beg:end) starting with the initial value init
{
return accumulate(beg,end,init);
}
double comp2(vector<double>& v)
{
using Task_type = double(double∗,double∗,double);
// type of task
packaged_task<Task_type> pt0 {accum};
packaged_task<Task_type> pt1 {accum}; // package the task (i.e., accum)
future<double> f0 {pt0.get_future()};
future<double> f1 {pt1.get_future()}; // get hold of pt0’s future
// get hold of pt1’s future
double∗ first = &v[0];
thread t1 {move(pt0),first,first+v.siz e()/2,0};
thread t2 {move(pt1),first+v.siz e()/2,first+v.siz e(),0}; // star t a thread for pt0
// star t a thread for pt1
// ...
return f0.get()+f1.g et();
// get the results
}
async:
double comp4(vector<double>& v)
// spawn many tasks if v is large enough
{
if (v.siz e()<10000) return accum(v.begin(),v.end(),0.0);
auto v0 = &v[0];
auto sz = v.size();
auto f0 = async(accum,v0,v0+sz/4,0.0);// first quarter
auto f1 = async(accum,v0+sz/4,v0+sz/2,0.0);// second quarter
auto f2 = async(accum,v0+sz/2,v0+sz∗3/4,0.0);// third quarter
auto f3 = async(accum,v0+sz∗3/4,v0+sz,0.0);// four th quar ter
return f0.get()+f1.g et()+f2.get()+f3.get(); // collect and combine the results
}