使用异步/等待资源锁定 [英] Resource locking with async/await

查看:68
本文介绍了使用异步/等待资源锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中有一个共享资源(一个Motion系统),可以由多个客户端访问.我有个别操作,需要在整个移动过程中访问系统,并且如果同时请求有冲突的操作,则应该抛出忙"异常.我还拥有音序器,需要获得对Motion系统的独占访问权,才能执行多个操作,以及其他操作.在整个序列中,没有其他客户端应该能够运行Operations.

I have an application where I have a shared resource (a Motion system) which can be accessed by multiple clients. I have individual Operations that require access to the system for the duration of the move and which should throw 'Busy' exceptions if conflicting operations are requested at the same time. I also have Sequencers which need to acquire exclusive access to the Motion system for the execution of several Operations, interspersed with other actions; during the entire sequence, no other clients should be able to run Operations.

传统上,我使用Thread-affinity来实现这一点,因此Thread可以请求独占访问并运行与操作相对应的阻塞调用.虽然线程可以访问,但是其他线程都不能使用该资源.我现在遇到的问题是,我已经开始使用异步/等待模式来实现我的系统,以允许更清晰的音序器实现.问题在于,现在我的音序器并不总是在同一线程上运行;而是在同一线程上运行.活动线程可以在回调过程中更改,因此不再容易确定我是否处于有效上下文中以继续运行操作.需要注意的一点是,某些操作本身是由等待组成的,这意味着序列和单个操作都可以跨越多个线程.

I've traditionally approached this using Thread-affinity, so that a Thread can request exclusive access and run blocking calls corresponding to operations. While the Thread has access, no other Threads may use the resource. The problem I'm having now is that I've moved toward implementing my system using async/await patterns, to allow cleaner sequencer implementation. The problem is that now my sequencer is not always running on the same thread; the active thread can change during the course of callbacks, so it is no longer easy to determine whether I am in a valid context to keep running operations. One item of note is that some of the Operations themselves are composed of awaits, which means both sequences and individual Operations can span multiple threads.

我的问题:在异步/等待导致线程切换的情况下,有人知道有一种很好的模式来处理获取独占访问的情况吗?

My question: does anybody know of a good pattern to deal with acquiring exclusive access in the presence of thread switching due to async/await?

作为参考,我考虑了一些事情:

For reference, a few things I've considered:

  1. 我可以创建一个自定义SynchronizationContext,在序列持续时间内将所有定序器调用封送回单个线程.这样做的好处是允许我重用现有的线程相似性访问管理代码.缺点是,每当我执行序列或操作时,这都将需要专用线程(因为操作也可以跨越多个线程.)

  1. I could create a custom SynchronizationContext that marshals all sequencer calls for the duration of a sequence back to a single Thread. This has the benefit of allowing me to reuse my existing thread-affinity access management code. The downside is that this will require dedicating a Thread whenever I do either a Sequence or an Operation (since Operations can also span multiple threads.)

创建一个可获取的访问令牌,以传递给Operation方法以证明您已获得访问权限.这具有使用令牌参数使方法膨胀的缺点.

Create an acquirable access token to pass to the Operation methods to prove that you have acquired access. This has the downside of bloating the methods with a token parameter.

使用(2)中的访问令牌方法,但是为Operations接口创建一个重复的接口实现,以便可以使用令牌'baked-in'实例化包装器.这样会创建一些难看的粘合代码,但是会清理定序器代码,因此不再需要将令牌传递给每个方法.

Use the access token approach from (2), but create a duplicate interface implementation for the Operations interface so a wrapper can be instantiated with the token 'baked-in'. This creates some ugly glue code, but it cleans up the sequencer code so that it no longer needs to pass a token to each method.

推荐答案

我的问题:在异步/等待导致线程切换的情况下,有人知道有一种很好的模式来处理获取独占访问的情况吗?

My question: does anybody know of a good pattern to deal with acquiring exclusive access in the presence of thread switching due to async/await?

是的,您可以使用 AsyncLock ,它也可以作为我的 AsyncEx库的一部分使用.如果要进行"TryLock"操作,则可能必须创建自己的基元.

Yes, you can use AsyncLock, which is also available as part of my AsyncEx library. If you want to have a "TryLock" kind of operation, then you may have to create your own primitive.

您确实失去了一些进行安全检查的能力:无法检查当前正在执行的线程是否具有特定的AsyncLock.

You do lose some of the capability to do safety checks: there is no way to check whether the currently-executing thread has a specific AsyncLock.

其他选项包括ConcurrentExclusiveSchedulerPair(我通过博客撰写了 )或TPL数据流.

Other options include ConcurrentExclusiveSchedulerPair (which I blog about here) or TPL Dataflow.

这篇关于使用异步/等待资源锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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