libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
readwritelock.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <shared_mutex>
12
14
24 public:
28 class ReadLocker {
29 public:
34 ReadLocker(ReadWriteLock &lock) : _lock(lock) {
35 _lock.lockForRead();
36 }
37
40 _lock.unlock();
41 }
42
43 ReadLocker(const ReadLocker &) = delete;
44 ReadLocker &operator=(const ReadLocker &) = delete;
45 ReadLocker(ReadLocker &&) = delete;
46 ReadLocker &operator=(ReadLocker &&) = delete;
47
48 private:
49 ReadWriteLock &_lock;
50 };
51
56 public:
61 WriteLocker(ReadWriteLock &lock) : _lock(lock) {
62 _lock.lockForWrite();
63 }
64
67 _lock.unlock();
68 }
69
70 WriteLocker(const WriteLocker &) = delete;
71 WriteLocker &operator=(const WriteLocker &) = delete;
72 WriteLocker(WriteLocker &&) = delete;
73 WriteLocker &operator=(WriteLocker &&) = delete;
74
75 private:
76 ReadWriteLock &_lock;
77 };
78
80 ReadWriteLock() = default;
81
83 ~ReadWriteLock() = default;
84
85 ReadWriteLock(const ReadWriteLock &) = delete;
86 ReadWriteLock &operator=(const ReadWriteLock &) = delete;
87 ReadWriteLock(ReadWriteLock &&) = delete;
88 ReadWriteLock &operator=(ReadWriteLock &&) = delete;
89
91 void lockForRead() {
92 _mutex.lock_shared();
93 }
94
96 void lockForWrite() {
97 _mutex.lock();
98 }
99
101 void unlock() {
102 _mutex.unlock();
103 }
104
110 return _mutex.try_lock_shared();
111 }
112
118 return _mutex.try_lock();
119 }
120
121 private:
122 std::shared_mutex _mutex;
123};
124
RAII scoped locker for shared (read) access.
Definition readwritelock.h:28
~ReadLocker()
Destructor. Releases the shared lock.
Definition readwritelock.h:39
ReadLocker(ReadWriteLock &lock)
Constructs a ReadLocker and acquires a shared lock.
Definition readwritelock.h:34
RAII scoped locker for exclusive (write) access.
Definition readwritelock.h:55
WriteLocker(ReadWriteLock &lock)
Constructs a WriteLocker and acquires an exclusive lock.
Definition readwritelock.h:61
~WriteLocker()
Destructor. Releases the exclusive lock.
Definition readwritelock.h:66
Reader-writer lock wrapping std::shared_mutex.
Definition readwritelock.h:23
void lockForRead()
Acquires a shared (read) lock. Multiple readers may hold this concurrently.
Definition readwritelock.h:91
void unlock()
Releases the currently held lock (shared or exclusive).
Definition readwritelock.h:101
bool tryLockForWrite()
Attempts to acquire an exclusive (write) lock without blocking.
Definition readwritelock.h:117
~ReadWriteLock()=default
Destructor.
ReadWriteLock()=default
Constructs an unlocked ReadWriteLock.
void lockForWrite()
Acquires an exclusive (write) lock. Blocks until all readers and writers release.
Definition readwritelock.h:96
bool tryLockForRead()
Attempts to acquire a shared (read) lock without blocking.
Definition readwritelock.h:109
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19