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

珠海网站建设哪家好5g影讯5g天线在线观看免费视频

珠海网站建设哪家好,5g影讯5g天线在线观看免费视频,武汉微信网站开发,wordpress 利用页面搞前言 本文章主要介绍C11语法中std::thread的使用,以及条件变量和互斥量的使用。 std::thread介绍 构造函数 std::thread 有4个构造函数 // 默认构造函,构造一个线程对象,在这个线程中不执行任何处理动作 thread() noexcept;// 移动构造函…

前言

  • 本文章主要介绍C++11语法中std::thread的使用,以及条件变量和互斥量的使用。

std::thread介绍

构造函数

  • std::thread 有4个构造函数
  • // 默认构造函,构造一个线程对象,在这个线程中不执行任何处理动作
    thread() noexcept;// 移动构造函数。将 other 的线程所有权转移给新的thread 对象。之后 other 不再表示执行线程。
    // 线程对象只可移动,不可复制
    thread( thread&& other ) noexcept;// 创建线程对象,并在该线程中执行函数f中的业务逻辑,args是要传递给函数f的参数
    template< class F, class... Args > 
    explicit thread( F&& f, Args&&... args );// 使用=delete显示删除拷贝构造, 不允许线程对象之间的拷贝
    thread( const thread& ) = delete;
    
  • 通过以下代码演示下如何构造函数的使用
    •   #include <iostream>#include <thread>#include <chrono>void threadFunc2() {std::cout << "enter threadFunc2" << std::endl;}void threadFunc3(int data) {std::cout << "enter threadFunc3, data: " << data << std::endl;}class CThread4 {public:void threadFunc4(const char * data) {std::cout << "enter threadFunc4, data: " << data << std::endl;}};void threadFunc5() {for (int i = 0; i < 5; i++) {std::cout << "enter threadFunc5" << std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));}}int main() {// 默认构造std::thread th1;// 线程中执行函数std::thread th2(threadFunc2);th2.join();// 线程中执行带参函数std::thread th3(threadFunc3, 10010);th3.join();CThread4 ct4;// 线程中执行类成员函数std::thread th4(&CThread4::threadFunc4, &ct4, "hello world");th4.join();std::thread th5_1(threadFunc5);// 使用移动构造std::thread th5_2(std::move(th5_1));th5_2.join();// 执行lambda表达式std::thread th6([] {std::cout << "enter threadFunc6" << std::endl;});th6.join();system("pause");return 0;}
      
  • 执行结果
    •   enter threadFunc2enter threadFunc3, data: 10010enter threadFunc4, data: hello worldenter threadFunc5enter threadFunc5enter threadFunc5enter threadFunc5enter threadFunc5enter threadFunc6请按任意键继续. . .
      

成员函数

  • // 获取线程ID
    std::thread::id get_id() const noexcept;
    // 阻塞当前线程,直至调用join的子线程运行结束
    void join();
    // 将执行线程从线程对象中分离,允许独立执行。
    void detach();
    // 判断主线程和子线程的关联状态
    bool joinable() const noexcept;
    // 如果 *this 仍然有一个关联的运行中的线程,则调用 std::terminate()。
    // 否则,将 other 的状态赋给 *this 并将 other 设置为默认构造的状态。
    thread& operator=( thread&& other ) noexcept;
    
  • 通过代码看下如何使用成员函数
    •   #include <iostream>#include <thread>#include <chrono>void threadFunc3(int data) {std::cout << "enter threadFunc3, data: " << data << std::endl;}void threadFunc4(int data) {std::cout << "start threadFunc4, data: " << data << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << "end threadFunc4, data: " << data << std::endl;}void threadFunc5(int data) {std::cout << "start threadFunc5, data: " << data << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << "end threadFunc5, data: " << data << std::endl;}int main() {{// 线程中执行带参函数std::thread th3(threadFunc3, 10010);std::cout << "th3 id: " << th3.get_id() << std::endl;// 此刻th3线程与主线程有关联std::cout << "th3 joinable: " << th3.joinable() << std::endl;th3.join();std::cout << "th3 id: " << th3.get_id() << std::endl;// 线程执行结束,此刻th3线程与主线程无关联std::cout << "th3 joinable: " << th3.joinable() << std::endl;}{std::thread th5(threadFunc5, 10050);// 如果不想在主线程中等待子线程,可以使用detach。// 这样即便主线程运行结束,子线程依旧会执行// 实际使用时不建议这样做th5.detach();}system("pause");return 0;}
      
  • 执行结果
    •   enter threadFunc3, data: 10010th3 id: 12820th3 joinable: 1th3 id: 0th3 joinable: 0start threadFunc5, data: 10050请按任意键继续. . . end threadFunc5, data: 10050
      

