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

企业网站推广平台微信建网站服务

企业网站推广平台,微信建网站服务,.net网站与php网站,买域名自己做网站C —— 线程同步(互斥锁) 线程同步互斥锁(互斥量)测试代码mutex互斥锁 线程同步 线程同步:多线程协同工作,协商如何使用共享资源。 C11线程同步包含三部分内容: 互斥锁(互斥量&…

C++ —— 线程同步(互斥锁)

  • 线程同步
  • 互斥锁(互斥量)
    • 测试代码
    • mutex互斥锁

线程同步

线程同步:多线程协同工作,协商如何使用共享资源。
C++11线程同步包含三部分内容:

  1. 互斥锁(互斥量)
  2. 条件变量
  3. 生产/消费者模型

互斥锁(互斥量)

只有加锁解锁两种状态,确保同一时间只有一个线程访问共享资源。
访问共享资源之前加锁,加锁成功访问资源,访问完成解锁释放。
若某线程持有锁,其他线程形成等待队列

C++11提供了4种互斥锁:

  • mutex:互斥锁(最常用)
  • timed_mutex:带超时机制的互斥锁
  • recursive_mutex:递归互斥锁
  • recursive_timed_mutex:带超时机制的递归互斥锁

测试代码

#include <iostream>
#include <thread>
using namespace std;void func (int n, const string& s) {for (int i = 1; i <= 10; i++) {cout << "No." << i << ", n = " << n << ", s = " << s << endl;this_thread::sleep_for(chrono::seconds(1));}
}
a
int main () {thread t1(func, 1, "t1");thread t2(func, 2, "t2");thread t3(func, 3, "t3");thread t4(func, 4, "t4");thread t5(func, 5, "t5");thread t6(func, 6, "t6");t1.join();t2.join();t3.join();t4.join();t5.join();t6.join();return 0;
}

cout属于全局对象,若多线程都用它向屏幕输出文字,会出乱子。线程越多,越容易乱。

mutex互斥锁

使用互斥锁cout加锁,代码如下:

#include <iostream>
#include <thread>
#include <mutex> // 使用互斥锁需要包含的头文件
using namespace std;mutex mtx; // 创建互斥锁对象,用于保护共享资源cout对象。void func (int n, const string& s) {for (int i = 1; i <= 10; i++) {// 每次调用cout对象之前,申请加锁mtx.lock();cout << "No." << i << ", n = " << n << ", s = " << s << endl;// 用完了就解锁mtx.unlock();this_thread::sleep_for(chrono::seconds(1));}
}int main () {// 代码不变...
}

再看一段示例代码:

#include <iostream>
#include <chrono> // 时间相关操作的头文件
#include <thread> // 线程相关操作的头文件
#include <mutex> // 互斥锁的头文件
using namespace std;int a = 0;
mutex mtx; // 创建互斥锁对象,用于保护共享资源a变量。void func () {for (int i = 0; i < 1000000; ++i) {mtx.lock();a++;mtx.unlock();}
}int main () {auto start = chrono::steady_clock::now();thread t1(func);thread t2(func);t1.join();t2.join();auto end = chrono::steady_clock::now();cout << "time = " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << "ms" << endl;cout << "a = " << a << endl;return 0;
}

运行结果:

time = 101ms
a = 2000000

在代码中添加一些打印信息,能更清楚的观察到:两个线程申请加锁,只有一个成功,另外一个线程等待,直到第一个线程解锁后第二个线程才能加锁。

#include <iostream>
#include <chrono> // 时间相关操作的头文件
#include <thread> // 线程相关操作的头文件
#include <mutex> // 互斥锁的头文件
using namespace std;int a = 0;
mutex mtx;void func () {for (int i = 0; i < 1000000; ++i) {cout << "thread id: " << this_thread::get_id() << ", 申请加锁" << endl;mtx.lock();cout << "thread id: " << this_thread::get_id() << ", 加锁成功" << endl;a++;this_thread::sleep_for(chrono::seconds(5));mtx.unlock();cout << "thread id: " << this_thread::get_id() << ", 解锁" << endl;this_thread::sleep_for(chrono::seconds(1));}
}int main () {thread t1(func);thread t2(func);t1.join();t2.join();cout << "a = " << a << endl;return 0;
}

