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

二手商品网站开发背景沧县住房和城乡建设局网站

二手商品网站开发背景,沧县住房和城乡建设局网站,图片类网站怎样做高并发,如何写软文推广产品一、介绍 在前面的C20新功能中#xff0c;简单的介绍过相关的std::jthread的应用。当时觉得它虽然比std::thread方便一些#xff0c;但也没有多大的优势。可在后面的不断的学习中#xff0c;发现std::jthread的使用上确实有优秀之处#xff0c;相对于传统的线程编程#…一、介绍 在前面的C20新功能中简单的介绍过相关的std::jthread的应用。当时觉得它虽然比std::thread方便一些但也没有多大的优势。可在后面的不断的学习中发现std::jthread的使用上确实有优秀之处相对于传统的线程编程等于是提前安全的封装了对线程安全管理和控制的相关模块和接口。 二、std::jthread应用 一般来说对线程的应用主要有以下几类 1、线程管理 线程管理就是对线程的按需启动和安全退出有一个最基础的要求在std::thread中可以通过线程分离和Join来控制线程的安全退出使用一些变量来处理线程中循环的退出。但这些在std::jthread中都有了安全的应用机制 线程启动直接启动即可。 线程退出处理自动合并joining这个没什么可说的。 线程停止控制线程取消使用std::stop_token和std::stop_source。std::stop_source负责维护线程的共享停止状态提供了一种发出停止线程的请求方法。它可以与std::stop_token与std::stop_callback共同工作。std::stop_token可以理解成一种对线程退出状态的查看查看关联的std::stop_source如果满足条件就退出。 其实线程的启动还相对好控制一些特别是线程的退出一般对初学者来说都是比较难以驾驭的经常是线程退出整个程序也崩溃了。所以std::jthread提供的这个退出控制还是不错的。 2、条件变量 在多线程编程中Linux环境下使用条件变量的很多但在C20中配合std::condition_variable_any则会更加方便。std::condition_variable_any比std::condition_variable应用更广泛而不只是局限于对 std::unique_lockstd::mutex的控制意味着能支持更多的锁机制。 #include condition_variable #include iostream #include mutex #include threadvoid testConditionAny(std::stop_token sToken) {std::mutex mutex;std::unique_lock lock(mutex);// cv_any::wait内部含std::stop_token的重载,当sToken停止会调用cv_any的唤醒动作std::condition_variable_any().wait(lock, sToken, [] {//这个谓词类似于处理假唤醒的bool值return false;});std::cout jthread function testConditionAny quit! std::endl; } int main() {//jthread的RAII封装会调用request_stop()std::jthread testAny(testConditionAny);std::this_thread::sleep_for(std::chrono::seconds(1));return 0; }其实这个和普通的condition_variable的用法是一致的。 3、回调处理 如果说上面的“条件变量”和std::jthread没有必然联系但在线程中的回调还是相当重要的。在C20中针对std::jthread则提供了一个回调std::stop_callback类。这个有一个细节需要注意如果是此回调执行了则在关联的 std::stop_token 的std::stop_source 调用了 request_stop() 的同一线程中调用。否则在构造的线程中执行。 三、例程 先看一个stop_source的例程 #include chrono #include iostream #include stop_token #include threadusing namespace std::chrono_literals;void worker_fun(int id, std::stop_source stop_source) {std::stop_token stoken stop_source.get_token();for (int i 10; i; --i){std::this_thread::sleep_for(300ms);if (stoken.stop_requested()){std::printf( 工作线程%d 被请求停止\n, id);return;}std::printf( 工作线程%d 返回睡眠\n, id);} }int main() {std::jthread threads[4];std::cout std::boolalpha;auto print [](const std::stop_source source){std::printf(stop_source stop_possible %s, stop_requested %s\n,source.stop_possible() ? true : false,source.stop_requested() ? true : false);};// 普通停止信号源std::stop_source stop_source;print(stop_source);// 创建工作线程for (int i 0; i 4; i)threads[i] std::jthread(worker_fun, i 1, stop_source);std::this_thread::sleep_for(500ms);std::puts(请求停止);stop_source.request_stop();print(stop_source);// 注意jthreads 的析构函数会调用 join因此无需显式调用 }运行结果 stop_source stop_possible true, stop_requested false工作线程2 返回睡眠工作线程3 返回睡眠工作线程1 返回睡眠工作线程4 返回睡眠 请求停止 stop_source stop_possible true, stop_requested true工作线程3 被请求停止工作线程1 被请求停止工作线程2 被请求停止工作线程4 被请求停止再看一个回调函数的 #include chrono #include condition_variable #include iostream #include mutex #include sstream #include threadusing namespace std::chrono_literals;// 使用一个辅助类进行原子 std::cout 流输出。 class Writer {std::ostringstream buffer;public:~Writer() { std::cout buffer.str(); }Writer operator(auto input) {buffer input;return *this;} };int main() {// 工作线程。// 它将等待直至被请求停止。std::jthread worker([](std::stop_token stoken) {Writer() 工作线程 id: std::this_thread::get_id() \n;std::mutex mutex;std::unique_lock lock(mutex);std::condition_variable_any().wait(lock, stoken, [stoken] { return stoken.stop_requested(); });});// 在工作线程上注册停止回调。std::stop_callback callback(worker.get_stop_token(),[] { Writer() 执行了停止回调线程: std::this_thread::get_id() \n; });// 可以提前销毁 stop_callback 对象以阻止其执行。{std::stop_callback scoped_callback(worker.get_stop_token(), [] {// 这里不会执行。Writer() 作用域内的停止回调被执行线程: std::this_thread::get_id() \n;});}// 演示由哪个线程何时执行 stop_callback。// 定义停止函数。auto stopper_func [worker] {std::cout std::this_thread::get_id() \n;if (worker.request_stop())Writer() 执行了停止请求线程: std::this_thread::get_id() \n;elseWriter() 未执行停止请求线程: std::this_thread::get_id() \n;};// 使多个线程竞争以停止工作线程。std::jthread stopper1(stopper_func);std::jthread stopper2(stopper_func);stopper1.join();stopper2.join();// 已经请求停止后立即执行新的 stop_callback。Writer() 主线程: std::this_thread::get_id() \n;std::stop_callback callback_after_stop(worker.get_stop_token(), [] { Writer() 执行了停止回调线程: std::this_thread::get_id() \n; }); }执行结果 工作线程 id: 140149702784576 140149694391872 140149685999168 执行了停止回调线程: 140149694391872 执行了停止请求线程: 140149694391872 未执行停止请求线程: 140149685999168 主线程: 140149709902784 执行了停止回调线程: 140149709902784需要知道的是stop_token 的获取可以从stop_source得到也可以从std::jthread得到都有相关的获取API接口。 注代码来自cppreference 四、总结 目前很少听说在实际工程中有应用jthread的可能大家觉得thread就比较好用也可能是开发习惯和代码惯性的问题。std::jthread需要支持的版本也比较高得到c20估计这也是一个非常重要的原因。毕竟现在C11都没有真正普及开来很多开发者仍然只是使用一些非常简单的新特性。 还是要追上来与是俱进
http://www.hkea.cn/news/14384803/

