【发布时间】:2015-01-29 09:28:52
【问题描述】:
我对 ReaderWriterLockSlim 和延迟 ExitWriteLock 感到沮丧。 为什么定时器回调中会释放WriteLock?
var _lock = new ReaderWriterLockSlim();
_lock.EnterWriteLock();
Assert.AreEqual(true, _lock.IsWriteLockHeld); // good
System.Threading.Timer timer = new Timer(state =>
{
_lock.ExitWriteLock(); //throws exception that lock is not held
}, null, 1, -1);
Thread.Sleep(1000);
Assert.AreEqual(false, _lock.IsWriteLockHeld);
【问题讨论】:
-
你能解释一下你期望这段代码做什么吗?这是什么类型的
Timer?另请参阅 MSDN:IsWriteLockHeldreturns "a value that indicates whether the current thread has entered the lock in write mode"...
标签: c# .net multithreading readerwriterlockslim