MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
shared_mutex.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_SHARED_MUTEX_HPP__
2#define MSTL_CORE_ASYNC_SHARED_MUTEX_HPP__
3
10
11#include "mutex.hpp"
13
19
27class MSTL_API shared_mutex {
28public:
33#ifdef MSTL_PLATFORM_WINDOWS__
34 ::SRWLOCK;
35#else
36 ::pthread_rwlock_t;
37#endif
38
39private:
40 mutable native_handle_type shared_mutex_;
41
42public:
47
52
53 shared_mutex(const shared_mutex&) = delete;
54 shared_mutex& operator =(const shared_mutex&) = delete;
55
61 return &shared_mutex_;
62 }
63
68 const native_handle_type* native_handle() const noexcept {
69 return &shared_mutex_;
70 }
71
78 void lock();
79
85 void unlock();
86
93 bool try_lock() noexcept;
94
102
109
116 bool try_lock_shared() noexcept;
117};
118
126template <typename SharedMutex>
128public:
129 using mutex_type = SharedMutex;
130
131private:
132 mutex_type* mutex_ = nullptr;
133 bool owns_lock_ = false;
134
135public:
141 shared_lock() = default;
142
150 : mutex_(&m), owns_lock_(true) {
151 mutex_->lock_shared();
152 }
153
162 : mutex_(&m) {}
163
172 : mutex_(&m), owns_lock_(m.try_lock_shared()) {}
173
174 shared_lock(const shared_lock&) = delete;
175 shared_lock& operator =(const shared_lock&) = delete;
176
183 shared_lock(shared_lock&& other) noexcept
184 : mutex_(other.mutex_), owns_lock_(other.owns_lock_) {
185 other.mutex_ = nullptr;
186 other.owns_lock_ = false;
187 }
188
196 shared_lock& operator =(shared_lock&& other) noexcept {
197 if (_MSTL addressof(other) == this) return *this;
198 if (owns_lock_) mutex_->unlock_shared();
199 mutex_ = other.mutex_;
200 owns_lock_ = other.owns_lock_;
201 other.mutex_ = nullptr;
202 other.owns_lock_ = false;
203 return *this;
204 }
205
212 if (owns_lock_) mutex_->unlock_shared();
213 }
214
219 MSTL_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
220
225 MSTL_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
226
231 MSTL_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
232
238 void lock() {
239 if (!mutex_) return;
240 if (owns_lock_) return;
241 mutex_->lock_shared();
242 owns_lock_ = true;
243 }
244
250 void unlock() {
251 if (!mutex_) return;
252 if (!owns_lock_) return;
253 mutex_->unlock_shared();
254 owns_lock_ = false;
255 }
256
263 bool try_lock() noexcept {
264 if (!mutex_) return false;
265 if (owns_lock_) return true;
266 owns_lock_ = mutex_->try_lock_shared();
267 return owns_lock_;
268 }
269
276 mutex_type* release() noexcept {
277 mutex_type* ret = mutex_;
278 mutex_ = nullptr;
279 owns_lock_ = false;
280 return ret;
281 }
282};
283 // Mutex
285
287#endif // MSTL_CORE_ASYNC_SHARED_MUTEX_HPP__
共享锁类模板
MSTL_NODISCARD mutex_type * mutex() const noexcept
获取管理的共享互斥锁指针
~shared_lock()
析构函数
SharedMutex mutex_type
共享互斥锁类型
shared_lock(shared_lock &&other) noexcept
移动构造函数
mutex_type * release() noexcept
释放所有权
shared_lock(mutex_type &m, defer_lock_tag tag) noexcept
延迟锁定构造函数
shared_lock()=default
默认构造函数
bool try_lock() noexcept
尝试获取读锁
void lock()
获取读锁
void unlock()
释放读锁
MSTL_NODISCARD bool owns_lock() const noexcept
检查是否拥有共享锁
shared_lock(mutex_type &m)
从共享互斥锁构造
shared_lock(mutex_type &m, try_lock_tag tag) noexcept
尝试锁定构造函数
void unlock_shared()
释放读锁
native_handle_type * native_handle() noexcept
获取原生句柄
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
~shared_mutex()
析构函数
::pthread_rwlock_t native_handle_type
共享互斥锁的系统句柄类型
shared_mutex()
构造函数
void lock()
获取写锁
bool try_lock() noexcept
尝试获取写锁
void lock_shared()
获取读锁
bool try_lock_shared() noexcept
尝试获取读锁
void unlock()
释放写锁
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL互斥锁
延迟锁定标签
尝试锁定标签