c++ - 为什么lock_guard需要Lockable概念

标签 c++ multithreading c++11

对于lock_guard来说,BasicLockable概念似乎已经足够了,但是在标准草案(N3242)中,Mutex模板参数需要Lockable。有什么理由吗?

最佳答案

在 n3290 (§30.4.2.1[thread.lock.guard]/1) 中,它说,

An object of type lock_guard controls the ownership of a lockable object within a scope. A lock_guard object maintains ownership of a lockable object throughout the lock_guard object’s lifetime (3.8). The behavior of a program is undefined if the lockable object referenced by pm does not exist for the entire lifetime of the lock_guard object. The supplied Mutex type shall meet the BasicLockable requirements (30.2.5.2).

n3242 中的第二段可能是编辑错误。这是library defect 2023: Incorrect requirements for lock_guard and unique_lock ,并在n3290中删除该段落,如n3278: Recent Concurrency Issue Resolutions所示

关于c++ - 为什么lock_guard需要Lockable概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13901136/

相关文章:

c++ - 如何将成员函数指针传递给 std::function

Python (rospy) 到 C++ (roscpp) struct.unpack

c++ - 好友功能失效

c++ - 从这个对象的方法开始的线程中销毁类对象的最佳实践

c++ - cplusplus.com 错了吗?指针和字符串文字

c++ - CLion STL 容器无法在 Fedora 23 上的调试器中正确显示

c++ - 下面这句话在c++中是什么意思

c++ - Constexpr 变量和除法

c++ - Boost 的无序容器对于读取来说是线程安全的吗?

c# - 简单的线程问题,锁定对共享资源或整个功能的访问?