是安全的使用ReaderWriterLockSlim在异步方法 [英] Is it Safe to use ReaderWriterLockSlim in an async method

查看:158
本文介绍了是安全的使用ReaderWriterLockSlim在异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 ReaderWriterLockSlim 类使用线程ID,看看谁拥有该锁是安全的,异步方法使用那里没有,所有的方法将被执行的机制保障相同的线程。

例如。

  System.Threading.ReaderWriterLockSlim readerwriterlock =新System.Threading.ReaderWriterLockSlim();
    私人异步任务测试()
    {
        readerwriterlock.EnterWriteLock();
        等待Task.Yield(); //这样做可能会产生任务工作
        readerwriterlock.ExitWriteLock(); // potentailly退出锁定在不同的线程
    }
 

解决方案
  

是安全的使用ReaderWriterLockSlim在异步方法

是的,没有。它可以安全地使用这一个异步方法,但很可能不是安全使用它在你输入一个异步方法和退出锁跨越一个等待

在这种情况下,没有,这并不一定是安全的。

<一个href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.exitwritelock.aspx">ExitWriteLock必须调用在同一个线程调用 EnterWriteLock 。否则,它抛出一个 SynchronizationLockException 。从<一个href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.exitwritelock.aspx">documentation,此异常被抛出时:

  

当前线程尚未在写入模式下输入的锁定

唯一的一次,这将是安全的,如果这是在一直的环境中有一个异步方法使用的地方,这将推动事情的电流的SynchronizationContext 回到同一个线程(如:Windows窗体,WPF,等等),而不是使用嵌套的异步调用,其中一个父了调用链设置一个任务与 ConfigureAwait(假)(将prevent的工作从采集的同步上下文)。如果你是在特定的情况下,你就知道该线程将保持,因为等待通话将元帅您返回到调用上下文。

Since the ReaderWriterLockSlim class uses Thread ID to see who owns the lock is it safe to use with async methods where there is no guarentee that all the method will be executed on the same thread.

For example.

    System.Threading.ReaderWriterLockSlim readerwriterlock = new System.Threading.ReaderWriterLockSlim();
    private async Task Test()
    {
        readerwriterlock.EnterWriteLock();
        await Task.Yield(); //do work that could yield the task
        readerwriterlock.ExitWriteLock(); //potentailly exit the lock on a different thread
    }

解决方案

Is it Safe to use ReaderWriterLockSlim in an async method

Yes and no. It can be safe to use this in an async method, but it is likely not safe to use it in an async method where you enter and exit the lock spanning an await.

In this case, no, this is not necessarily safe.

ExitWriteLock must be called from the same thread that called EnterWriteLock. Otherwise, it throws a SynchronizationLockException. From the documentation, this exception is thrown when:

The current thread has not entered the lock in write mode.

The only time this would be safe is if this was used in an async method which was always in an environment where there was a current SynchronizationContext in place which will move things back to the same thread (ie: Windows Forms, WPF, etc), and wasn't used by a nested async call where a "parent" up the call chain setup a Task with ConfigureAwait(false) (which would prevent the Task from capturing the synchronization context). If you are in that specific scenario, you'd know the thread would be maintained, as the await call would marshal you back onto the calling context.

这篇关于是安全的使用ReaderWriterLockSlim在异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