Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RW_mutex{
- public:
- RW_mutex() : writers(0), readers(0), writing(false) {}
- void write_lock(){
- std::unique_lock<std::mutex> lock(gate);
- writers++;
- while (writing || readers > 0){
- room_empty.wait(lock);
- }
- writing = true;
- }
- void read_lock(){
- std::unique_lock<std::mutex> lock(gate);
- while (writers > 0){
- room_empty.wait(lock);
- }
- readers++;
- }
- void write_unlock(){
- std::unique_lock<std::mutex> lock(gate);
- writers--;
- writing = false;
- room_empty.notify_all();
- }
- void read_unlock(){
- std::unique_lock<std::mutex> lock(gate);
- readers--;
- if (readers == 0) room_empty.notify_all();
- }
- private:
- std::mutex gate;
- std::size_t writers;
- std::size_t readers;
- std::condition_variable room_empty;
- bool writing;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement