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

建站的注意事项推广普通话手抄报内容简短

建站的注意事项,推广普通话手抄报内容简短,网站页面外链怎么做,WordPress邀请码功能C多线程并发编程入门(目录) 本文用最简单易懂的实际案例,讲清楚了 join 的实际内涵,保证你过目不忘。 Hello join 示例 join 函数是我们接触C多线程 thread 遇到的第一个函数。 比如: int main() {thread t(f);t.…

C++多线程并发编程入门(目录)

本文用最简单易懂的实际案例,讲清楚了 join 的实际内涵,保证你过目不忘。

Hello join 示例

join 函数是我们接触C++多线程 thread 遇到的第一个函数。

比如:

int main()
{thread t(f);t.join();
}

join 用来阻塞当前线程退出

join 表示线程 t 运行起来了。但是,t 也阻碍了 main 线程的退出。

也就是说,如果 f 的执行需要 5秒钟, main也要等待5秒才能退出。

这看起来非常合理,因为 main 就应该等待 t 退出之后再退出。

main 等待所有线程

多个线程都以 join 的方式启动的时候,main 就要等到最后。

比如:

int main()
{thread t1(f1);t1.join();thread t2(f2);t2.join();
}

假如, f1 需要执行5秒, f2 需要执行 1 秒, 那么 main 就需要等待 max(5, 1) = 5 秒。

整个过程中 f1 f2 各自独立运行,谁运行谁的,互不干涉。

执行示意图

完整示例

下面的代码,main 线程 等待 first 线程 和 second 线程都退出之后再退出。

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;void first()
{// simulate expensive operationstd::this_thread::sleep_for(std::chrono::seconds(5));cout << "first finished!" << endl;
}void second()
{// simulate expensive operationstd::this_thread::sleep_for(std::chrono::seconds(1));cout << "second finished!" << endl;
}int main()
{auto start = std::chrono::system_clock::now();std::cout << "starting first helper...\n";std::thread helper1(first);std::cout << "starting second helper...\n";std::thread helper2(second);std::cout << "waiting for helpers to finish..." << std::endl;helper1.join();helper2.join();auto elapsed = chrono::duration_cast<chrono::seconds>(system_clock::now() - start).count();std::cout << "done! elapsed "<<elapsed<<" seconds.";
}

执行结果

main 等待所有线程的退出

可以看出,main 确实是等待两个线程都执行完之后才退出的。

有一个细节, 先执行完了 second 线程,后执行完了 first 线程。

这样很正常, 毕竟 second 线程耗时短(1秒),first 线程耗时长(5秒)。

所以,main 等待了 5 秒钟才退出。

线程的嵌套(join的弊端)

实际工作中,我们创建线程不大可能都在 main 函数中创建。

我们通常是在其他线程中遇到了某种事件发生,这时候才知道要赶紧创建新的线程来执行某个新任务。

比如,我们写了一个腾讯会议软件,点击开始录制按钮的时候,创建录制线程。

录制按钮的回调函数是在UI线程里执行的,也就是创建录制线程并执行录制现场这件事发生在UI线程里。不在main线程里。

示例:

int main()
{thread ui(ui_fun);ui.join();//main等待整个窗口程序关闭再退出
}ui_fun()
{thread button_clicked(on_button_clicked);//创建录制线程button_clicked.join();//执行录制动作
}

这种情况,main 等待 ui 线程,这没什么问题。但是, ui 等待 录制线程,就会导致 ui 线程卡住。

此时你按界面上其他的按钮,就不会有任何响应。这是不应该发生的

执行示意图

main一共等待了1+5=6秒

完整示例

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;void record()
{// simulate expensive operationstd::this_thread::sleep_for(std::chrono::seconds(1));cout << "record finished!" << endl;
}void ui_fun()
{std::cout << "starting record ...\n";std::thread record_thread(record);record_thread.join();// simulate expensive operationstd::this_thread::sleep_for(std::chrono::seconds(5));cout << "ui_fun finished!" << endl;
}int main()
{auto start = std::chrono::system_clock::now();std::cout << "starting ui_fun ...\n";std::thread helper1(ui_fun);std::cout << "waiting for ui_fun to finish..." << std::endl;helper1.join();auto elapsed = chrono::duration_cast<chrono::seconds>(system_clock::now() - start).count();std::cout << "done! elapsed " << elapsed << " seconds.";
}

执行结果

参考

C++ std::thread join()的理解 - 代萌 - 博客园 (cnblogs.com)

std::thread::join - cppreference.com

http://www.mmbaike.com/news/24377.html

相关文章:

  • 做拍客哪个网站好搜索引擎网址
  • 网站制作的内容什么好网络快速推广渠道
  • 网上做衣服的网站有哪些百度关键字优化精灵
  • 什么做电子书下载网站好关键词排名优化易下拉技巧
  • 代做毕设要注册答疑网站搜索引擎营销的基本流程
  • 交易平台网站开发教程百度云企业网站推广有哪些方式
  • 一般做网站宽度是多少营销策划案ppt优秀案例
  • 佛山网站建设有限公司设计公司网站
  • 廊坊网站建设方案服务济南优化哪家好
  • 免费微信微网站模板下载小说搜索风云榜排名
  • 做网站需要工具苏州网站
  • 苏州退工在哪个网站做宁波企业网站seo
  • 微网站搭建的步骤和技巧重庆seo推广服务
  • 企业网站管理系统用哪个好网络服务提供者收集和使用个人信息应当符合的条件有
  • 建设积分网站长沙百度贴吧
  • 学校网站建设评审会议通知杭州网站建设网页制作
  • 泊头在哪做网站比较好大数据培训机构排名前十
  • 网站改版设计思路中国刚刚发生的新闻
  • 做网站的软件初中生泉州百度推广咨询
  • 北京市网站备案百度下载老版本
  • wordpress论坛注册网站运营seo实训总结
  • 网站维护后期费用营销网站建设教学
  • 毕业论文设计网站开发成免费crm软件有哪些优点
  • 免费域名网站申请流量平台排名
  • 网站用香港的空间速度怎么样网站优化基本技巧
  • 建设部标准定额网站营销型网站建设公司价格
  • 赣县网站建设软件开发工资一般多少
  • 做市场分析的网站seo搜索引擎优化方式
  • 建筑公司企业愿景文案珠海优化seo
  • 网购哪个平台最靠谱百度手机seo