unique_lock
unique_lock在VS中的代码
1 template <class _Mutex> 2 class unique_lock { // whizzy class with destructor that unlocks mutex 3 public: 4 using mutex_type = _Mutex; 5 6 // CONSTRUCT, ASSIGN, AND DESTROY 7 unique_lock() noexcept : _Pmtx(nullptr), _Owns(false) {} 8 9 explicit unique_lock(_Mutex& _Mtx) : _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // construct and lock 10 _Pmtx->lock(); 11 _Owns = true; 12 } 13 14 unique_lock(_Mutex& _Mtx, adopt_lock_t) 15 : _Pmtx(_STD addressof(_Mtx)), _Owns(true) { // construct and assume already locked 16 } 17 18 unique_lock(_Mutex& _Mtx, defer_lock_t) noexcept 19 : _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // construct but don't lock 20 } 21 22 unique_lock(_Mutex& _Mtx, try_to_lock_t) 23 : _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock()) { // construct and try to lock 24 } 25 26 template <class _Rep, class _Period> 27 unique_lock(_Mutex& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time) 28 : _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock_for(_Rel_time)) { // construct and lock with timeout 29 } 30 31 template <class _Clock, class _Duration> 32 unique_lock(_Mutex& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time) 33 : _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock_until(_Abs_time)) { // construct and lock with timeout 34 } 35 36 unique_lock(_Mutex& _Mtx, const xtime* _Abs_time) 37 : _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // try to lock until _Abs_time 38 _Owns = _Pmtx->try_lock_until(_Abs_time); 39 } 40 41 unique_lock(unique_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) { 42 _Other._Pmtx = nullptr; 43 _Other._Owns = false; 44 } 45 46 unique_lock& operator=(unique_lock&& _Other) { 47 if (this != _STD addressof(_Other)) { 48 if (_Owns) { 49 _Pmtx->unlock(); 50 } 51 52 _Pmtx = _Other._Pmtx; 53 _Owns = _Other._Owns; 54 _Other._Pmtx = nullptr; 55 _Other._Owns = false; 56 } 57 return *this; 58 } 59 60 ~unique_lock() noexcept { 61 if (_Owns) { 62 _Pmtx->unlock(); 63 } 64 } 65 66 unique_lock(const unique_lock&) = delete; 67 unique_lock& operator=(const unique_lock&) = delete; 68 69 void lock() { // lock the mutex 70 _Validate(); 71 _Pmtx->lock(); 72 _Owns = true; 73 } 74 75 _NODISCARD bool try_lock() { 76 _Validate(); 77 _Owns = _Pmtx->try_lock(); 78 return _Owns; 79 } 80 81 template <class _Rep, class _Period> 82 _NODISCARD bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) { 83 _Validate(); 84 _Owns = _Pmtx->try_lock_for(_Rel_time); 85 return _Owns; 86 } 87 88 template <class _Clock, class _Duration> 89 _NODISCARD bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) { 90 _Validate(); 91 _Owns = _Pmtx->try_lock_until(_Abs_time); 92 return _Owns; 93 } 94 95 _NODISCARD bool try_lock_until(const xtime* _Abs_time) { 96 _Validate(); 97 _Owns = _Pmtx->try_lock_until(_Abs_time); 98 return _Owns; 99 } 100 101 void unlock() { 102 if (!_Pmtx || !_Owns) { 103 _Throw_system_error(errc::operation_not_permitted); 104 } 105 106 _Pmtx->unlock(); 107 _Owns = false; 108 } 109 110 void swap(unique_lock& _Other) noexcept { 111 _STD swap(_Pmtx, _Other._Pmtx); 112 _STD swap(_Owns, _Other._Owns); 113 } 114 115 _Mutex* release() noexcept { 116 _Mutex* _Res = _Pmtx; 117 _Pmtx = nullptr; 118 _Owns = false; 119 return _Res; 120 } 121 122 _NODISCARD bool owns_lock() const noexcept { 123 return _Owns; 124 } 125 126 explicit operator bool() const noexcept { 127 return _Owns; 128 } 129 130 _NODISCARD _Mutex* mutex() const noexcept { 131 return _Pmtx; 132 } 133 134 private: 135 _Mutex* _Pmtx; 136 bool _Owns; 137 138 void _Validate() const { // check if the mutex can be locked 139 if (!_Pmtx) { 140 _Throw_system_error(errc::operation_not_permitted); 141 } 142 143 if (_Owns) { 144 _Throw_system_error(errc::resource_deadlock_would_occur); 145 } 146 } 147 };
unique_lock包含两个私有变量 _Mutex* _Pmtx; bool _Owns;
_pmtx是一个mutex,_Owns表示是否已上锁

浙公网安备 33010602011771号