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

东莞网站建设服务协议seo关键词外包公司

东莞网站建设服务协议,seo关键词外包公司,企业推广文章,外贸页面网站制作std::jthread 说明: std::jthread 是 C20 中引入的一个新特性,它是线程库中的一个类,专门用于处理 std::thread 与 std::stop_token 和 std::stop_source 之间的交互,以支持更优雅和安全的线程停止机制。 std::stop_source控制…

std::jthread

说明:

std::jthread 是 C++20 中引入的一个新特性,它是线程库中的一个类,专门用于处理 std::thread 与 std::stop_token 和 std::stop_source 之间的交互,以支持更优雅和安全的线程停止机制。

std::stop_source控制线程标记。相当于g_bQuitFlag = true;

std::stop_token线程函数内检测结束标记。相当于while(!g_bQuitFlag){...};

作用1.可控制的线程结束,防止人工封装的线程结束控制变量不是线程安全的。2.线程结束后不必手工调用join();

源码简单赏析:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\thread

下面代码可以复制出来浏览。

class jthread {
public:using id = thread::id;using native_handle_type = thread::native_handle_type;jthread() noexcept : _Impl{}, _Ssource{ nostopstate } {}template <class _Fn, class... _Args, enable_if_t<!is_same_v<remove_cvref_t<_Fn>, jthread>, int> = 0>_NODISCARD_CTOR explicit jthread(_Fn&& _Fx, _Args&&... _Ax) {if constexpr (is_invocable_v<decay_t<_Fn>, stop_token, decay_t<_Args>...>) //线程函数第一个参数为stop_token,如果客户端std::jthread jt(work, stop_source.get_token());使用自己的stop_source展开相当于//is_invocable_v<decay_t<_Fn>, stop_token, stop_token>为false,执行下面else为不使用this->_Ssource,这里很微妙!//如果客户端std::jthread jt(work);不使用自己的stop_source展开相当于is_invocable_v<decay_t<_Fn>, stop_token>为true//也就是说这个if判断了三种情况,1.线程函数第一个不为stop_token 2.线程函数第一个参数为stop_token且客户端使用自己的stop_source //3.线程函数第一个参数为stop_token且客户端使用不自己的stop_source{_Impl._Start(_STD forward<_Fn>(_Fx), _Ssource.get_token(), _STD forward<_Args>(_Ax)...);//按照使用this内置的stop_source::stop_token传给线程函数}else {_Impl._Start(_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...);//按照std::thread处理,不使joinable功能,那么this->request_stop();也无效。处于兼容性考虑吧}}~jthread() {_Try_cancel_and_join();//析构时可自动调用_Impl.joinable();_Ssource.request_stop();_Impl.join();}jthread(const jthread&) = delete;//不支持拷贝构造jthread(jthread&&) noexcept = default;//jthread依然是unique thread类型的jthread& operator=(const jthread&) = delete;//不支持赋值jthread& operator=(jthread&& _Other) noexcept {// note: the standard specifically disallows making self-move-assignment a no-op here// N4861 [thread.jthread.cons]/13// Effects: If joinable() is true, calls request_stop() and then join(). Assigns the state// of x to *this and sets x to a default constructed state._Try_cancel_and_join();//移动赋值时,先结束掉本线程,再移动源线程到this_Impl = _STD move(_Other._Impl);_Ssource = _STD move(_Other._Ssource);return *this;}//剩下的简单函数封装void swap(jthread& _Other) noexcept {_Impl.swap(_Other._Impl);_Ssource.swap(_Other._Ssource);}_NODISCARD bool joinable() const noexcept {return _Impl.joinable();}void join() {_Impl.join();}void detach() {_Impl.detach();}_NODISCARD id get_id() const noexcept {return _Impl.get_id();}_NODISCARD stop_source get_stop_source() noexcept {return _Ssource;}_NODISCARD stop_token get_stop_token() const noexcept {return _Ssource.get_token();}bool request_stop() noexcept {return _Ssource.request_stop();}friend void swap(jthread& _Lhs, jthread& _Rhs) noexcept {_Lhs.swap(_Rhs);}_NODISCARD static unsigned int hardware_concurrency() noexcept {return thread::hardware_concurrency();}private:void _Try_cancel_and_join() noexcept {if (_Impl.joinable()) {_Ssource.request_stop();_Impl.join();}}thread _Impl;//采用c++组合方式对std::thread进行薄薄的封装,是不是和std::queue差不多的方法?stop_source _Ssource;//使用std自己实现的原子操作类std::stop_source控制线程停止
};

应用:

根据源代码阅读衍生的几个例子。源代码和例子说的是一致的。

1.不使用jthread内置的stop_source相当于thread类,std::jthread::request_stop();将不起作用,方便于一个外置std::stop_source控制多个线程函数

#include <iostream>
#include <thread>
#include <chrono>void work(std::stop_token stop_token) {while (!stop_token.stop_requested()) {std::cout << "Working...\n";std::this_thread::sleep_for(std::chrono::seconds(1));}std::cout << "Stopping...\n";
}int main() {std::stop_source stop_source;std::jthread jt(work, stop_source.get_token());std::this_thread::sleep_for(std::chrono::seconds(3));stop_source.request_stop();return 0;
}

