兼容模式
read upgradable write
read true true false
upgradable true false false
write false false false
这个矩阵是对称的。 read -> upgradable -> write 锁定级别依次提高。
所谓的锁定升级,是指在不释放已持有的较低级别的锁的前提下,再去获取更高级别的锁。
关于upgradable这个级别,MSDN上说:
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. 在任意时刻仅有一个线程可以进入可升级读模式。如果有一个线程处于可升级读模式,并且没有任何线程等待进入写模式,那么任意数量的线程可以进入读模式,即使有线程在等待进入可升级读模式。
If one or more threads are waiting to enter write mode, a thread that calls the EnterUpgradeableReadLock method blocks until those threads have either timed out or entered write mode and then exited from it. 如果一个或多个线程在等待进入写模式,那么试图进入可升级读模式的线程会被阻塞,直到等待进入写模式的线程全部退出或超时。
转载于:https://www.cnblogs.com/zhy2002/archive/2008/11/18/1335879.html