Resource locking with async/await

前端 未结 2 386
失恋的感觉
失恋的感觉 2020-12-24 13:19

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 sy

相关标签:
2条回答
  • 2020-12-24 13:37

    There's SemaphoreSlim.WaitAsync which fits closely here. (I found it in a similar question).

    0 讨论(0)
  • 2020-12-24 13:50

    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?

    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.

    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.

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

    0 讨论(0)
提交回复
热议问题