博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
std::unique_lock<std::mutex> or std::lock_guard<std::mutex> C++11 区别
阅读量:7121 次
发布时间:2019-06-28

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

http://stackoverflow.com/questions/20516773/stdunique-lockstdmutex-or-stdlock-guardstdmutex

 

The difference is that you can lock and unlock a std::unique_lockstd::lock_guard will be locked only once on construction and unlocked on destruction.

So for usecase B you definitely need a std::unique_lock for the condition variable. In case A it depends whether you need to relock the guard.

std::unique_lock has other features that allow it to e.g.: be constructed without locking the mutex immediately but to build the RAII wrapper (see ).

Lock guards can be used when you simply need a wrapper for a limited scope, e.g.: a member function:

void member_foo() { std::lock_guard
lock(this->my_mutex); ... }

To clarify a question by chmike, by default std::lock_guard and std::unique_lock are the same. So in the above case, you could replace std::lock_guard with std::unique_lock. However, std::unique_lock might have a tad more overhead.

转载地址:http://vpiel.baihongyu.com/

你可能感兴趣的文章
亚信基于AWS构建世界级企业互联网平台
查看>>
达索系统成立“大土木工程达索系统BIM技术推进联盟”深化应用、共享经验
查看>>
猥琐思路复现Spring WebFlow远程代码执行
查看>>
开发平台怎么选?来看看专业人士怎么说
查看>>
移动设备尚未形成DDoS的3个原因
查看>>
《OpenGL编程指南(原书第9版)》——1.4 OpenGL渲染管线
查看>>
《中国人工智能学会通讯》——7.7 结束语
查看>>
勒索软件好多都使用恶意LNK链接文件欺骗用户 来看趋势科技分析新型LNK-PowerShell攻击...
查看>>
《数字逻辑设计与计算机组成》一 第2章 2.1 简介
查看>>
《并行计算的编程模型》一3.5 远程内存访问:put和get
查看>>
思博伦安全专家预测2017年民用和军用全球导航应用面临的更大风险
查看>>
勒索软件指向Flash与Silverlight漏洞
查看>>
人工智能项目正在起飞:这对未来的工作意味着什么?
查看>>
天时、地利、人和,技术成熟推动闪存联盟2.0落地
查看>>
五款可以取代 Slack 的开源工具
查看>>
如何将大数据变成企业的洞察力和行动力?
查看>>
新技术给数据中心带来新风险
查看>>
Spring核心框架体系结构
查看>>
换脸上阵的路由界新面孔,联想云路由动手玩
查看>>
浅谈浏览器缓存机制
查看>>