260*_*607 22 c++ multithreading boost mutex boost-thread
我对锁和互斥锁之间的区别感到非常困惑.在Boost文档中,它说,
锁定类型
互斥体类型
在另一篇文章中,我看到这样的函数,
boost::shared_mutex _access;
void reader()
{
boost::shared_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
}
void conditional_writer()
{
boost::upgrade_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
if (something) {
boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
// do work here, but now you have exclusive access
}
// do more work here, without anyone having exclusive access
}
Run Code Online (Sandbox Code Playgroud)
更新了问题
Ant*_*ams 39
甲互斥是同步对象.您在代码段的开头获取互斥锁,并在最后释放它,以确保没有其他线程同时访问相同的数据.互斥锁通常具有与其保护的数据相同的生命周期,并且多个线程可以访问一个互斥锁.
甲锁定对象是一个封装该锁的对象.构造对象时,它获取互斥锁上的锁.当它被破坏时,锁被释放.您通常会为每次访问共享数据创建一个新的锁定对象.
| 归档时间: |
|
| 查看次数: |
14830 次 |
| 最近记录: |