VanLangen.Locking.ReadersWriterLockAsync 1.0.11

ReadersWriterLockAsync solves the thread affinity problem for using multiple readers and a single writer lock using async code. It also resumes awaits on the SynchronizationContext where it was started. For example on UI-threads.

Install-Package VanLangen.Locking.ReadersWriterLockAsync -Version 1.0.11
dotnet add package VanLangen.Locking.ReadersWriterLockAsync --version 1.0.11
<PackageReference Include="VanLangen.Locking.ReadersWriterLockAsync" Version="1.0.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VanLangen.Locking.ReadersWriterLockAsync --version 1.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: VanLangen.Locking.ReadersWriterLockAsync, 1.0.11"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install VanLangen.Locking.ReadersWriterLockAsync as a Cake Addin
#addin nuget:?package=VanLangen.Locking.ReadersWriterLockAsync&version=1.0.11

// Install VanLangen.Locking.ReadersWriterLockAsync as a Cake Tool
#tool nuget:?package=VanLangen.Locking.ReadersWriterLockAsync&version=1.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ReadersWriterLockAsync

ReadersWriterLockAsync solves the thread affinity problem for using multiple readers and a single writer lock using async code. It supports SynchronizationContexts. (usefull when developing UI applications)

I'm still working on some tests (for thoughts, send them)

Examples:

The Write function adds some stopwatch time to the output

Async example:

Write("Before calling UseReaderAsync");
var result = _readersWriterLock.UseReaderAsync(async () =>
{
    Write("Reader start");
    await Task.Delay(1000);
    Write("Reader end");
});
Write("After calling UseReaderAsync");

if (!result.IsCompleted)
{
    Write("result.IsCompleted == false, awaiting");
    await result;
    Write("awaiting ready");
}
else
    Write("result.IsCompleted == true");

Output:

      21,078 ms | Before calling UseReaderAsync
      51,063 ms | Reader start
      70,050 ms | After calling UseReaderAsync
      70,193 ms | result.IsCompleted == false, awaiting
   1.083,426 ms | Reader end
   1.083,705 ms | awaiting ready

As you can see the "Reader start" is executed directly. Because of the await, the result.IsComplete is not set. We're able to await the result.

Non async example:

Write("Before calling UseReaderAsync");
var result = _readersWriterLock.UseReaderAsync(() =>
{
    Write("Reader start");
    // await Task.Delay(1000);
    Write("Reader end");
});
Write("After calling UseReaderAsync");

if (!result.IsCompleted)
{
    Write("result.IsCompleted == false, awaiting");
    await result;
    Write("awaiting ready");
}
else
    Write("result.IsCompleted == true");

Output:

      19,983 ms | Before calling UseReaderAsync
      46,000 ms | Reader start
      46,100 ms | Reader end
      46,625 ms | After calling UseReaderAsync
      46,666 ms | result.IsCompleted == true

Await isn't used in the UseReaderAsync argument action, so the method completes directly. The execution is some faster when await isn't called.

Some readers and writers

Let's add two readers and then two writers followed by a reader. The expected behavior should be:

  1. reader A and B should run parallel
  2. writer C
  3. writer D
  4. reader E
// Initialize an array with some readers and writers.
var allValueTasks = new[]
{
    // the first reader will run directly
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader A start");
        await Task.Delay(1000);
        Write("Reader A end");
    }),
    // the second reader will also run directly
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader B start");
        await Task.Delay(1000);
        Write("Reader B end");
    }),
    // because of two readers, this writer has to be queued
    _readersWriterLock.UseWriterAsync(async () =>
    {
        Write("Writer C start");
        await Task.Delay(1000);
        Write("Writer C end");
    }),
    // because of two readers and a writer queued, this writer has to be queued also
    _readersWriterLock.UseWriterAsync(async () =>
    {
        Write("Writer D start");
        await Task.Delay(1000);
        Write("Writer D end");
    }),
    // Lets add another reader, because some writers are queued, this reader is queued also
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader E start");
        await Task.Delay(1000);
        Write("Reader E end");
    }),
};

foreach (var valueTask in allValueTasks)
    if (!valueTask.IsCompleted)
        await valueTask;
}

