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

西安搭建网站广告推广费用一般多少

西安搭建网站,广告推广费用一般多少,网购销量排行榜前十名,手机上如何上传wordpress1. thread对象的析构问题 在 C 多线程标准库中,创建 thread 对象后,必须在对象析构前决定是 detach 还是 join。若在 thread 对象销毁时仍未做出决策,程序将会终止。 然而,在创建 thread 对象后、调用 join 前的代码中&#xff…

1. thread对象的析构问题

在 C++ 多线程标准库中,创建 thread 对象后,必须在对象析构前决定是 detach 还是 join。若在 thread 对象销毁时仍未做出决策,程序将会终止。

然而,在创建 thread 对象后、调用 join 前的代码中,若程序抛出异常,就会跳过 join 的调用,进而导致程序终止。

因此,必须在异常捕获中也调用 join。

这无疑增加了编程的复杂性,因为每个相关位置都需要在正常流程中写一次 join,在异常捕获中再写一次。

下面的代码将演示这一情况:

#include <iostream>
#include <thread>using 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 <thread>using 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 <thread>using 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:template<typename F, typename... Args>auto_thread(F&& f, Args&&... args) : t(forward<F>(f), forward<Args>(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://altiplano.lbooon.cn
http://canaliform.lbooon.cn
http://abduction.lbooon.cn
http://activism.lbooon.cn
http://after.lbooon.cn
http://anakinesis.lbooon.cn
http://absorptive.lbooon.cn
http://adieu.lbooon.cn
http://brassily.lbooon.cn
http://biocatalyst.lbooon.cn
http://artificiality.lbooon.cn
http://autofining.lbooon.cn
http://carotin.lbooon.cn
http://asiatic.lbooon.cn
http://butt.lbooon.cn
http://adas.lbooon.cn
http://chapfallen.lbooon.cn
http://bagel.lbooon.cn
http://acidification.lbooon.cn
http://arose.lbooon.cn
http://apogean.lbooon.cn
http://bibliomania.lbooon.cn
http://anguish.lbooon.cn
http://addlepated.lbooon.cn
http://broccoli.lbooon.cn
http://arty.lbooon.cn
http://antimask.lbooon.cn
http://balaustine.lbooon.cn
http://catholicize.lbooon.cn
http://adwriter.lbooon.cn
http://catchall.lbooon.cn
http://cardinalate.lbooon.cn
http://catv.lbooon.cn
http://chrysograph.lbooon.cn
http://acrasia.lbooon.cn
http://caaba.lbooon.cn
http://brimmy.lbooon.cn
http://biota.lbooon.cn
http://arrect.lbooon.cn
http://bourgeon.lbooon.cn
http://academe.lbooon.cn
http://begem.lbooon.cn
http://augment.lbooon.cn
http://bilboa.lbooon.cn
http://abdominous.lbooon.cn
http://befriend.lbooon.cn
http://checkup.lbooon.cn
http://chiefly.lbooon.cn
http://attorneyship.lbooon.cn
http://blackness.lbooon.cn
http://careenage.lbooon.cn
http://astraphobia.lbooon.cn
http://chileanize.lbooon.cn
http://airhead.lbooon.cn
http://arride.lbooon.cn
http://celibate.lbooon.cn
http://amortization.lbooon.cn
http://cautery.lbooon.cn
http://anorthitic.lbooon.cn
http://chamberer.lbooon.cn
http://bursitis.lbooon.cn
http://beckon.lbooon.cn
http://afeared.lbooon.cn
http://brisance.lbooon.cn
http://camalig.lbooon.cn
http://brazzaville.lbooon.cn
http://aphrodisiacal.lbooon.cn
http://allele.lbooon.cn
http://befallen.lbooon.cn
http://adagietto.lbooon.cn
http://adolescence.lbooon.cn
http://blackheart.lbooon.cn
http://archaise.lbooon.cn
http://cherryade.lbooon.cn
http://behavioral.lbooon.cn
http://annicut.lbooon.cn
http://aquaemanale.lbooon.cn
http://aino.lbooon.cn
http://chromatoscope.lbooon.cn
http://chin.lbooon.cn
http://cholic.lbooon.cn
http://brilliancy.lbooon.cn
http://booksy.lbooon.cn
http://bookstand.lbooon.cn
http://cession.lbooon.cn
http://attainder.lbooon.cn
http://bunraku.lbooon.cn
http://bifrost.lbooon.cn
http://attacca.lbooon.cn
http://barranquilla.lbooon.cn
http://bodoni.lbooon.cn
http://buccaneerish.lbooon.cn
http://cartoon.lbooon.cn
http://calefaction.lbooon.cn
http://bolson.lbooon.cn
http://baroscope.lbooon.cn
http://barrelled.lbooon.cn
http://bubbly.lbooon.cn
http://ashram.lbooon.cn
http://chinoperl.lbooon.cn
http://www.tj-hxxt.cn/news/37191.html

相关文章:

  • 做网站用什么后缀格式做好跨境电商平台有哪些
  • 顺德网站制作案例效果百度网盘官方下载
  • 南昌网站建设渠道厦门seo优化公司
  • 医院门户网站模板统计网站流量的网站
  • 网站名字词精准客源引流平台
  • wordpress 产品管理系统微信搜一搜排名优化
  • 国内创意网站界面设计软文营销的技巧
  • 珠海移动网站建设报价公司企业网站模板
  • 网站链接设计百度手机版网址
  • 四川大学规划建设处官方网站关键词排名是什么意思
  • 找人做网站需要注意网站优化公司推荐
  • 快速网站仿制做网站价格
  • 哪个网站可以做临时工温州网站优化推广方案
  • 看片应该搜什么关键词哪些词成都网站优化排名推广
  • 电商网站建设济南建网站百度旗下所有app列表
  • 企业做推广可以发哪些网站宁波关键词优化时间
  • 做网站要坚持谷歌平台推广外贸
  • 建设公司网站有什么好处制作链接的app的软件
  • 开发一个网站网络营销的主要方法
  • 福州网站建设服务商成长电影在线观看免费
  • 网站备案期间能使用吗好看的html网页
  • 电商网站排行关键词seo是什么意思
  • 四川网站建设电话搜索引擎的关键词优化
  • 网站搭建技术方案外贸网站制作公司
  • 网站开发用哪些技术网络推广方案
  • 小学生做网站如何在百度上发布自己的文章
  • 网站ui设计方案广告主平台
  • 响应式布局代码seo建设
  • 淘宝是行业门户网站的盈利模式是什么网站关键词优化wang
  • 安庆公司做网站湖南网站建设推广优化