gpt4 book ai didi

c++ - Reader 在 Reader writer lock 中完成写入后,Reader 不会醒来

转载 作者:搜寻专家 更新时间:2023-10-31 01:39:53 29 4
gpt4 key购买 nike

class ReadLock
{
private:
std::mutex readWriteMutex;
std::mutex conditionmtx;
std::condition_variable cv;
int readings = 0;
int writings = 0;
int writers = 0;
public:
void AquireReadLock()
{
readWriteMutex.lock();
if (writers)
{
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
while (writings)
{
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
readings++;
readWriteMutex.unlock();
}
void ReleaseReadLock()
{
readWriteMutex.lock();
//std::unique_lock<std::mutex> lck(conditionmtx);
cv.notify_all();
readings--;
readWriteMutex.unlock();
}
void AquireWriteLock()
{
readWriteMutex.lock();
writers++;
while (readings || writings)
{
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
writings++;
readWriteMutex.unlock();
}
void ReleaseWriteLock()
{
readWriteMutex.lock();
writings--;
writers--;;
//std::unique_lock<std::mutex> lck(conditionmtx);
cv.notify_all();
readWriteMutex.unlock();
}

};

ReadLock lock;
void WriteFunction(int id)
{
std::cout << "thread " + std::to_string(id) + " asks for write " << '\n';
lock.AquireWriteLock();
std::cout << "thread " + std::to_string(id) + " writting" << '\n';
std::this_thread::sleep_for(std::chrono::milliseconds(3500));
std::cout << "thread " + std::to_string(id) + " finished writting" << '\n';
lock.ReleaseWriteLock();
}
void ReadFunction(int id)
{
if (id == 0)
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "thread " + std::to_string(id) + " asks for read" << '\n';
lock.AquireReadLock();
std::cout << "thread " + std::to_string(id) + " reading" << '\n';
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
std::cout << "thread " + std::to_string(id) + " finished reading" << '\n';
lock.ReleaseReadLock();
}

int _tmain(int argc, _TCHAR* argv[])
{
std::thread threads[3];

for (int i = 0; i < 3; ++i)
if (i % 2 == 0)
threads[i] = std::thread(ReadFunction, i);
else
threads[i] = std::thread(WriteFunction, i);

for (auto& th : threads) th.join();

}

我正在尝试使用条件变量和 Mutex 实现读写锁。线程 2 首先写入,线程 0 和线程 1 等待线程 2 完成写入,但是一旦线程 2 完成写入,线程 1 和线程 0 就没有醒来读取。有人可以帮我解决这个问题吗?我是 C++ 同步的新手

最佳答案

此代码会在 writerRelease()readerRealease() 解除弯曲时产生死锁,这是第一次发现的。

如何找出?

多线程代码很难调试。我建议您在此处添加一些日志记录,以显示何时进入获取/释放功能以及何时锁定互斥锁。

例如:

void ReleaseReadLock()
{
cout <<this_thread::get_id()<< " will release ReadLock" << endl;
readWriteMutex.lock();
cout << this_thread::get_id() << " ...mutex locked" << endl;
cv.notify_all();
cout << this_thread::get_id() << " ...notified" << endl;
readings--;
readWriteMutex.unlock();
cout << this_thread::get_id()<<" released ReadLock " << endl;
}

使用这样的代码,您将观察到这种情况(或它的轻微变体):

5204 thread 1 asks for write
3692 thread 2 asks for read
5204 will aquire WriteLock ==> start write lock acquisition
3692 will aquire ReadLock ==> start read lock acquisition
3692 ...mutex locked ==> mutex was locked for read lock acquisition
3692 aquired ReadLock ==> mutex was unlocked : end read lock acquisition.
5204 ...mutex locked ==> mutext was locked for writelock
3692 thread 2 reading
5288 thread 0 asks for read
5288 will aquire ReadLock ==> another read lock will wait for mutex
3692 thread 2 finished reading
3692 will release ReadLock ==> Reader can't release lock because mutex is locked by writelock

会发生什么?

已成功获取释放锁。所以 readings 是 1 。

要减少此变量,releaseReadLock() 必须终止其工作。但它不能,因为它在函数开始时需要的互斥锁仍由 aquireWriteLock() 持有。所以它等待。

但是 aquireWriteLock() 陷入了一个循环,只要 readings 或 writings 就会继续循环。它将释放只有 readings 回到 0 的互斥锁。

简而言之,releaseReadLock()acquireWriteLock()都卡住了,互相等待。

如何解决?

好吧,死锁是一件非常讨厌的事情。

有一点很有帮助,那就是始终以相同的顺序对多个对象执行锁定。然后三分之一可能最终无法锁定,但不会有“死亡之吻”。

更具体地说,查看您的代码,我的印象是您的 readWriteMutex 主要用于防止 3 个计数器出现竞争条件。我建议摆脱这个互斥体并改用原子变量。

然后在 ReleaseReadLock() 中,您应该在通知等待线程之前减少读取器的数量。通过这两个措施,我可以多次运行而不会死锁(这并不能证明它是完美的,但至少避免了最明显的情况。具体由你分析/验证)。

class ReadLock
{
private:
std::mutex conditionmtx;
std::condition_variable cv;
atomic<int> readings = 0; // atomics don't need mutex for being updated
atomic<int> writings = 0;
atomic<int> writers = 0;
public:
void AquireReadLock()
{
cout << this_thread::get_id() << " will aquire ReadLock" << endl;
if(writers) {
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
while(writings) {
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
readings++;
cout << this_thread::get_id() << " aquired ReadLock" << endl;
}
void ReleaseReadLock()
{
cout <<this_thread::get_id()<< " will release ReadLock" << endl;
readings--;
cv.notify_all();
cout << this_thread::get_id()<<" released ReadLock " << endl;
}
void AquireWriteLock()
{
cout << this_thread::get_id() << " will aquire WriteLock" << endl;
writers++;
while(readings || writings) {
std::unique_lock<std::mutex> lck(conditionmtx);
cv.wait(lck);
}
writings++;
cout << this_thread::get_id() << " aquired WriteLock" << endl;
}
void ReleaseWriteLock()
{
cout << this_thread::get_id() << " will release WriteLock" << endl;
writings--;
writers--;;
cv.notify_all();
cout << this_thread::get_id() << " ...notified" << endl;
cout << this_thread::get_id() << " released WriteLock" << endl;
}
};

关于c++ - Reader 在 Reader writer lock 中完成写入后,Reader 不会醒来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515242/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com