条件变量

  • 条件变量是C++11提供的一种用于等待的同步机制,它能阻塞一个或多个线程,直到收到另外一个线程发出的通知或者超时时,才会唤醒当前阻塞的线程。
  • C++11中的条件变量叫 condition_variable,需要配合std::unique_lock<std::mutex>使用。
  • 先看以下一段代码
    •   #include <iostream>#include <thread>#include <chrono>#include <mutex>#include <condition_variable>int g_cnt = 0;// 定义互斥量std::mutex g_mutex;// 定义条件变量std::condition_variable g_cond;void threadFunc1() {while (g_cnt != 50) {std::this_thread::sleep_for(std::chrono::milliseconds(1));}std::cout << "threadFunc1 g_cnt: " << g_cnt << std::endl;}void threadFunc2() {while (g_cnt < 100) {g_cnt++;std::this_thread::sleep_for(std::chrono::milliseconds(1));}}int main() {{std::thread th1(threadFunc1);std::thread th2(threadFunc2);th1.join();th2.join();std::cout << "g_cnt: " << g_cnt << std::endl;}system("pause");return 0;}
      
  • 线程2对g_cnt进行递增操作,线程1在g_cnt等于50时退出循环并打印结果。但这个流程有一个问题,比如线程1某次访问g_cnt时,值为49,下一次再访问时,值可能为50,也可能为51。如果g_cnt值为51,那这个条件永远都不会满足,循环也就永远无法结束。并且线程1中,我们只想获取g_cnt等于50这个状态,没必要每次都去访问,这也会耗费系统资源。
  • 使用条件变量做以下修改
    •   #include <iostream>#include <thread>#include <chrono>#include <mutex>#include <condition_variable>int g_cnt = 0;// 定义互斥量std::mutex g_mutex;// 定义条件变量std::condition_variable g_cond;bool g_flag = false;void threadFunc1() {std::unique_lock<std::mutex> lock(g_mutex);while (!g_flag) {// 阻塞等待,等待被唤醒g_cond.wait(lock);}std::cout << "threadFunc1 g_cnt: " << g_cnt << std::endl;}void threadFunc2() {while (g_cnt < 100) {g_cnt++;if (g_cnt == 50) {g_flag = true;// 唤醒阻塞的线程g_cond.notify_one();}std::this_thread::sleep_for(std::chrono::milliseconds(1));}}int main() {{std::thread th1(threadFunc1);std::thread th2(threadFunc2);th1.join();th2.join();std::cout << "g_cnt: " << g_cnt << std::endl;}system("pause");return 0;}
      
  • 这样就可以保证线程1的条件肯定可以满足。

