• C++ 实现读写锁的示例


    下面是一个简单的 C++ 实现读写锁的示例:
    
    #include 
    #include 
    #include 
    
    class ReadWriteLock {
    private:
        std::mutex mutex_;
        std::condition_variable read_cv_;
        std::condition_variable write_cv_;
        int readers_;
        bool writing_;
    
    public:
        ReadWriteLock() : readers_(0), writing_(false) {}
    
        void lockRead() {
            std::unique_lock<std::mutex> lock(mutex_);
            // 如果有线程正在进行写操作或者等待写操作,则阻塞等待
            while (writing_) {
                read_cv_.wait(lock);
            }
            readers_++;
        }
    
        void unlockRead() {
            std::unique_lock<std::mutex> lock(mutex_);
            readers_--;
            if (readers_ == 0) {
                write_cv_.notify_one(); // 唤醒等待写操作的线程
            }
        }
    
        void lockWrite() {
            std::unique_lock<std::mutex> lock(mutex_);
            // 如果有线程正在读或写,则阻塞等待
            while (writing_ || readers_ > 0) {
                write_cv_.wait(lock);
            }
            writing_ = true;
        }
    
        void unlockWrite() {
            std::unique_lock<std::mutex> lock(mutex_);
            writing_ = false;
            read_cv_.notify_all();  // 唤醒等待读操作的线程
            write_cv_.notify_one(); // 唤醒等待写操作的线程
        }
    };
    
    // 示例用法
    ReadWriteLock rwLock;
    
    void readerFunction(int id) {
        while (true) {
            rwLock.lockRead();
            // 执行读取操作
            std::cout << "Reader " << id << " is reading" << std::endl;
            rwLock.unlockRead();
            // 休眠一段时间模拟读取过程
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }
    }
    
    void writerFunction(int id) {
        while (true) {
            rwLock.lockWrite();
            // 执行写入操作
            std::cout << "Writer " << id << " is writing" << std::endl;
            rwLock.unlockWrite();
            // 休眠一段时间模拟写入过程
            std::this_thread::sleep_for(std::chrono::milliseconds(2000));
        }
    }
    
    int main() {
        // 创建多个读者线程和写者线程进行测试
        std::thread readers[3];
        std::thread writers[2];
    
        for (int i = 0; i < 3; i++) {
            readers[i] = std::thread(readerFunction, i);
        }
    
        for (int i = 0; i < 2; i++) {
            writers[i] = std::thread(writerFunction, i);
        }
    
        // 等待所有线程结束
        for (auto& reader : readers) {
            reader.join();
        }
    
        for (auto& writer : writers) {
            writer.join();
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    在上述示例中,ReadWriteLock 类实现了读写锁的基本功能。lockRead()unlockRead() 方法用于获取和释放读锁,lockWrite()unlockWrite() 方法用于获取和释放写锁。

    在主函数中,创建了多个读者线程和写者线程来模拟并发读写操作。读者线程使用 lockRead()unlockRead() 获取和释放读锁,写者线程使用 lockWrite()unlockWrite() 获取和释放写锁。

    通过使用互斥量std::mutex)、条件变量(std::condition_variable)以及共享的计数器和标志位来实现读写锁的功能,并确保读者和写者之间的互斥和同步。

  • 相关阅读:
    【python基础】函数-值传递
    PHP 大文件分块上传 底层实现
    Hybrid-PSC:基于对比学习的混合网络,解决长尾图片分类 | CVPR 2021
    分享一下短视频创作前需要了解的内容和注意事项
    arduino使用esp8266开发板使用https方式读取哔哩哔哩网页粉丝数等
    47-5 内网渗透 - 提权环境搭建
    MAC下虚拟机PD转换成win上可以用的vmware
    应用系统中的报表开发成本值多少?
    分布式机器学习:PageRank算法的并行化实现(PySpark)
    安卓 实现60s倒计时的CountDownTimer(小坑)
  • 原文地址:https://blog.csdn.net/m0_46376834/article/details/133868466