Output:

      23,056 ms | Reader A start
      53,059 ms | Reader B start
   1.073,641 ms | Reader B end
   1.073,820 ms | Reader A end
   1.078,123 ms | Writer C start
   2.086,785 ms | Writer C end
   2.087,468 ms | Writer D start
   3.096,612 ms | Writer D end
   3.097,310 ms | Reader E start
   4.102,154 ms | Reader E end

ReadersWriterLockAsync

ReadersWriterLockAsync solves the thread affinity problem for using multiple readers and a single writer lock using async code. It supports SynchronizationContexts. (usefull when developing UI applications)

I'm still working on some tests (for thoughts, send them)

Examples:

The Write function adds some stopwatch time to the output

Async example:

Write("Before calling UseReaderAsync");
var result = _readersWriterLock.UseReaderAsync(async () =>
{
    Write("Reader start");
    await Task.Delay(1000);
    Write("Reader end");
});
Write("After calling UseReaderAsync");

if (!result.IsCompleted)
{
    Write("result.IsCompleted == false, awaiting");
    await result;
    Write("awaiting ready");
}
else
    Write("result.IsCompleted == true");

Output:

      21,078 ms | Before calling UseReaderAsync
      51,063 ms | Reader start
      70,050 ms | After calling UseReaderAsync
      70,193 ms | result.IsCompleted == false, awaiting
   1.083,426 ms | Reader end
   1.083,705 ms | awaiting ready

As you can see the "Reader start" is executed directly. Because of the await, the result.IsComplete is not set. We're able to await the result.

Non async example:

Write("Before calling UseReaderAsync");
var result = _readersWriterLock.UseReaderAsync(() =>
{
    Write("Reader start");
    // await Task.Delay(1000);
    Write("Reader end");
});
Write("After calling UseReaderAsync");

if (!result.IsCompleted)
{
    Write("result.IsCompleted == false, awaiting");
    await result;
    Write("awaiting ready");
}
else
    Write("result.IsCompleted == true");

Output:

      19,983 ms | Before calling UseReaderAsync
      46,000 ms | Reader start
      46,100 ms | Reader end
      46,625 ms | After calling UseReaderAsync
      46,666 ms | result.IsCompleted == true

Await isn't used in the UseReaderAsync argument action, so the method completes directly. The execution is some faster when await isn't called.

Some readers and writers

Let's add two readers and then two writers followed by a reader. The expected behavior should be:

  1. reader A and B should run parallel
  2. writer C
  3. writer D
  4. reader E
// Initialize an array with some readers and writers.
var allValueTasks = new[]
{
    // the first reader will run directly
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader A start");
        await Task.Delay(1000);
        Write("Reader A end");
    }),
    // the second reader will also run directly
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader B start");
        await Task.Delay(1000);
        Write("Reader B end");
    }),
    // because of two readers, this writer has to be queued
    _readersWriterLock.UseWriterAsync(async () =>
    {
        Write("Writer C start");
        await Task.Delay(1000);
        Write("Writer C end");
    }),
    // because of two readers and a writer queued, this writer has to be queued also
    _readersWriterLock.UseWriterAsync(async () =>
    {
        Write("Writer D start");
        await Task.Delay(1000);
        Write("Writer D end");
    }),
    // Lets add another reader, because some writers are queued, this reader is queued also
    _readersWriterLock.UseReaderAsync(async () =>
    {
        Write("Reader E start");
        await Task.Delay(1000);
        Write("Reader E end");
    }),
};

foreach (var valueTask in allValueTasks)
    if (!valueTask.IsCompleted)
        await valueTask;
}

Output:

      23,056 ms | Reader A start
      53,059 ms | Reader B start
   1.073,641 ms | Reader B end
   1.073,820 ms | Reader A end
   1.078,123 ms | Writer C start
   2.086,785 ms | Writer C end
   2.087,468 ms | Writer D start
   3.096,612 ms | Writer D end
   3.097,310 ms | Reader E start
   4.102,154 ms | Reader E end

  • net5.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 History

Version Downloads Last updated
1.0.11 429 12/11/2020
1.0.10 89 12/10/2020
1.0.9 123 12/8/2020
1.0.8 85 12/7/2020
1.0.7 104 12/7/2020
1.0.6 132 12/6/2020
1.0.5 142 12/6/2020
1.0.4 148 12/6/2020
1.0.3 184 12/6/2020
1.0.2 102 12/6/2020
1.0.1 104 12/6/2020
1.0.0 114 12/6/2020