c++11,线程池之二--有等待线程池中任务完成功能的线程池
来源:互联网 发布:淘宝单号购买平台 编辑:程序博客网 时间:2024/05/27 21:48
#include <deque>#include <future>#include <memory>#include <functional>#include <iostream>#include <iostream>class function_wrapper{ struct impl_base { virtual void call()=0; virtual ~impl_base() {} }; std::unique_ptr<impl_base> impl; template<typename F> struct impl_type: impl_base { F f; impl_type(F&& f_): f(std::move(f_)) {} void call() { f(); } };public: template<typename F> function_wrapper(F&& f): impl(new impl_type<F>(std::move(f))) {} void call() { impl->call(); } function_wrapper(function_wrapper&& other): impl(std::move(other.impl)) {} function_wrapper& operator=(function_wrapper&& other) { impl=std::move(other.impl); return *this; } function_wrapper(const function_wrapper&)=delete; function_wrapper(function_wrapper&)=delete; function_wrapper& operator=(const function_wrapper&)=delete;};
class thread_pool{public: std::deque<function_wrapper> work_queue; template<typename FunctionType> std::future<typename std::result_of<FunctionType()>::type> submit(FunctionType f) { typedef typename std::result_of<FunctionType()>::type result_type; std::packaged_task<result_type()> task(std::move(f)); std::future<result_type> res(task.get_future()); work_queue.push_back(std::move(task)); return res; } public: thread_pool(): joiner(threads) { done.store(false); unsigned const thread_count=std::thread::hardware_concurrency(); try { for(unsigned i=0;i<thread_count;++i) { threads.push_back( std::thread(&thread_pool::worker_thread,this)); } } catch(...) { done=true; throw; } } ~thread_pool() { done=true; } template<typename FunctionType> void submit(FunctionType f) { work_queue.push(std::function<void()>(f)); } };
1 0
- c++11,线程池之二--有等待线程池中任务完成功能的线程池
- 如何等待java线程池中所有任务完成
- 如何等待java线程池中所有任务完成
- Java主线程等待线程池所有任务完成--线程池的封装
- 主线程等待线程池所有任务完成
- java线程池主线程等待子线程执行完成
- java线程池主线程等待子线程执行完成
- Java主线程等待子线程、线程池完成
- 为uThreadPool增加线程池等待功能
- java线程池中任务执行完成后再次执行
- 完成端口线程池及任务接口
- 线程池中ScheduledExecutorService定时器功能(二)
- Java多线程并发之 fork jion ,不同线程之间互帮互助,最快完成线程池任务。
- Spring 线程池,完成系统需要的异步任务
- java 等待线程/线程池执行完毕
- 线程实用解析--------(二)创建调用有参函数的线程和线程池简介
- 线程实用解析---------(二)创建调用有参函数的线程和线程池简介
- 线程池概念 二、测试线程池如何执行任务
- caj转pdf超简单!
- RecyclerView+Cardview学习探索
- CentOS修改系统时间
- 连接String与Int(转)
- hdoj 第几天?
- c++11,线程池之二--有等待线程池中任务完成功能的线程池
- 朴素贝叶斯算法理解
- 【IOS】IOS OpenCV之HelloWorld
- ContentProvider实现数据共享1
- 滤波电容、去耦电容、旁路电容作用
- 随意细解:UI -- 标签视图控制器
- UIImagePickerController详解
- [LeetCode]002-AddTwoNumber
- Android调用系统相机方法及保存照片方式