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

顺德大良那里做网站好/交换友情链接吧

顺德大良那里做网站好,交换友情链接吧,windows安装wordpress,撰写超越时空网上书城网站策划书vs2019 C20 jthread01 jthread的一个实现02 jthread用法02.01 cppreference上面的介绍02.02 可协作中断的用法02.03 NICOLAI JOSUTTIS提供了大量测试用例jthread是有自动合并和取消支持的 std::thread。jthread包装了一下thread,提供了线程运行中停止的接口。jthrea…

vs2019 C++20 jthread

    • 01 jthread的一个实现
    • 02 jthread用法
      • 02.01 cppreference上面的介绍
      • 02.02 可协作中断的用法
      • 02.03 NICOLAI JOSUTTIS提供了大量测试用例

jthread是有自动合并和取消支持的 std::threadjthread包装了一下thread,提供了线程运行中停止的接口。jthread定义在<thread>头文件中。
使用取消功能需要用到std::stop_token。参考:<stop_token>
建议以后使用thread的都开始使用jthread,不必等到c++20出来。

  1. std::jthread对象销毁时,会调用join,等待其所表示的执行结束。
  2. 支持外部请求中止(通过get_stop_source、get_stop_token和request_stop)。
    std::jthread为了实现上述新功能,带来了额外的性能开销(主要是多了一个成员变量)。而根据C++一直以来“不为不使用的功能付费”的设计哲学,他们自然就把这些新功能拆出来新做了一个类。1

01 jthread的一个实现

NICOLAI JOSUTTIS [尼古拉·乔苏蒂斯]2关于jthread的一个实现:
https://github.com/josuttis/jthread
这个实现没有使用C++20的新特性。只有jthread.hppstop_token.hpp两个文件,700多行代码。
https://github.com/josuttis/jthread/blob/master/source/jthread.hpp
https://github.com/josuttis/jthread/blob/master/source/stop_token.hpp

02 jthread用法

文中所有提到的代码,整理在:https://github.com/5455945/cpp_demo/tree/master/C%2B%2B20/jthread

02.01 cppreference上面的介绍

jthread在基本用法上面完全兼容thread。
https://zh.cppreference.com/w/cpp/thread/jthread/jthread

// https://zh.cppreference.com/w/cpp/thread/jthread/jthread
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include "jthread.hpp" // https://github.com/josuttis/jthread/tree/master/sourcevoid f1(int n)
{for (int i = 0; i < 5; ++i) {std::cout << "Thread 1 executing\n";++n;std::this_thread::sleep_for(std::chrono::milliseconds(10));}
}void f2(int& n)
{for (int i = 0; i < 5; ++i) {std::cout << "Thread 2 executing\n";++n;std::this_thread::sleep_for(std::chrono::milliseconds(10));}
}class foo
{
public:void bar(){for (int i = 0; i < 5; ++i) {std::cout << "Thread 3 executing\n";++n;std::this_thread::sleep_for(std::chrono::milliseconds(10));}}int n = 0;
};class baz
{
public:void operator()(){for (int i = 0; i < 5; ++i) {std::cout << "Thread 4 executing\n";++n;std::this_thread::sleep_for(std::chrono::milliseconds(10));}}int n = 0;
};void test_jthread_cppreference01()
{int n = 0;foo f;baz b;std::jthread t0; // t0 不是线程std::jthread t1(f1, n + 1); // 按值传递std::jthread t2a(f2, std::ref(n)); // 按引用传递std::jthread t2b(std::move(t2a)); // t2b 现在运行 f2() 。 t2a 不再是线程std::jthread t3(&foo::bar, &f); // t3 在对象 f 上运行 foo::bar()std::jthread t4(b); // t4 在对象 b 上运行 baz::operator()t1.join();t2b.join();t3.join();std::cout << "Final value of n is " << n << '\n';std::cout << "Final value of foo::n is " << f.n << '\n';// t4 在析构时结合
}

02.02 可协作中断的用法

参考 C ++ 20中的新线程(jthread)功能
使用request_stop()jthread线程发送停止线程请求;在jthread线程中使用stop_requested()来判断是否收到停止线程请求。如果收到停止请求,即做出退出线程反应。

void sleep(const int seconds) {std::this_thread::sleep_for(std::chrono::seconds(seconds));
}void test_jthread03() {std::jthread jt{ [](std::stop_token st) {while (!st.stop_requested()) { // 有停止线程请求的处理std::cout << "Doing work\n";sleep(1);}} };sleep(5);jt.request_stop(); // 请求线程停止,因有响应停止请求而终止线程jt.join();
}

02.03 NICOLAI JOSUTTIS提供了大量测试用例

https://github.com/josuttis/jthread/tree/master/source中提供了大量测试用例,对理解jthread的用法很有帮助。


  1. std::jthread与std::thread的区别是什么? ↩︎

  2. 《C++标准库(第2版)》、《C ++ 17 –完整指南》作者 ↩︎

http://www.lbrq.cn/news/761059.html

相关文章:

  • 做网站交互demo工具/店铺在百度免费定位
  • 鸡泽网站建设/外贸网站平台有哪些
  • 网站的标题符号/网络运营推广怎么做
  • 网站建设网络公/运营商大数据精准营销
  • 网站做的和别人一样违法吗/宁波seo软件
  • dede 如何做视频网站/百度电脑版网页
  • 厨之梦进口食品网站谁做的/深圳网站建设开发公司
  • 打名字就说你是什么做的网站/制作企业网站的公司
  • 武汉网站建设询搜点网络/网页设计一般用什么软件
  • 合规部对于网站建设的意见/中国大数据平台官网
  • 昆山做网站费用/icp备案查询
  • mac系统可以做数据库网站开发/网页推广链接怎么做
  • 游戏网站做的思想步骤/app搜索优化
  • 关于做网站的笑话/精准的搜索引擎优化
  • 温州市建设工程质监站网站/网络营销以什么为中心
  • 网站电子商务类型/杭州seo顾问
  • 课程网站建设的步骤/seo的研究对象
  • 会计信息系统网站建设流程图/谷歌广告代理商
  • 高端网站建设方案模板范文/线上营销策略
  • 花生壳 wordpress/百度快速排名优化服务
  • 暖色网站/怎么开设自己的网站
  • wordpress文章名/游戏优化
  • 织梦网站移动化/重庆seo网络推广平台
  • 网站公安备案要多久/怎么自己刷推广链接
  • 深圳市seo推广联系方式/江苏seo团队
  • 怎么做幼儿园的网站/黑帽seo排名技术
  • 应聘网站运营建设面试/淘宝自动推广软件
  • 国内访问wordpress/常州seo收费
  • 外贸网站都有哪些/网络推广公司收费标准
  • 一个专门做标题的网站/百度竞价推广的技巧
  • 阿里云出里两款新的云服务器
  • 【学习嵌入式day-25-线程】
  • 阿里云TranslateGeneral - 机器翻译SDK-自己封账单文件版本—仙盟创梦IDE
  • 《疯狂Java讲义(第3版)》学习笔记ch4
  • 【完整源码+数据集+部署教程】肾脏病变实例分割系统源码和数据集:改进yolo11-CARAFE
  • 希尔排序专栏