c++11的mutex unique_lock和lock_guard区别

 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 TimedLockable mutex, returns if the mutex has been unavailable for the specified time duration 
(public member function)
tries to lock the associated TimedLockable 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则可以控制加锁的等待时间,此时这种锁为乐观锁。

    以上为粗浅理解,后面有新的心得在补充之。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值