很简单就是在读的时候把写的锁锁住就好了

class readwrite_lock  
{  
public:  
    readwrite_lock()  
        : read_cnt(0)  
    {  
    }  
  
    void readLock()  
    {  
        read_mtx.lock();  
        if (++read_cnt == 1)  
            write_mtx.lock();  
  
        read_mtx.unlock();  
    }  
  
    void readUnlock()  
    {  
        read_mtx.lock();  
        if (--read_cnt == 0)  
            write_mtx.unlock();  
  
        read_mtx.unlock();  
    }  
  
    void writeLock()  
    {  
        write_mtx.lock();  
    }  
  
    void writeUnlock()  
    {  
        write_mtx.unlock();  
    }  
  
private:  
    mutex read_mtx;  
    mutex write_mtx;  
    int read_cnt; // 已加读锁个数  
};  

  

相关文章:

  • 2020-04-28
  • 2022-12-23
  • 2021-09-11
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-26
  • 2021-06-06
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案