Giter Site home page Giter Site logo

Comments (2)

coderdnewbie avatar coderdnewbie commented on May 28, 2024

Here was the main test case:

using System.Diagnostics;

namespace TestLockProvider;

public class Program
{
    // This is the pattern for parallel async processing
       
    static async Task Main()
    {
        await TestOption1();

        //await TestOption2();

        Console.ReadKey();
    }

    private static async Task TestOption1()
    {
        LockProvider<int> lockObj = new();

        Stopwatch watch = new();
        watch.Start();

        var list = new List<(string name, int waitTime)>
        {
            ("Item1", 200),
            ("Item2", 500),
            ("Item3", 1000),
            ("Item4", 1500),
            ("Item5", 5000),
            ("Item6", 20),
            ("Item7", 50),
            ("Item8", 100),
            ("Item9", 150),
            ("Item10", 500),
            ("Item11", 200),
            ("Item12", 500),
            ("Item13", 1000),
            ("Item14", 1500),
            ("Item15", 5000),
            ("Item16", 20),
            ("Item17", 50),
            ("Item18", 100),
            ("Item19", 150),
            ("Item20", 500),
            ("Item21", 200),
            ("Item22", 500),
            ("Item23", 1000),
            ("Item24", 1500),
            ("Item25", 5000),
            ("Item26", 20),
            ("Item27", 50),
            ("Item28", 100),
            ("Item29", 150),
            ("Item30", 500),
            ("Item31", 200),
            ("Item32", 500),
            ("Item33", 1000),
            ("Item34", 1500),
            ("Item35", 5000),
            ("Item36", 20),
            ("Item37", 50),
            ("Item38", 100),
            ("Item39", 150),
            ("Item40", 500),
            ("Item41", 200),
            ("Item42", 500),
            ("Item43", 1000),
            ("Item44", 1500),
            ("Item45", 5000),
            ("Item46", 20),
            ("Item47", 50),
            ("Item48", 100),
            ("Item49", 150),
            ("Item50", 500),
            ("Item51", 200),
            ("Item52", 500),
            ("Item53", 1000),
            ("Item54", 1500),
            ("Item55", 5000),
            ("Item56", 20),
            ("Item57", 50),
            ("Item58", 100),
            ("Item59", 150),
            ("Item60", 500),
            ("Item61", 200),
            ("Item62", 500),
            ("Item63", 1000),
            ("Item64", 1500),
            ("Item65", 5000),
            ("Item66", 20),
            ("Item67", 50),
            ("Item68", 100),
            ("Item69", 150),
            ("Item70", 500),
        };

        await Parallel.ForEachAsync(list, async (item, cancellationToken) =>
        {
            try
            {
                await lockObj.WaitAsync(1, cancellationToken);
                Console.WriteLine($"Called for {item.name}, will wait {item.waitTime} ms");
                await Task.Delay(item.waitTime, cancellationToken);
                Console.WriteLine($"Done handling {item.name}");
            }
            finally
            {
                lockObj.Release(1);
            }
        });
        watch.Stop();
        Console.WriteLine($"Time: {watch.ElapsedMilliseconds} ms");
    }

    private static async Task TestOption2()
    {
        LockProvider<int> lockObj = new();

        Parallel.Invoke(() =>
        {
            for (int i = 0; i < 10; i++)
            {
                int a = i;
                _ = Compute(lockObj, a);
            }
        });
        Console.WriteLine("Done");
        await Task.Delay(0);
    }

    static int x = 0;
    private static async Task Compute(LockProvider<int> lockObj, int a)
    {
        Console.WriteLine($"Starting {a}");

        while (true)
        {
            await lockObj.WaitAsync(1);
            x++;
            await Task.Delay(100);
            if (x != 1)
            {
                Console.WriteLine($"ERROR - {a} ({x})");
            }
            else
            {
                Console.WriteLine($"OK - {a} ({x})");
            }
            x--;
            lockObj.Release(1);
        }
    }
}

from lockprovider.

coderdnewbie avatar coderdnewbie commented on May 28, 2024

Forget this solution, as the senior dev found more issues. The test case TestOption1 shows issues.

from lockprovider.

Related Issues (7)

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.