lock_guard的源代码就可以很轻易的总结它的特性
1.构造,析构是主要函数,用生命周期控制锁状态
2.无拷贝属性,默认拷贝函数被强制取消
3.成员为锁引用,这个是互斥的根本
template<class _Mutex>
class lock_guard
{ // class with destructor that unlocks mutex
public:
typedef _Mutex mutex_type;
explicit lock_guard(_Mutex& _Mtx)
: _MyMutex(_Mtx)
{ // construct and lock
_MyMutex.lock();
}
lock_guard(_Mutex& _Mtx, adopt_lock_t)
: _MyMutex(_Mtx)
{ // construct but don't lock
}
~lock_guard() _NOEXCEPT
{ // unlock
_MyMutex.unlock();
}
lock_guard(const lock_guard&) = delete;
lock_guard& operator=(const lock_guard&) = delete;
private:
_Mutex& _MyMutex;
};
1.构造,析构是主要函数,用生命周期控制锁状态
2.无拷贝属性,默认拷贝函数被强制取消
3.成员为锁引用,这个是互斥的根本
template<class _Mutex>
class lock_guard
{ // class with destructor that unlocks mutex
public:
typedef _Mutex mutex_type;
explicit lock_guard(_Mutex& _Mtx)
: _MyMutex(_Mtx)
{ // construct and lock
_MyMutex.lock();
}
lock_guard(_Mutex& _Mtx, adopt_lock_t)
: _MyMutex(_Mtx)
{ // construct but don't lock
}
~lock_guard() _NOEXCEPT
{ // unlock
_MyMutex.unlock();
}
lock_guard(const lock_guard&) = delete;
lock_guard& operator=(const lock_guard&) = delete;
private:
_Mutex& _MyMutex;
};