原文作者:@玄冬Wong

转载请注明原文出处:http://aigo.iteye.com/blog/2296462

key world: std::shared_mutex、std::mutex、performance、benchmark、性能测试

shared_mutex的适用场景比较特殊:一个或多个读线程同时读取共享资源,且只有一个写线程来修改这个资源,这种情况下才能从shared_mutex获取性能优势。

cppreference文档

http://en.cppreference.com/w/cpp/thread/shared_mutex

Shared mutexes are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so.

测试代码:

注意,VC第一个支持shared_mutex的版本是VS2015 update2

测试结果:

2线程抢占

[test_mutex]

thread count:2

result:10000000 cost:1348ms temp:10000000

[test_shared_mutex]

thread count:2

result:10000000 cost:699ms temp:10000000

4线程抢占

[test_mutex]

thread count:4

result:10000000 cost:2448ms temp:10000000

[test_shared_mutex]

thread count:4

result:10000000 cost:1233ms temp:10000000

8线程抢占

[test_mutex]

thread count:8

result:5000000 cost:2452ms temp:5000000

[test_shared_mutex]

thread count:8

result:5000000 cost:1123ms temp:3231860

结论:

在多个只读线程和一个写线程的情况下,shared_mutex比mutex快一倍。

PS:std::shared_mutex和std::mutex分别对应java中的ReentrantReadWriteLockReentrantLock

最新文章

  1. Linux下反弹shell的种种方式
  2. Golang Import使用入门
  3. JVM之类加载器上篇
  4. struts2 结果页面配置
  5. 用VNC远程图形化连接Linux桌面的配置方法
  6. 【转】类中如何引用server.MapPath()
  7. 【阿里云产品公测】阿里云ACE配置全程图解,详细到不行!
  8. (转)VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
  9. oracle定时执行一个存储过程
  10. Linux内核源码分析 day01——内存寻址
  11. 微信里iphone后退不刷新问题解决方案
  12. Codeforces831A Unimodal Array
  13. fiddler 学习笔记1-下载安装、开启、关闭抓包功能
  14. 转: hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
  15. Node_初步了解(4)小爬虫
  16. Unity之AssetBundle打包
  17. Unity 3D光源-Spot Light聚光灯用法详解、模拟手电筒、台灯等线性教程
  18. Lagrange 乘子法求最优解
  19. GIS原理学习目录
  20. js 数组的深度递归搜索

热门文章

  1. mysql 用户创建,授权
  2. IBM MQ V6.0 for Windows7
  3. Webpack快速入门
  4. 关于List集合中元素排序问题
  5. 使用C#的HttpWebRequest模拟登陆访问人人网(转)
  6. SpringBoot官方文档学习(三)配置文件、日志、国际化和JSON
  7. jupyter安装小结
  8. MySQL 数据库,主键为何不宜太长长长长长长长长?
  9. Kubernetes 学习14 kubernetes statefulset
  10. cf 799E