Giter VIP home page Giter VIP logo

Comments (3)

StephenCleary avatar StephenCleary commented on June 1, 2024

You do bring up some good points. This would, however, be a significant breaking change.

from asyncex.

timcassell avatar timcassell commented on June 1, 2024

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.

WalkerCodeRanger avatar WalkerCodeRanger commented on June 1, 2024

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.

  1. The using statement makes it difficult to put a try/catch around the call to Lock without including other code that could raise an OperationCancelledException because of an actual cancellation.
  2. The try/catch is a lot of boilerplate
  3. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.