//----------------------------------------------- // stallings-m.h: reader-priority //----------------------------------------------- #ifndef stallings_m_h #define stallings_m_h //----------------------------------------------- #include // std::size_t #include // std::mutex //----------------------------------------------- class stallings_m_lock { public: stallings_m_lock(std::size_t): readers(0) {} void reader_lock() { std::unique_lock lock(reader_mutex); if (++readers == 1) writer_mutex.lock(); } void reader_unlock() { std::unique_lock lock(reader_mutex); if (--readers == 0) writer_mutex.unlock(); } void writer_lock() { writer_mutex.lock(); } void writer_unlock() { writer_mutex.unlock(); } private: std::mutex reader_mutex, writer_mutex; std::size_t readers; }; //----------------------------------------------- #endif // stallings_m_h