Code Monkey home page Code Monkey logo

readerswriterlockasync's Introduction

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's People

Contributors

jvanlangen avatar

Stargazers

TheCollegedude avatar

Watchers

 avatar

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.