一、简介
读写锁(Read-Write Lock)是一种并发控制机制,用于多线程环境中实现对共享资源的高效读写操作。读写锁允许多个线程同时读取共享资源,但在有写操作时,需要互斥地独占对共享资源的访问,以确保数据的一致性。
二、shared_mutex
std::shared_mutex 是 C++17 引入的,用于实现共享/独占访问控制,以下是简单示例:
#include <iostream>
#include <thread>
#include <shared_mutex>std::shared_mutex rwLock;
int sharedData = 0; // 共享资源void readerFunction(int id) {while (true) {rwLock.lock_shared();std::cout << "Reader " << id << " reads sharedData: " << sharedData << std::endl;rwLock.unlock_shared();// 模拟一些耗时操作std::this_thread::sleep_for(std::chrono::milliseconds(500));}
}void writerFunction(int id) {while (true) {rwLock.lock();sharedData++;std::cout << "Writer " << id << " writes sharedData: " << sharedData << std::endl;rwLock.unlock();// 模拟一些耗时操作std::this_thread::sleep_for(std::chrono::milliseconds(1000));}
}int main() {const int numReaders = 3;const int numWriters = 3;std::thread readers[numReaders];std::thread writers[numWriters];// 启动线程for (int i = 0; i < numReaders; i++) {readers[i] = std::thread(readerFunction, i);}for (int i = 0; i < numWriters; i++) {writers[i] = std::thread(writerFunction, i);}// Join线程for (int i = 0; i < numReaders; i++) {readers[i].join();}for (int i = 0; i < numWriters; i++) {writers[i].join();}return 0;
}
三、自定义 ReadWriteLock
C++17前可以通过 mutex
和 condition_variable
实现一个自定义的读写锁。代码如下:
#include <mutex>
#include <condition_variable>class ReadWriteLock {
public:ReadWriteLock() : readersCount(0), writing(false) {}void lockRead() {std::unique_lock<std::mutex> lock(mutex_);readCondition_.wait(lock, [this] { return !writing; });readersCount++;}void unlockRead() {std::unique_lock<std::mutex> lock(mutex_);readersCount--;if (readersCount == 0) {writeCondition_.notify_one();}}void lockWrite() {std::unique_lock<std::mutex> lock(mutex_);writeCondition_.wait(lock, [this] { return readersCount == 0 && !writing; });writing = true;}void unlockWrite() {std::unique_lock<std::mutex> lock(mutex_);writing = false;readCondition_.notify_all();writeCondition_.notify_one();}private:std::mutex mutex_;std::condition_variable readCondition_;std::condition_variable writeCondition_;int readersCount;bool writing;
};
使用示例:
#include <iostream>
#include <thread>
#include "ReadWriteLock.hpp" // 包含上面ReadWriteLock的头文件int sharedData = 0; // 共享数据
ReadWriteLock rwLock;void readerFunction(int id) {while (true) {rwLock.lockRead();std::cout << "Reader " << id << " reads sharedData: " << sharedData << std::endl;rwLock.unlockRead();// 模拟一些耗时操作std::this_thread::sleep_for(std::chrono::milliseconds(500));}
}void writerFunction(int id) {while (true) {rwLock.lockWrite();sharedData++;std::cout << "Writer " << id << " writes sharedData: " << sharedData << std::endl;rwLock.unlockWrite();// 模拟一些耗时操作std::this_thread::sleep_for(std::chrono::milliseconds(1000));}
}int main() {const int numReaders = 3;const int numWriters = 3;std::thread readers[numReaders];std::thread writers[numWriters];// 启动线程for (int i = 0; i < numReaders; i++) {readers[i] = std::thread(readerFunction, i);}for (int i = 0; i < numWriters; i++) {writers[i] = std::thread(writerFunction, i);}// Join线程for (int i = 0; i < numReaders; i++) {readers[i].join();}for (int i = 0; i < numWriters; i++) {writers[i].join();}return 0;
}