Skip to main content
 首页 » 编程设计

c#之ReaderWriterLock.UpgradeToWriterLock 超时后不会抛出异常

2024年02月13日11txw1958

我在编写单元测试时偶然发现了 ReaderWriterLock 的一个非常奇怪的问题。 我尝试将超时选项设置为 50 毫秒来测试 UpgradeToWriterLock 方法。

在主线程上,我获取读者锁,然后开始许多任务。在任务中,我也采用读者锁定,然后尝试在超时的情况下升级到编写者。由于主线程持有读锁,因此这应该在每个线程上都失败。由于超时为 50 毫秒,任务应抛出超时异常并完​​成。如果我开始 10 多个任务,他们就不会。他们卡在 UpgradeToWriterLock 上。

谁能解释一下?完整源代码如下。

    [TestMethod] 
    public void UpgradeLockFailTest() 
    { 
        // strangely when more than 10 threads then it gets stuck on UpgradeToWriterLock regardless of the timeout 
        const int THREADS_COUNT = 20; 
        // 50 milliseconds 
        const int TIMEOUT = 50; 
 
        // create the main reader writer lock 
        ReaderWriterLock rwl = new ReaderWriterLock(); 
 
        // acquire the reader lock on the main thread 
        rwl.AcquireReaderLock(TIMEOUT); 
 
        // create and start all the tasks 
        Task[] tasks = new Task[THREADS_COUNT]; 
        for (int i = 0; i < THREADS_COUNT; i++) 
        { 
            tasks[i] = Task.Factory.StartNew(() => 
            { 
                try 
                { 
                    // acquire the reader lock on the worker thread 
                    rwl.AcquireReaderLock(TIMEOUT); 
 
                    // acquire the writer lock on the worker thread  
                    rwl.UpgradeToWriterLock(TIMEOUT); // <-- GETS STUCK HERE AND DOESN'T RESPECT TIMEOUT 
 
                } 
                finally 
                { 
                    rwl.ReleaseLock(); 
                } 
 
            }); 
        } 
 
        // should be enough for all the tasks to be created 
        Thread.Sleep(2000); 
 
        try 
        { 
            // wait for all tasks 
            Task.WaitAll(tasks); // <-- GETS STUCK HERE BECAUSE THE TASKS ARE STUCK ON UpgradeToWriterLock 
        } 
        catch (AggregateException ae) 
        { 
            Assert.AreEqual(THREADS_COUNT, ae.InnerExceptions.Count); 
        } 
 
        // release all the locks on the main thread 
        rwl.ReleaseLock(); 
    } 

有趣的是,如果我在等待任务之前释放主线程读取器锁,一切都会按预期进行。抛出正确数量的超时异常。

请您参考如下方法:

你确定他们都卡住了,而不仅仅是最后一个?

来自UpgradeToWriterLock documentation :

The time-out exception is not thrown until the thread that called the UpgradeToWriterLock method can reacquire the reader lock. If there are no other threads waiting for the writer lock, this happens immediately. However, if another thread is queued for the writer lock, the thread that called the UpgradeToWriterLock method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. This is true even if the other thread that requested the writer lock requested it after the current thread called the UpgradeToWriterLock method.

请注意引发超时异常的多个条件。 “如果另一个线程排队等待写入器锁,则调用 UpgradeToWriterLock 方法的线程无法重新获取读取器锁 [并抛出异常],直到”:

  1. 所有当前读者都已释放他们的锁
  2. 一个线程已经获取并释放了写锁

您决不允许最后一个尝试升级的线程出现这些情况,因此您永远在 UpgradeToWriterLock 等待,因此 WaitAll 也会永远等待。如果你的主线程在等待之前也尝试升级,我想你会没事的。