博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11中lock_guard和unique_lock的区别
阅读量:4449 次
发布时间:2019-06-07

本文共 805 字,大约阅读时间需要 2 分钟。

c++11中有一个区域锁lock_guard,还有第二个区域锁unique_lock。

区域锁lock_guard使用起来比较简单,除了构造函数外没有其他member function,在整个区域都有效。

区域锁unique_guard除了lock_guard的功能外,提供了更多的member_function,相对来说更灵活一些。

 unique_guard的最有用的一组函数为:

locks the associated mutex 
(public member function)
tries to lock the associated mutex, returns if the mutex is not available 
(public member function)
attempts to lock the associated  mutex, returns if the mutex has been unavailable for the specified time duration 
(public member function)
tries to lock the associated  mutex, returns if the mutex has been unavailable until specified time point has been reached 
(public member function)
unlocks the associated mutex 

 

通过上面的函数,可以通过lock/unlock可以比较灵活的控制锁的范围,减小锁的粒度。

通过try_lock_for/try_lock_until则可以控制加锁的等待时间,此时这种锁为乐观锁。

转载于:https://www.cnblogs.com/jiu0821/p/7269777.html

你可能感兴趣的文章
1.基础数据类型的初识 字符串 bool 整型 if else elif
查看>>
【设计模式】4、原型模式
查看>>
进入meta模式关闭背光灯
查看>>
轻松实现Ecshop商城多语言切换
查看>>
webstorm上svn的安装使用
查看>>
【JEECG技术文档】数据权限自定义SQL表达式用法说明
查看>>
使用 Bootstrap Typeahead 组件
查看>>
EF不能很好的支持DDD?估计是我们搞错了!
查看>>
ubuntu下基于sqlite3后台的php环境的搭建
查看>>
Qt 静态库与共享库(动态库)共享配置的一个小办法
查看>>
linux_cacti 配置之 安装snmp 服务
查看>>
201407-至今
查看>>
c# 应用事务
查看>>
优化杭州某著名电子商务网站高并发千万级大型数据库经验之- SQL语句优化(转)...
查看>>
DtCms.Model.Article.cs
查看>>
WPF——TargetNullValue(如何在绑定空值显示默认字符)
查看>>
Linux之crontab
查看>>
清除浮动
查看>>
JAVA优化建议
查看>>
Docker --- 安装MySQL
查看>>