c*******y 发帖数: 1630 | 3 void do_sim(vector &rnumber, size_t run_bg, run_ed){
for(size_t i=run_bg;i!=run_ed;++i){
rnumber.at(i) = rand();
}
trailnum = 100;
vector rnumber(100, 0.);
numOfThreads = boost::thread::hardware_concurrency();
unsigned int perTrail = trailnum/numOfThreads;
unsigned int run_bg, run_ed = 0;
for(unsigned int i = 0; i!=numOfThreads-1; ++i){
run_bg = i*perTrail;
run_ed = (i+1)*perTrail;
threads[i] = boost::thread(do_sim, boost::ref(rnumber), run_bg, run_ed);
}
run_bg = run_ed+1;
run_ed = trailnum;
threads[numOfThreads-1] = boost::thread(do_sim, boost::ref(rnumber), run_bg,
run_ed);
for_each(threads.begin(), threads.end(), boost::mem_fn(&boost::thread::join)
);
return std::accumulate(rnumber.begin(), rnumber.end(), 0.)/rnumber.size();
【在 m**c 的大作中提到】 : 本人新手,比如simulate 100个随机数计算均值。 : 要用 multiple cores 怎么做paraell processing呢? mvp吗? : 用 multiple threading怎么做呢?boost里面的thread怎么做呢 : 哪个牛人给个简单的小例子可以吗?多谢。
|