相关文章:

  • 寻找做网站的公司wordpress主题预览插件
  • 网站开发公司招聘电子商务网站的重要性
  • 上传文章网站珠海市建设局官方网站
  • 怎么跟客户介绍网站建设知名的电子商务网站
  • 用asp做网站span深圳口碑好的vi设计公司
  • jsp网站开发中英文页面切换网络营销运营公司
  • 温州网站制作公司域名查询万网
  • 时尚大气网站设计wordpress 分词
  • 网站综合营销方案设计正品购物平台哪个最好
  • 银川商城网站开发设计优秀网络小说
  • 网站开发后服务费做教育网站需要规划哪些内容
  • 网站首页作用做网站需要数据库吗
  • 网站平台建设公司新手建站广告联盟赚钱
  • 网站制作公司昆明wordpress 语法
  • 中国建设银行网站类型青岛微网站
  • wordpress 添加手机号肇庆网站seo
  • 海外网站有哪些如何对网站进行改版
  • wordpress 名站网站二级菜单是什么意思
  • wordpress网站示例安全教育网站建设背景
  • 辽宁住房和城乡建设厅网站wordpress 手机端发帖
  • 一级a做爰片在线看网站canva 可画主页首页首页模板素材
  • 网站制作流程的组成部分包括怎么做网站网站
  • 做房产网站六安市百姓畅言六安杂谈
  • 网站联盟营销免费咨询妇科在线医生
  • 从化网站设计外贸网站源码怎么建
  • 扬州做网站设置网站的默认页面
  • 枣庄市住房和城乡建设局网站网站建设好评公司
  • 网站备案被拒绝舟山 做企业网站
  • 门户网站开发简历免费网络推广方法
  • 海口网站建设策划dede打包好的网站怎么提取模板