std::lock用于同时获取多把锁(两把及两把以上)
std::scoped_lock定义于c++ 17标准,是std::lock的RAII封装类
以下代码纯来自msvc实现
std::scoped_lock原型
在构造函数中调用std::lock,在析构函数中unlock
class _NODISCARD_LOCK scoped_lock {
// class with destructor that unlocks mutexes
public:
explicit scoped_lock(_Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) {
// construct and lock
_STD lock(_Mtxes...);
}
explicit scoped_lock(adopt_lock_t, _Mutexes&... _Mtxes) noexcept // strengthened
: _MyMutexes(_Mtxes...) {
} // construct but don't lock
~scoped_lock() noexcept {
_STD apply([](_Mutexes&... _Mtxes) {
(..., (void) _Mtxes.unlock()); }, _MyMutexes);
}
scoped_lock(const scoped_lock&) = delete;
scoped_lock& operator=(const scoped_lock&) = delete;
private:
tuple<_Mutexes&...> _MyMutexes;
};
lock函数原型
这里我画了一个流程图,std::lock调用层次_Lock_nonmember1->_Lock_attempt_small
如果不能将所有的锁全部获取就会将已经获取的锁全部释放后再次尝试获取
void lock(_Lock0& _Lk0, _Lock1& _Lk1, _LockN&... _LkN) {
// lock multiple locks, without deadlock
_Lock_nonmember1(_Lk0, _Lk1, _LkN...);
}
template <class _Lock0, class _Lock1>
void _Lock_nonmember1(_Lock0& _Lk0, _Lock1& _Lk1) {
// lock 2 locks, without deadlock, special case for better codegen and reduced metaprogramming for common case
while (_Lock_attempt_small(_Lk0, _Lk1) && _Lock_attempt_small(_Lk1, _Lk0)) {
// keep trying
}
}
template <class _Lock0, class _Lock1>
bool _Lock_attempt_small(_Lock0& _Lk0, _Lock1& _Lk1) {
// attempt to lock 2 locks, by first locking _Lk0, and then trying to lock _Lk1 returns whether to try again
_Lk0.lock();
{
_Unlock_one_guard<_Lock0> _Guard{
_Lk0};
if (_Lk1.try_lock()) {
_Guard._Lk_ptr = nullptr;
return false;
}
}
_STD this_thread::yield();
return true;
}