运行结果:

thread id: 139727756003072, 申请加锁
thread id: 139727756003072, 加锁成功
thread id: 139727747610368, 申请加锁
thread id: 139727756003072, 解锁
thread id: 139727747610368, 加锁成功
thread id: 139727756003072, 申请加锁
thread id: 139727747610368, 解锁

感谢浏览


文章转载自:

http://k6FpWSvq.gccdr.cn
http://vZpDvV3T.gccdr.cn
http://mgbsGPln.gccdr.cn
http://O11pTHRP.gccdr.cn
http://RIUyuwzg.gccdr.cn
http://yXq21sLf.gccdr.cn
http://Oy2xNJvc.gccdr.cn
http://ZewAKEZj.gccdr.cn
http://yqVb1kko.gccdr.cn
http://NX4cNNFK.gccdr.cn
http://vDcdWhby.gccdr.cn
http://eCVskGll.gccdr.cn
http://1Uuxb1Kp.gccdr.cn
http://kx3h1n5p.gccdr.cn
http://D8F3J4ln.gccdr.cn
http://Of86ThiN.gccdr.cn
http://C2BzCCXp.gccdr.cn
http://FY7B4GXg.gccdr.cn
http://hsboe6bM.gccdr.cn
http://5ZelAIJ5.gccdr.cn
http://lunIAebI.gccdr.cn
http://fiIJhD3Z.gccdr.cn
http://LkcIuN6b.gccdr.cn
http://2Xo9K6mh.gccdr.cn
http://GFaw1eSS.gccdr.cn
http://57HJlpnP.gccdr.cn
http://t7IJHxTX.gccdr.cn
http://EhjXTcFb.gccdr.cn
http://kodbeB4I.gccdr.cn
http://vDbG0sNQ.gccdr.cn
http://www.dtcms.com/wzjs/655870.html

相关文章:

  • 遵义怎样做网站wordpress 后台 主题
  • 集团网站建设招标附近做广告牌的电话
  • 怎样做网站漂浮扬州做网站的
  • 杭州做网站hzyze深圳外贸建站网络推广哪家好
  • 网站列表怎么做东莞什么行业做网站的多
  • 无锡做网站公司哪家比较好python网站开发教程
  • 优秀设计师的个人网站买卖网站
  • 中山做网站网站建设备案条件
  • 苏州做网站哪里好wordpress 图片 筛选 插件
  • 网站建设实训的心得的体会访问自己做的网站
  • 网站响应式首页模板下载如何用flashfxp上传网站
  • 一 建设网站前的市场分析松原建设网站
  • 有哪里可以做兼职翻译的网站wordpress采集自动伪原创
  • 润滑油手机网站模板桂林公司注册
  • 做垂直导购网站还行吗苏州沧浪做网站哪家好
  • 博物馆网站建设国外搜索引擎大全
  • 自己做网站投放广告如何做行业网站
  • 南昌淘宝网站制作公司wordpress 微信连接数据库
  • 兰州网站维护地方网站怎么做推广
  • 公司网站图片传不上去wordpress在IE9显示错位
  • 北京p2p网站建设即速应用小程序官网
  • 创意设计网站公司医药行业网站建设
  • 怎么推广公司网站做直播导航网站
  • 石家庄市环保局网站建设项目备案系统wordpress 评论贴图
  • 做国外的营销的网站vr网站建设
  • 民治营销型网站上国外的网站很慢
  • 自己做的网页加在网站文章上为什么打不开游戏网站排行
  • 电子商务网站怎么做推广创意界面
  • 北海住房和城乡建设局网站网站设计宽屏尺寸
  • 网站大全app下载代理记账包含哪些业务