线程互斥

  • 控制线程对共享资源的访问。比如写文件时,不能读文件,读文件时,不能写文件。
  • C++11提供了4种互斥锁
    • std::mutex:独占的互斥锁,不能递归使用。
    • std::timed_mutex:带超时的独占互斥锁,不能递归使用。在获取互斥锁资源时增加了超时等待功能。
    • std::recursive_mutex:递归互斥锁,不带超时功能。允许同一线程多次获得互斥锁。
    • std::recursive_timed_mutex:带超时的递归互斥锁。
  • 分析以下这段代码的输出结果
    •   #include <iostream>#include <thread>#include <chrono>#include <mutex>int g_cnt = 0;void threadFunc(int num) {for (int i = 0; i < num; i++) {g_cnt++;std::this_thread::sleep_for(std::chrono::milliseconds(1));}}int main() {{// 线程中执行带参函数std::thread th1(threadFunc, 100);std::thread th2(threadFunc, 100);th1.join();th2.join();std::cout << "g_cnt: " << g_cnt << std::endl;}system("pause");return 0;}
      
  • 我们期望的g_cnt输出结果为200,但实际上g_cnt很大概率不是200而是小于200。这是由于没有对共享资源g_cnt进行加锁保护,这会导致数据竞争。两个线程可能同时访问g_cnt,导致某个线程的++操作被另一个线程覆盖。
  • 对上面代码做下修改,对共享资源g_cnt进行加锁保护。
    •   #include <iostream>#include <thread>#include <chrono>#include <mutex>int g_cnt = 0;// 定义互斥量std::mutex g_mutex;void threadFunc(int num) {for (int i = 0; i < num; i++) {// 加锁g_mutex.lock();g_cnt++;// 解锁g_mutex.unlock();std::this_thread::sleep_for(std::chrono::milliseconds(1));}}int main() {{// 线程中执行带参函数std::thread th1(threadFunc, 100);std::thread th2(threadFunc, 100);th1.join();th2.join();std::cout << "g_cnt: " << g_cnt << std::endl;}system("pause");return 0;}
      
  • 加锁后,就可以保证g_cnt的结果为200。
  • 上面代码有这样一种风险。如果加锁后,中途退出而忘记解锁,就会导致死锁现象。
    •   void threadFunc(int num) {for (int i = 0; i < num; i++) {// 加锁g_mutex.lock();g_cnt++;if (i == 50) {break;}// 解锁g_mutex.unlock();std::this_thread::sleep_for(std::chrono::milliseconds(1));}}
      
  • C++ 11 提供了一种模板类 std::lock_guard,可以简化互斥锁的写法。调用构造时加锁,离开作用域时解锁。不用手动加解锁,大大提高了安全性。
  • 实现代码如下。即便中途退出,也不会出现死锁现象。
    •   void threadFunc(int num) {for (int i = 0; i < num; i++) {// 加锁std::lock_guard<std::mutex> lock(g_mutex);g_cnt++;if (i == 50) {break;}std::this_thread::sleep_for(std::chrono::milliseconds(1));}}
      

参考

  • https://en.cppreference.com/w/cpp/thread/thread
http://www.tj-hxxt.cn/news/103223.html

相关文章:

  • 黑群晖做php网站长春seo结算
  • 手机套 东莞网站建设2021十大网络舆情案例
  • 苏州能做网站河南郑州网站推广优化外包
  • 装饰网站建设套餐报价长沙百度关键词推广
  • 深圳网站优化费用网络舆情监控
  • 网站上线前准备百度热线人工服务电话
  • 合肥网站建设是什么网店营销策划方案ppt
  • 网站服务器搬家好网站制作公司
  • 有哪些网站能免费建站如何做好网络营销推广
  • 网页设计网站怎么做seo外包公司报价
  • 做混剪素材下载网站seo专业培训中心
  • wordpress ad整站优化和关键词优化的区别
  • 无锡网站建设哪家做得比较好网络销售是什么
  • 招远做网站公司cpa推广平台
  • 一个平台维护三天正常吗兰州网络推广优化服务
  • 诸城网站建设费用广州seo网络培训课程
  • 一个ip 做2个网站重庆seo公司排名
  • 中国互联网排名前十名seo网络推广优化
  • 靖江市属于哪里有做网站的百度指数网址是多少
  • 做创新方法工作网站怎么创建网页链接
  • 重庆微信网站济南seo公司
  • app设计ppt厦门seo网络推广
  • 网站建设课程设计济南网站优化排名
  • 怎么向中央媒体求助seo泛目录培训
  • 外贸型网站制作微信营销模式
  • 网站建设公司怎样拓展网站业务百度查看订单
  • 如何搭建自己的博客seowhy官网
  • 怎么做区块链网站电商的推广方式有哪些
  • 企业网站轮播图麒麟seo外推软件
  • 网站开发前台开发今天上海重大新闻事件