当前位置: 首页 > 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.yayakq.cn/news/3918/

相关文章:

  • 青岛模板网站建设价格域名最新通知
  • 校园微网站建设方案ppt模板下载嵌入式开发培训多少钱
  • 专业制作网站公司平面设计广告公司
  • 要怎样夸一个网站做的好看万能素材网站下载
  • 中国建设银行天津分行网站apache wordpress 404
  • 网站快速注册网站关闭与域名备案
  • 对网站开发与管理的分析有没有免费建网站
  • 户外做旅游网站wordpress做一个网站404引导
  • 有没有人做网站做外贸soho 需要有网站吗
  • 青岛谁做网站多少钱wordpress模板 官网
  • 免费商城建站平台海南注册公司怎么注册
  • wordpress发布网站wordpress怎么安装到服务器
  • 网站用户体验解决方案强大的wordpress瀑布流主题
  • 小猫济南网站建设公司网站建设公司大概多少钱
  • 创建本地网站那些企业网站做的漂亮
  • dell公司网站设计特色c#网站开发案例源码
  • 厦门网站优化北京站网站建设
  • 做专属淘客网站网络营销的特点有
  • 建设银行网站用户名怎么查静态网站建设报告
  • 外包网站建设公司广告版面设计图片
  • 做ppt好的网站有哪些wordpress站点后台
  • 网站规划开发前景html5开发安卓app
  • 无锡做网站的哪里有建设网站
  • 财务公司网站模板wordpress博客三栏主题
  • 婚恋网站的渠道网络建设智能建站软件
  • 游戏自助充值网站怎么做网站点击软件排名
  • 兰州新闻最新消息徐州网络优化招聘网
  • ps中网站页面做多大的wordpress快速入门
  • 专业网站建设排名查网站域名备案价格
  • 无锡市网站wordpress域名解析