template <typename Iterator,typename Func>
void parallel_for_each(Iterator first,Iterator last,Func f)
{
unsigned long const length = std::distance(first,last);
if(!length)
return;
unsigned long const min_per_thread = 25;
if(length < (2 * min_per_thread))
std::for_each(first,last,f);
else
{
Iterator const mid_point = first + length/2;
std::future<void> first_half = std::async(¶llel_for_each<Iterator,Func>,first,mid_point,f);
parallel_for_each(mid_point,last,f);
first_half.get();
}
}
c++11,for_each的并行化,改写模板
最新推荐文章于 2024-09-15 00:15:00 发布