tmultireadexexclusivewritesynchronizer将写锁降级为读锁时的行为
Behaviour of TMultiReadExclusiveWriteSynchronizer when demoting write-lock to read-lock
下面的代码导致我的应用程序冻结,即使程序只有一个线程在运行。
semaphore
是boost::shared_ptr<TMultiReadExclusiveWriteSynchronizer>
。
if (semaphore)
{
semaphore->BeginWrite();
// Perform write operations on the shared object here.
semaphore->BeginRead();
semaphore->EndWrite();
// Perform read-only operations on the object, allowing other threads to also read.
semaphore->EndRead();
semaphore->BeginWrite(); // Program locks up here.
}
调试信息,用于@v。ouddou置评。
当我遍历代码时,只有一个线程。顺便说一下,这是一个窗口应用程序,所以程序入口点是WinMain(如果有关系的话)。
当我进入死亡行(最后一个semaphore->BeginWrite()
)时,程序冻结,如果我停止它,则有两个线程。我的主线程是在组装土地,但调用堆栈是WaitForSingleObject
-> WaitForSingleObjectEx
-> ZwWaitForSingleObject
。
还有一个没有堆栈信息的第二个线程,在RtlUserThreadStart
的入口点。我认为这个线程只是为了我能够暂停应用程序的缘故。此时,在我的代码中创建第二个线程没有任何意义。
这似乎已经解决了问题。但是,它需要添加一个临界区来防止读过程中的死锁。
// Care: Two threads attempting to acquire the locks here would deadlock.
critical_section->Acquire();
semaphore->BeginRead();
semaphore->BeginWrite();
critical_section->Leave();
// Perform write operations on the shared object here.
semaphore->EndWrite();
// Perform read-only operations on the object, allowing other threads to also read.
semaphore->EndRead();
semaphore->BeginWrite(); // No longer deadlocks.
相关文章:
- 如何调试读写器锁的死锁?
- 如何使用单个解锁方法(可称为读取器或写入器)实现C++读写器锁?
- 简单的读写锁
- 快速且无锁的单写器、多读器
- CreateProcess cmd.exe读/写管道死锁
- 什么可能导致单个写入/多读锁的僵局
- 优化读/写锁的实现
- 单读多写器,带pthreads和锁,不带boost
- 无锁单写多读列表的实现细节
- 如何从更基本的同步原语中创建多读/单写锁
- 此用例是否需要读写锁
- 仅使用关键部分的读/写锁定会导致死锁
- 在线程内部函数上使用哪种保护方法(互斥,读写锁.)
- 我们可以在socket Map上使用读写锁吗?
- tmultireadexexclusivewritesynchronizer将写锁降级为读锁时的行为
- 写文件/读文件死锁
- 在另一个函数中增强读/写锁
- 单个进程中有数千个读/写锁
- 如何在c++ 14中实现读/写锁
- 具有条件变量的读写器锁