Reader 写完 Reader writer lock 后不会唤醒
Reader doesn't wake up once writer finishes writting in Reader writer lock
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 实现 reader 写入器锁。线程 2 首先写入,线程 0 和线程 1 等待线程 2 完成写入但是一旦线程 2 完成写入线程 1 和线程 0 没有醒来 read.can 有人帮我解决这个问题?我是 C++ 同步新手
此代码在 writerRelease()
或 readerRealease()
去弯曲上产生死锁,这是第一次获得。
如何找到?
多线程代码很难调试。我建议您在此处添加一些日志记录,以显示何时输入 aquire/release 函数以及何时锁定互斥锁。
例如:
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()
都卡住了,互相等待。
如何解决?
好吧,死锁是一件非常讨厌的事情。
有一点很有帮助,那就是始终以相同的顺序对多个对象执行锁定。然后三分之一可能最终无法锁定,但不会有 "kiss of death"。
更具体地说,查看您的代码,我的印象是您的 readWriteMutex
主要用于防止 3 个计数器出现竞争条件。我建议摆脱这个互斥体并改用原子变量。
然后在 ReleaseReadLock() 中,您应该在通知等待线程之前减少读取器的数量。通过这两个措施,我可以 运行 几次而不会死锁(这并不能证明它是完美的,但至少避免了最明显的情况。由你决定 analyse/verify 详细)。
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;
}
};
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 实现 reader 写入器锁。线程 2 首先写入,线程 0 和线程 1 等待线程 2 完成写入但是一旦线程 2 完成写入线程 1 和线程 0 没有醒来 read.can 有人帮我解决这个问题?我是 C++ 同步新手
此代码在 writerRelease()
或 readerRealease()
去弯曲上产生死锁,这是第一次获得。
如何找到?
多线程代码很难调试。我建议您在此处添加一些日志记录,以显示何时输入 aquire/release 函数以及何时锁定互斥锁。
例如:
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()
都卡住了,互相等待。
如何解决?
好吧,死锁是一件非常讨厌的事情。
有一点很有帮助,那就是始终以相同的顺序对多个对象执行锁定。然后三分之一可能最终无法锁定,但不会有 "kiss of death"。
更具体地说,查看您的代码,我的印象是您的 readWriteMutex
主要用于防止 3 个计数器出现竞争条件。我建议摆脱这个互斥体并改用原子变量。
然后在 ReleaseReadLock() 中,您应该在通知等待线程之前减少读取器的数量。通过这两个措施,我可以 运行 几次而不会死锁(这并不能证明它是完美的,但至少避免了最明显的情况。由你决定 analyse/verify 详细)。
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;
}
};