#include<iostream>
#include<future>
#include<thread>
#include<vector>
#include<numeric> //accumulate
using namespace std;
double accum(double* beg,double* end,double init){
return accumulate(beg,end,init);
}
double comp2(vector<double>&v){
using Task_type=double(double*,double*,double);
packaged_task<Task_type> pt0{accum};
packaged_task<Task_type> pt1{accum};
future<double> f0{pt0.get_future()};
future<double> f1{pt1.get_future()};
double* first=&v[0];
thread t0{move(pt0),first,first+v.size()/2,0};
thread t1{move(pt1),first+v.size()/2,first+v.size(),0};
t0.join();
t1.join();
return f0.get()+f1.get();
}
int main(){
vector<double> vd{1,2,3,4,5,6,7};
cout<<comp2(vd)<<endl;
system("pause");
return 0;
}
运行结果
PS D:\Codes\test> g++ test.cpp
PS D:\Codes\test> ./a.exe
28
请按任意键继续. . .
网友评论