2.使用jthread内置的stop_source :

#include <iostream>
#include <thread>
#include <chrono>void work(std::stop_token stop_token) {while (!stop_token.stop_requested()) {std::cout << "Working...\n";std::this_thread::sleep_for(std::chrono::seconds(1));}std::cout << "Stopping...\n";
}int main() {std::jthread jt(work);std::this_thread::sleep_for(std::chrono::seconds(3));//这两行都没必要人工调用的jt.request_stop();//jt.join();//return 0;
}

3.当线程函数参数没有参数stop_token时,使用起来std::jthread::request_stop();不起作用。相当于std::thread:

#include <iostream>
#include <thread>
#include <chrono>void work() {while (1) {std::cout << "Working...\n";std::this_thread::sleep_for(std::chrono::seconds(1));}std::cout << "Stopping...\n";
}int main() {std::jthread jt(work);std::this_thread::sleep_for(std::chrono::seconds(3));jt.request_stop();jt.join();return 0;
}
//输出:
//Working...
//Working...
//Working...
//Working...
//...

结论:

功能过于强大,灵活,坑也比较多。功能少,呆板,也没有什么坑。其实没有什么坑,只不过没看过开源库源码。官方文档不足以表达,就像黑盒测试永远无法相当于白盒测试。常常碰一碰运气运行一下看起来没问题,黑盒看起来也没问题?官方文档std::jthread - cppreference.com

http://www.hkea.cn/news/637053/

相关文章:

  • 邯郸市做网站建设网络口碑营销案例分析
  • 罗湖网站建设联系电话西安核心关键词排名
  • 如何编写网站电脑清理软件十大排名
  • 怎么给企业制作网站seo关键词排名优化哪好
  • 高仿服装网站建设西安百度关键词推广
  • 网站单页面怎么做的百度seo站长工具
  • 网站建设谢辞企业营销型网站有哪些
  • 免费网站制作申请行业关键词一览表
  • 网站建设费关键词排名提高方法
  • 搭建淘宝客网站源码最近发生的新闻事件
  • 网站模版网网站关键词排名优化价格
  • 做网站去哪里全国免费发布广告信息平台
  • 靖江做网站湖南seo服务电话
  • 工程建设科学技术奖申报网站友情链接交换标准
  • 做网站后期为什么续费链交换
  • 网站开发与设计专业西安seo顾问培训
  • 企业网站建设话术优化营商环境指什么
  • 傻瓜式网站制作微信运营技巧
  • 甘肃网络推广软件seo方案
  • 建筑公司网站首页图片网站推广引流
  • 购物网站 后台模板今日头条站长平台
  • 营销导向企业网站策划站长工具无内鬼放心开车禁止收费
  • WordPress不能支付宝交易吗如何优化
  • 南昌seo网站设计站长工具是做什么的
  • 做IP授权的一般看什么网站一级消防工程师考试
  • 项目建设备案网站爱站网站长百度查询权重
  • 铜陵专业网站制作公司软文免费发布平台
  • 鹿泉市建设局网站短视频seo关键词
  • 手机网站开发标准网络营销服务工具
  • 施工企业分包工程会计与税务处理网站推广优化是什么意思