Async.Locks 0.1.0

dotnet add package Async.Locks --version 0.1.0
                    
NuGet\Install-Package Async.Locks -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Async.Locks" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Async.Locks" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Async.Locks" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Async.Locks --version 0.1.0
                    
#r "nuget: Async.Locks, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Async.Locks@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Async.Locks&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Async.Locks&version=0.1.0
                    
Install as a Cake Tool

Async.Locks

NuGet License

Async.Locks is a .NET library designed to provide robust and flexible asynchronous locking primitives. It offers a set of tools for managing concurrency in asynchronous applications, ensuring thread safety and preventing race conditions.

Features

  • Asynchronous Locking: Provides asynchronous versions of common locking mechanisms.
  • Customizable Queuing: Supports customizable lock acquisition queuing strategies through the IAsyncLockQueueStrategy interface.
  • Timeout and Cancellation: Allows for timeouts and cancellation of lock acquisition attempts.
  • Event-Driven: Provides events for lock acquisition, release, timeouts, and cancellations.
  • Extensible Design: Designed for easy extension and customization.
  • Robust and Thread-Safe: Ensures thread safety and handles concurrent access correctly.
  • Clear and Consistent API: Adheres to established .NET naming conventions and design patterns.

Getting Started

Installation

Install the Async.Locks NuGet package:

Install-Package Async.Locks

Usage

AsyncLock
using Async.Locks;

public async Task ExampleAsync()
{
    IAsyncLock asyncLock = new AsyncLock();

    await using (await asyncLock.AcquireAsync())
    {
        // Critical section
        Console.WriteLine("Lock acquired!");
        await Task.Delay(1000); // Simulate work
    }

    Console.WriteLine("Lock released!");
}
AsyncLock with Timeout
using Async.Locks;

public async Task ExampleWithTimeoutAsync()
{
    IAsyncLock asyncLock = new AsyncLock();
    var timeout = TimeSpan.FromMilliseconds(500);

    try
    {
        await using (await asyncLock.AcquireAsync(timeout: timeout))
        {
            // Critical section
            Console.WriteLine("Lock acquired!");
            await Task.Delay(200); // Simulate work
        }
        Console.WriteLine("Lock released!");
    }
    catch (TimeoutException)
    {
        Console.WriteLine("Lock acquisition timed out!");
    }
}
AsyncLock with Cancellation
using Async.Locks;
using System.Threading;

public async Task ExampleWithCancellationAsync()
{
    IAsyncLock asyncLock = new AsyncLock();
    var cancellationTokenSource = new CancellationTokenSource();
    var cancellationToken = cancellationTokenSource.Token;

    cancellationTokenSource.CancelAfter(200);

    try
    {
        await using (await asyncLock.AcquireAsync(cancellationToken: cancellationToken))
        {
            // Critical section
            Console.WriteLine("Lock acquired!");
            await Task.Delay(1000); // Simulate work
        }
        Console.WriteLine("Lock released!");
    }
    catch (TaskCanceledException)
    {
        Console.WriteLine("Lock acquisition cancelled!");
    }
}
AsyncLock with Custom Queue Strategy
using Async.Locks;

public async Task ExampleWithCustomQueueStrategyAsync()
{
    IAsyncLock asyncLock = new AsyncLock(new LifoLockQueueStrategy());

    await using (await asyncLock.AcquireAsync())
    {
        // Critical section
        Console.WriteLine("Lock acquired with LIFO queue!");
        await Task.Delay(1000);
    }
    Console.WriteLine("Lock released!");
}
Events
using Async.Locks;

public async Task ExampleWithEventsAsync()
{
    IAsyncLock asyncLock = new AsyncLock();

    asyncLock.OnLockAcquired += () => Console.WriteLine("Lock acquired event!");
    asyncLock.OnLockReleased += () => Console.WriteLine("Lock released event!");
    asyncLock.OnLockTimeout += () => Console.WriteLine("Lock timeout event!");
    asyncLock.OnLockCancelled += () => Console.WriteLine("Lock cancelled event!");

    await using (await asyncLock.AcquireAsync())
    {
        await Task.Delay(100);
    }
}

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

Documentation

For more detailed documentation and examples, please visit the GitHub Wiki.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 740 3/27/2025