15 #include <condition_variable> 93 while (_num_readers != 0) {
94 std::this_thread::yield();
105 if (!_write_mutex.try_lock()) {
111 if (_num_readers == 0) {
114 _write_mutex.unlock();
125 _write_mutex.unlock();
133 while (_has_writer) {
136 std::lock_guard<std::mutex>{_write_mutex};
144 while (_has_writer) {
148 std::lock_guard<std::mutex>{_write_mutex};
186 std::atomic<int> _num_readers{0};
187 std::atomic<bool> _has_writer{
false};
188 std::mutex _write_mutex;
210 if (_num_readers != 0) {
211 std::unique_lock<std::mutex>
lock{_reader_done_mutex};
212 _reader_done_cond.wait(
lock, [=]{
return _num_readers==0; });
223 if (!_write_mutex.try_lock()) {
229 if (_num_readers == 0) {
232 _write_mutex.unlock();
245 _write_mutex.unlock();
253 while (_has_writer) {
256 std::lock_guard<std::mutex>
lock{_write_mutex};
264 while (_has_writer) {
268 std::lock_guard<std::mutex>
lock{_reader_done_mutex};
272 _reader_done_cond.notify_one();
274 std::lock_guard<std::mutex>{_write_mutex};
307 _reader_done_cond.notify_one();
317 std::atomic<int> _num_readers{0};
318 std::atomic<bool> _has_writer{
false};
319 std::mutex _write_mutex;
320 std::condition_variable _reader_done_cond;
321 std::mutex _reader_done_mutex;
327 template<
typename MutexType>
331 explicit ReadLock(MutexType& mut) : _rw_mutex(mut)
337 ReadLock(MutexType& mut, std::defer_lock_t) : _rw_mutex(mut)
347 _rw_mutex.lock_shared();
356 _locked = _rw_mutex.try_lock_shared();
365 _rw_mutex.unlock_shared();
376 MutexType& _rw_mutex;
383 template<
typename MutexType>
387 explicit WriteLock(MutexType& mut) : _rw_mutex(mut)
393 WriteLock(MutexType& mut, std::defer_lock_t) : _rw_mutex(mut)
412 _locked = _rw_mutex.try_lock();
432 MutexType& _rw_mutex;
bool try_lock()
Definition: read_write_mutex.hpp:103
void unlock()
unlock, unless already unlocked.
Definition: read_write_mutex.hpp:418
void lock()
Lock, unless already locked.
Definition: read_write_mutex.hpp:344
bool try_lock_shared()
Definition: read_write_mutex.hpp:282
Definition: read_write_mutex.hpp:77
void unlock()
Definition: read_write_mutex.hpp:122
bool try_lock()
Definition: read_write_mutex.hpp:221
bool try_lock()
Does not block. Returns true iff the mutex is locked by this thread after the call.
Definition: read_write_mutex.hpp:353
Definition: read_write_mutex.hpp:196
WriteLock(MutexType &mut, std::defer_lock_t)
Won't lock right away.
Definition: read_write_mutex.hpp:393
void lock()
Definition: read_write_mutex.hpp:86
void unlock_shared()
Definition: read_write_mutex.hpp:301
void unlock_shared()
Definition: read_write_mutex.hpp:175
This is a drop-in replacement for C++14's std::shared_lock.
Definition: read_write_mutex.hpp:328
ReadLock(MutexType &mut, std::defer_lock_t)
Won't lock right away.
Definition: read_write_mutex.hpp:337
bool try_lock_shared()
Definition: read_write_mutex.hpp:156
void lock()
Lock, unless already locked.
Definition: read_write_mutex.hpp:400
This is a drop-in replacement for C++11's std::unique_lock.
Definition: read_write_mutex.hpp:384
void lock_shared()
Definition: read_write_mutex.hpp:131
void unlock()
unlock, unless already unlocked.
Definition: read_write_mutex.hpp:362
bool try_lock()
Does not block. Returns true iff the mutex is locked by this thread after the call.
Definition: read_write_mutex.hpp:409
void lock_shared()
Definition: read_write_mutex.hpp:251
void lock()
Definition: read_write_mutex.hpp:204
Definition: coroutine.hpp:18