简体   繁体   English

是否有与ReaderWriterLockSlim相同的lock {}语句?

[英]Is there an equivalent of the lock{} statement for ReaderWriterLockSlim?

I like the shortcut in C# of lock(myLock){ /* do stuff */} . 我喜欢C#of lock(myLock){ /* do stuff */}的快捷方式lock(myLock){ /* do stuff */} Is there an equivalent for read/write locks? 是否有读/写锁的等价物? (Specifically ReaderWriterLockSlim.) Right now, I use the following custom method, which I think works, but is kind of annoying because I have to pass in my action as an anonymous function, and I would prefer to use a standard locking mechanism if possible. (特别是ReaderWriterLockSlim。)现在,我使用以下自定义方法,我认为它有效,但有点烦人,因为我必须将我的操作作为匿名函数传递,我宁愿使用标准锁定机制,如果可能的话。

    void bool DoWithWriteLock(ReaderWriterLockSlim RWLock, int TimeOut, Action Fn)
    {
        bool holdingLock = false;
        try
        {
            if (RWLock.TryEnterWriteLock(TimeOut))
            {
                holdingLock = true;
                Fn();
            }
        }
        finally
        {
            if (holdingLock)
            {
                RWLock.ExitWriteLock();
            }
        }
        return holdingLock;
    }

You can't override the behaviour of the lock keyword. 您无法覆盖lock关键字的行为。 A common technique is to hijack the using keyword. 一种常见的技术是劫持using关键字。

  1. Make DoWithWriteLock return an IDisposable 使DoWithWriteLock返回IDisposable
  2. Keep the TryEnterWriteLock call inside the DoWithWriteLock method TryEnterWriteLock调用保留在DoWithWriteLock方法中
  3. Return an object that implements IDisposable . 返回实现IDisposable的对象。 In that object's Dispose method, put the call to ExitWriteLock . 在该对象的Dispose方法中,将调用放到ExitWriteLock

The end result: 最终结果:

// Before
DoWithWriteLock(rwLock,
    delegate
    {
        Do1();
        Do2()
    } );

// After
using (DoWithWriteLock(rwLock))
{
    Do1();
    Do2();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个ReaderWriterLockSlim等同于读者? - Is there a ReaderWriterLockSlim equivalent that favors readers? 应该在 ReaderWriterLockSlim 锁上调用 Dispose() 吗? - Should Dispose() be called on ReaderWriterLockSlim lock? ReaderWriterLockSlim vs Double Lock Check模式 - ReaderWriterLockSlim vs Double Lock Check pattern ReaderWriterLockSlim与LockRecursionPolicy.SupportsRecursion vs lock - ReaderWriterLockSlim with LockRecursionPolicy.SupportsRecursion vs lock ReaderWriterLockSlim 什么时候比简单的锁更好? - When is ReaderWriterLockSlim better than a simple lock? 为什么 lock 比 ReaderWriterLockSlim 快 240%? - Why lock is 240% faster than ReaderWriterLockSlim? 有什么更好的方法?为什么将List用作线程安全:BlockingCollection或ReaderWriterLockSlim或lock? - What is better and why to use List as thread safe: BlockingCollection or ReaderWriterLockSlim or lock? 在C#中,如何知道线程是否正在等待输入ReaderWriterLockSlim的写锁? - In C#, how to know if a thread is waiting to enter write lock for ReaderWriterLockSlim? 是否有任何情况下,在ReaderWriterLockSlim上调用EnterWriteLock应该输入读锁定? - Is there any circumstance in which calling EnterWriteLock on a ReaderWriterLockSlim should enter a Read lock instead? ReaderWriterLockSlim - System.Threading.LockRecursionException:“此模式不允许递归写入锁获取。” 什么是逻辑错误? - ReaderWriterLockSlim - System.Threading.LockRecursionException: 'Recursive write lock acquisitions not allowed in this mode.' What is the Logic Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM