AsyncLock | Async/Awaitable non-blocking lock in C | Reactive Programming library
kandi X-RAY | AsyncLock Summary
kandi X-RAY | AsyncLock Summary
Async/Awaitable non-blocking lock in C#
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of AsyncLock
AsyncLock Key Features
AsyncLock Examples and Code Snippets
Community Discussions
Trending Discussions on AsyncLock
QUESTION
I am examining the effects of replacing some instances of regular C# event pattern with IAsyncEnumerable. This would be accomplished by lazy instantiation/activation of an IAsyncEnumerable and caching that reference for use by all callers/listeners. Some quick tests (see below) show that this works, but I haven't seen other examples online using IAsyncEnumerable in this fashion.
I realize this isn't exactly what IAsyncEnumerable was created for, and that most would advocate for ReactiveX (https://github.com/dotnet/reactive) in this case. However, I'd appreciate an analysis of why one would or would not want to do this as described (instead of just how to do this with Rx). I've provided a couple of examples below. My candidate event pattern replacement is one where it is more of an event stream (like deserialized messages being produced off a serial connection or UDP socket etc.)
Example 1:
...ANSWER
Answered 2020-Oct-28 at 15:41Looks like channels is what you're looking for.
QUESTION
EDIT: I've updated my examples to use the https://github.com/StephenCleary/AsyncEx library. Still waiting for usable hints.
There are resources, which are identified by strings (for example files, URLs, etc.). I'm looking for a locking mechanism over the resources. I've found 2 different solutions, but each has its problems:
The first is using the ConcurrentDictionary
class with AsyncLock
:
ANSWER
Answered 2020-Feb-04 at 21:50You could probably improve upon the first solution by removing a lock from the dictionary when it stops being in use. The removed locks could then be added to a small pool, so that the next time you need a lock you just grab one from the pool instead of creating a new one.
Update: Here is an implementation of this idea. It is based on SemaphoreSlim
s instead of Stephen Cleary's AsyncLock
s, because a custom disposable is required in order to remove unused semaphores from the dictionary.
QUESTION
I have a custom "CachedEnumerable" class (inspired by Caching IEnumerable) that I need to make thread safe for my asp.net core web app.
Is the following implementation of the Enumerator thread safe? (All other reads/writes to IList _cache are locked appropriately) (Possibly related to Does the C# Yield free a lock?)
And more specifically, if there are 2 threads accessing the enumerator, how do I protect against one thread incrementing "index" causing a second enumerating thread from getting the wrong element from the _cache (ie. element at index + 1 instead of at index)? Is this race condition a real concern?
...ANSWER
Answered 2019-Oct-24 at 13:21The access to cache, yes it is thread safe, only one thread per time can read from _cache object.
But in that way you can't assure that all threads gets elements in the same order as they access to GetEnumerator.
Check these two exaples, if the behavior is what you expect, you can use lock in that way.
Example 1:
THREAD1 Calls GetEnumerator
THREAD1 Initialize T current;
THREAD2 Calls GetEnumerator
THREAD2 Initialize T current;
THREAD2 LOCK THREAD
THREAD1 WAIT
THREAD2 read from cache safely _cache[0]
THREAD2 index++
THREAD2 UNLOCK
THREAD1 LOCK
THREAD1 read from cache safely _cache[1]
THREAD1 i++
THREAD1 UNLOCK
THREAD2 yield return current;
THREAD1 yield return current;
Example 2:
THREAD2 Initialize T current;
THREAD2 LOCK THREAD
THREAD2 read from cache safely
THREAD2 UNLOCK
THREAD1 Initialize T current;
THREAD1 LOCK THREAD
THREAD1 read from cache safely
THREAD1 UNLOCK
THREAD1 yield return current;
THREAD2 yield return current;
QUESTION
Partially, this question has a bit of similarity with this one, but as another one is not properly asked (and not fully asked) I am trying to ask it in general, so this question can not be considered as a duplication.
The question is about understanding of how AsyncLock actually works. (In this context I am referring to Neosmart.AsyncLock library, however, I consider it uses common approach in AsyncLock implementation).
So. For instance, we have a main thread (let it be a UI-thread):
...ANSWER
Answered 2019-Sep-06 at 18:10As Matthew points out in the comments, AsyncLock
is reentrant, meaning that if the same thread attempts to take the lock a second time, it recognizes that and allows it to continue. The author of AsyncLock
wrote a lengthy article about how reentrance was really the reason he wrote it: AsyncLock: an async/await-friendly locking library for C# and .NET
It's not a bug; it's a feature.™
After the "Update 5/25/2017" heading, there are code examples demonstrating exactly what you are experiencing here and showing how it is a feature.
Reasons to want reentrance are:
- If you are just concerned with multiple threads touching the same variable (preventing race conditions), then there is simply no reason to block a thread that already has the lock.
- It makes recursive functions that use locks easier to write because you don't need to test if you already have the lock. A lack of reentrance support + sloppy recursive coding = deadlock.
If you really want it to not be reentrant, you can use what he says was not appropriate for reentrance: SemaphoreSlim
:
QUESTION
I'm trying to understand how the AsyncLock works.
First of all, here's a snippet to prove that it actually works:
...ANSWER
Answered 2019-Jul-19 at 01:07The lock inside AsyncLock
is beeing released very quickly. Each task which tries to acquire AsyncLock
, successfully acquires it's internal lock
and the actual locking logic is done with a queue.
QUESTION
With send
and yield
we can have two-way communication with a generator and implement a state machine quite nicely (see example below). Now, we can't (?) send to asyncio coroutines, so how could one implement a state machine with asyncio-coroutines?
Generator example
...ANSWER
Answered 2018-Aug-15 at 11:26Python3.6 added support for async generators (PEP525) so async
functions can now be generators too!
QUESTION
We're currently experiencing an issue with WhenActivated
with version 8 unward. We aren't using the routing infrastructure and are rather using an in house MVVM framework. In order to support ISupportActivation
we listening to the OnAppearing
and OnDisapearing
events in a base page and manually calling Activate()
and Deactivate()
.
The following used to work in the Alpha version of version 8 but isn't any longer. No issues in iOS.
The code below illustrate that issue, a single ReactiveCommand is bound on the MainPage and we navigate to a Target hello world page. A repo can be found here
BaseContentPage ...ANSWER
Answered 2018-Jul-25 at 00:29I can answer one of the two questions.
how to fix this?
I've seen a lot of ReactiveUI projects but never see anyone dispose of ReactiveCommands. The important thing to remember is to dispose of ReactiveCommand subscriptions and bindings.
So if you choose to bind the ReactiveUI way, rather than via xaml, you could do the following in xaml:
QUESTION
I have a strategy pattern in my business logic, which is referenced to two application. One is a Web MVC application and the other one is a Xamarin App.
Now I want to write / read data from the database in this strategy pattern, but I have two different repository. How can I inject the right one into this strategy pattern.
...ANSWER
Answered 2018-Jul-17 at 12:51Based on the comments it looks like you must use a given repository when the strategy is used either from the web or from an app.
The key here is to avoid trying to guess in which app one is but rather to delegate this responsibility further. This is where dependency injection shines: let the caller give you the correct repository !
First these two repositories have to be grouped under the same abstraction:
QUESTION
I'm accessing an EventStore from a dotnet core web application. There is a single connection shared by all threads. The connection is opened on first access and I need to ensure only one thread opens the connection. Previously I would have used a lock
but then I can't await
the method to open the connection.
I found the following snippet of code that looks like it should do the trick:
...ANSWER
Answered 2018-Feb-08 at 21:39Turns out there is a useful nuget library and associated Github repo from Stephen Cleary that is a drop in replacement for the AsyncLock
class above.
QUESTION
I'm new to Rx.Net & RxUI. During my learning of these two libraries, I tried to build a demo application which extracts images from websites. I used WPF combined with Rx.Net and RxUI to build the Views and ViewModels, and HtmlAgilityPack to handle html documents. My code looks like below
ViewModel:
...ANSWER
Answered 2018-Feb-04 at 03:59Change
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AsyncLock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page