std::accumulate异常安全的两个并行版本
来源:互联网 发布:平民护肤品推荐 知乎 编辑:程序博客网 时间:2024/05/16 18:11
为了避免异常发生而导致的内存泄漏通常有两种方法解决:
1、采用try 和catch
2、在一个对象的析构函数中检测,线程是否join。(毕竟这是C++中惯用的清除资源方法)
本文主要实现第二种:
class join_threads{std::vector<std::thread>& threads;public:explicit join_threads(std::vector<std::thread>& threads_): threads(threads_){}~join_threads(){for (unsigned long i = 0; i < threads.size(); ++i){if (threads[i].joinable())threads[i].join();}}};
采用std::future和std::promise实现的std::accumulate异常安全并行版本:
template<typename Iterator,typename T>struct accumulate_block{ T operator()(Iterator first,Iterator last) { return std::accumulate(first,last,T()); }};template<typename Iterator,typename T>T parallel_accumulate(Iterator first,Iterator last,T init){ unsigned long const length=std::distance(first,last); if(!length) return init; unsigned long const min_per_thread=25; unsigned long const max_threads= (length+min_per_thread-1)/min_per_thread; unsigned long const hardware_threads= std::thread::hardware_concurrency(); unsigned long const num_threads= std::min(hardware_threads!=0?hardware_threads:2,max_threads); unsigned long const block_size=length/num_threads; std::vector<std::future<T> > futures(num_threads-1); std::vector<std::thread> threads(num_threads-1); join_threads joiner(threads); Iterator block_start=first; for(unsigned long i=0;i<(num_threads-1);++i) { Iterator block_end=block_start; std::advance(block_end,block_size); std::packaged_task<T(Iterator,Iterator)> task( accumulate_block<Iterator,T>()); futures[i]=task.get_future(); threads[i]=std::thread(std::move(task),block_start,block_end); block_start=block_end; } T last_result=accumulate_block()(block_start,last); T result=init; for(unsigned long i=0;i<(num_threads-1);++i) { result+=futures[i].get(); } result += last_result; return result;}
使用std::async实现的std::accumulate异常安全并行版本:
template<typename Iterator,typename T>T parallel_accumulate(Iterator first,Iterator last,T init){ unsigned long const length=std::distance(first,last); unsigned long const max_chunk_size=25; if(length<=max_chunk_size) { return std::accumulate(first,last,init); } else { Iterator mid_point=first; std::advance(mid_point,length/2); std::future<T> first_half_result= std::async(parallel_accumulate<Iterator,T>, first,mid_point,init); T second_half_result=parallel_accumulate(mid_point,last,T()); return first_half_result.get()+second_half_result; }}
0 0
- std::accumulate异常安全的两个并行版本
- C++并发实战5:并行化的std::accumulate
- std::transform || inner_product || accumulate
- std::accumulate(容器求和)
- C++异常安全之std:auto_ptr
- c++ std - accumulate 求数组和
- bitonic_sort 串行(递归和for循环)和并行(cuda)两个版本的代码实现
- accumulate的用法
- accumulate的用法
- "accumulate"的思考
- std 异常
- accumulate
- accumulate
- accumulate
- 一个线程安全的std::map封装
- c++11,std::find的并行化模板化
- 【C++ STL应用与实现】86: 如何使用std::accumulate
- c++之std::accumulate连续操作如累加累积等
- Gap Locks 区间锁
- HDU 1702 ACboy needs your help again! (模拟栈和队列)
- 不能低头干活,更要抬头看路
- Android拍照及选择图片及裁剪及兼容6.0权限实现
- mysql 配置
- std::accumulate异常安全的两个并行版本
- android端升级下载后如何自动安装并且有提示安装完成或打开,然后点击打开
- Ubuntu下面安装单节点hadoop
- Mybatis缓存
- 拼音工具类
- 线程的交换与移动
- 2016-10-19 今天试验的代码段
- ios中修改字体
- BNU19907 UVA11489 Integer Game