Comments (3)
You do bring up some good points. This would, however, be a significant breaking change.
from asyncex.
This has the same behavior as Monitor.TryEnter
. If you try to enter a Monitor with 0 timeout, it will try to take the lock before returning false. So it's a matter of which convention you want to follow. But I guess it would be a little more obvious if it had a Try
in the method name.
from asyncex.
My previous argument was based on the effect when not doing an atomic wait. I just needed to actually do an atomic wait for real and found some additional concerns.
When doing an atomic wait, the Lock
method could throw an OperationCancelledException
, but that exception isn't an error. It is the expected result when failing to acquire the lock. There are three problems with this.
- The
using
statement makes it difficult to put atry
/catch
around the call toLock
without including other code that could raise anOperationCancelledException
because of an actual cancellation. - The
try
/catch
is a lot of boilerplate - An exception is being used for a non-error case raising concerns about the performance implications of using an exception there.
The following code demonstrates what I mean:
public async Task Example(CancellationToken ct = default)
{
try
{
using(_asyncLock.Lock(new CancellationToken(true)).ConfigureAwait(false))
{
await SaveAsync(results, ct);
}
}
catch (OperationCancelledException)
{
// This will catch both the expected exception from the call to Lock(), and the actual cancellation
// from the call to SaveAsync(). To avoid this, one would need to try/catch just the Lock() while
// saving the disposable into a variable to put into a using after the try/catch.
}
}
I created an extension method to AsyncLock
that I think shows what could be a good usage pattern. It follows a convention we established for our codebase of naming methods that do not wait *Now
. Using that name isn't necessary, it just demonstrates how such a method could work. (Note that the method doesn't need to be async because it never actually waits to acquire the lock. This allows for the use of an out parameter.)
public async Task Example(CancellationToken ct = default)
{
using(_asyncLock.LockNow(out var held))
{
if (!held)
return;
await SaveAsync(results, ct);
}
}
from asyncex.
Related Issues (20)
- Contradiction? HOT 1
- Support AsyncProducerConsumerQueue wait for empty
- AsyncCollection IsEmpty HOT 3
- [Discussion] Return `ValueTask` instead of `AwaitableDisposable`? HOT 5
- [Question] .WaitAndUnwrapException() vs AsyncContext and potential deadlock HOT 2
- The collection has been marked as complete with regards to additions HOT 1
- What happened to NotifyTask? HOT 1
- CancellationTokenTaskSource's dispose
- Using LazyAsync with JSONSeralizer? HOT 1
- Producer/Consumer PriorityQueue
- doc/TaskConstants.md links to an article that doesn't exist anymore
- Cross thread Sync context stealing HOT 2
- [Question] Using discard with AsyncLock HOT 1
- [Question] Is ConfigureAwait(false) is needed? HOT 1
- AsyncContext holds exceptions until all tasks complete.
- Feature request: AsyncReaderWriterLock support for UpgradeableReadLock HOT 11
- The ApmAsyncFactory interop hangs if a path in async method returns synchronously. HOT 1
- Setting AsyncAutoResetEvent after used in Task.WhenAny HOT 3
- AsyncLazy with API call is being called twice across 4 components. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncex.