gpt4 book ai didi

c# - C# ReaderWriteLock 中可升级读锁与写锁的区别

转载 作者:太空狗 更新时间:2023-10-29 22:12:31 26 4
gpt4 key购买 nike

ReaderWriterLock 类中的可升级读锁和写锁有什么区别?

根据MSDN的描述,只有一个线程可以进入可升级读锁。如果我遵循正确,我可以忘记升级它,因为它与写锁相同。有什么我想念的吗?

最佳答案

(我假设您指的是 ReaderWriterLockSlim,因为我认为 ReaderWriterLock 没有可升级的读取模式。)

虽然只有一个线程可以进入一个可升级读锁,但是一个线程可以有一个可升级读锁其他线程仍然可以进入一个读锁:

Only one thread can enter upgradeable mode at any given time. If a thread is in upgradeable mode, and there are no threads waiting to enter write mode, any number of other threads can enter read mode, even if there are threads waiting to enter upgradeable mode.

将其与写锁进行比较:

When there are threads waiting to enter write mode, additional threads that try to enter read mode or upgradeable mode block until all the threads waiting to enter write mode have either timed out or entered write mode and then exited from it.

换句话说,写锁是完全排他的,而可升级的读锁将允许其他线程继续进入/退出读取模式直到需要升级

关于c# - C# ReaderWriteLock 中可升级读锁与写锁的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696238/

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