当前位置: 首页 > news >正文

创造有价值的网站福州网站建设好的公司

创造有价值的网站,福州网站建设好的公司,网站设计便宜,深圳成交型网站建设1. thread对象的析构问题 在 C 多线程标准库中#xff0c;创建 thread 对象后#xff0c;必须在对象析构前决定是 detach 还是 join。若在 thread 对象销毁时仍未做出决策#xff0c;程序将会终止。 然而#xff0c;在创建 thread 对象后、调用 join 前的代码中#xff… 1. thread对象的析构问题 在 C 多线程标准库中创建 thread 对象后必须在对象析构前决定是 detach 还是 join。若在 thread 对象销毁时仍未做出决策程序将会终止。 然而在创建 thread 对象后、调用 join 前的代码中若程序抛出异常就会跳过 join 的调用进而导致程序终止。 因此必须在异常捕获中也调用 join。 这无疑增加了编程的复杂性因为每个相关位置都需要在正常流程中写一次 join在异常捕获中再写一次。 下面的代码将演示这一情况 #include iostream #include threadusing namespace std;void threadFunc() {cout Hello from thread endl; }int main() {thread t(threadFunc);try{throw runtime_error(Something went wrong);}catch (...){t.join();throw;}t.join(); } 2. 一种简单的解决办法——RAII 一种简单的解决办法就是使用RAII思想编写一个类来绑定一个thread对象在类的析构函数中调用thread对象的join方法。 下面的代码展示了这一点 #include iostream #include threadusing namespace std;class thread_guard { public:thread_guard(std::thread t) : t_(t) {}~thread_guard(){if (t_.joinable()){t_.join();}}thread_guard(const thread_guard) delete;thread_guard operator(const thread_guard) delete; private:thread t_; };void threadFunc() {cout Thread function running... endl; }int main() {thread t(threadFunc);thread_guard g(t);return 0; } 局部对象会自动被销毁在销毁时thread_guard类对象的析构函数会自动调用thread类对象的join方法从而保证thread不会异常终止。 但是这种方法太死板了只会调用join方法。 我们可能希望自己选择detach或者join也可能想要在thread对象销毁时做一些别的事情。 出于这种想法本文提出了一种可扩展的智能析构线程下面将对其进行介绍。 3. 可扩展的智能析构线程 首先对于thread对象析构时不同的处理这里使用了策略模式。通过提供不同的策略类就可以扩展出不同的析构行为。 同时目前实现的策略类没有自己的成员函数所以采用了单例模式来创建避免创建出大量相同的对象而造成内存浪费。 最后通过简单工厂模式来获取策略类。 下面展示一下具体的代码 #include iostream #include threadusing namespace std;class thread_destroy_strategy { public:virtual void destroy(thread t)const 0;virtual ~thread_destroy_strategy() default; };class join_strategy : public thread_destroy_strategy { public:static join_strategy* getInstance(){static join_strategy instance;return instance;}void destroy(thread t)const override{if (t.joinable()){t.join();cout Thread this_thread::get_id() joined endl;}} };class detach_strategy : public thread_destroy_strategy { public:static detach_strategy* getInstance(){static detach_strategy instance;return instance;}void destroy(thread t)const override{if (t.joinable()){t.detach();cout Thread this_thread::get_id() detached endl;}} };enum class EThreadStrategy {JOIN,DETACH };class strategyFactory { public:static thread_destroy_strategy* getStrategy(EThreadStrategy strategy){switch (strategy){case EThreadStrategy::JOIN:return join_strategy::getInstance();case EThreadStrategy::DETACH:return detach_strategy::getInstance();default:return nullptr;}} };class auto_thread { public:templatetypename F, typename... Argsauto_thread(F f, Args... args) : t(forwardF(f), forwardArgs(args)...) {}~auto_thread(){thread_destroy_strategy* pStrategy strategyFactory::getStrategy(strategy);if (pStrategy){pStrategy-destroy(t);}}auto_thread(const auto_thread) delete;auto_thread operator(const auto_thread) delete;public:void setStrategy(EThreadStrategy strategy_){strategy strategy_;} private:thread t;EThreadStrategy strategy EThreadStrategy::JOIN; };void threadFunc() {cout Hello from thread endl; }int main() {auto_thread t(threadFunc);t.setStrategy(EThreadStrategy::JOIN); // 默认就是JOIN策略, 也可以设置为DETACH策略 } 策略类在destroy时打印了一下线程id。 运行结果如下图所示 以上就是本文的全部内容
http://www.hkea.cn/news/14301627/

相关文章:

  • 专业做企业活动的趴网站wordpress 判断加密
  • 如何选择做pc端网站移动宽带过期了怎么续费
  • 免费兼职一单一结合肥百度seo代理
  • 网站优化要多少钱做个app好还是做网站好
  • 信息公开网站建设大连门户网站开发
  • 网站建设 部署与发布 答案wordpress文章显示摘要
  • 网站建设风格有哪些wordpress图片小程序
  • 怎么建立网站 个人进一步优化落实
  • 兼职做网站平台天津公司网站的建设
  • 网站psd设计稿seo做的好的网站有哪些
  • 搭建三合一网站网页设计的交流网站
  • 网站引导页怎么设置免费域名注册查询入口
  • 铁道部建设监理协会网站网站开发移动app
  • python工程打包供网站开发调用衡水冀县做网站
  • 网站建设与管理认识wordpress静态文件目录下
  • 做标记网站网站及管理系统
  • 即时设计网站网站开发美工绩效考核
  • 软件网站建设公司做甜品网站栏目
  • 怎么做外贸企业网站fotor懒设计
  • 建设企业网站体会vi视觉识别设计公司
  • 专门做红酒的网站深圳手机商城网站设计费用
  • 旅游电子商务网站建设规划书免费网站服务器安全
  • 做产品推广得网站注册qq空间申请
  • 中国建设很行河北省分行合作网站百度收录怎么弄
  • 如何在百度网站收录提交入口办公空间设计说明200字
  • 高端的网站建设公司中策大数据工程信息网
  • 做一个网站的价格html5做网站的总结
  • 合肥做网站的价格网站开发科普书
  • 教育网站建设收费现在济南可以正常出入吗
  • 做外国订单有什么网站太原网站建设平台