Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <rwlock_traits.hpp>
- #include <tpcc/stdlike/condition_variable.hpp>
- #include <mutex>
- namespace tpcc {
- namespace solutions {
- class ReaderWriterLock {
- public:
- // Reader section
- void ReaderLock() {
- std::unique_lock<std::mutex> lock(mutex);
- while (writer)
- unlocked.wait(lock);
- readers++;
- }
- void ReaderUnlock() {
- std::unique_lock<std::mutex> lock(mutex);
- readers--;
- if (readers == 0)
- unlocked.notify_all();
- }
- // Writer section
- void WriterLock() {
- std::unique_lock<std::mutex> lock(mutex);
- while (writer || (readers > 0))
- unlocked.wait(lock);
- writer = true;
- }
- void WriterUnlock() {
- std::unique_lock<std::mutex> lock(mutex);
- writer = false;
- unlocked.notify_all();
- }
- private:
- bool writer{false};
- std::mutex mutex;
- tpcc::condition_variable unlocked;
- int readers{0};
- };
- } // namespace solutions
- } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment