Exemplo n.º 1
0
        private static void AsyncOneManyLockTest()
        {
            AsyncOneManyLock m_lock = new AsyncOneManyLock();

            Task.Run(async() =>
            {
                await m_lock.WaitAsync(OneManyMode.Shared);
                Console.WriteLine("I'm a reader1");
                Thread.Sleep(3000);
                m_lock.Release();
            });

            Task.Run(async() =>
            {
                await m_lock.WaitAsync(OneManyMode.Shared);
                Console.WriteLine("I'm a reader2");
                Thread.Sleep(3000);
                m_lock.Release();
            });

            Thread.Sleep(1000);

            Task.Run(async() =>
            {
                await m_lock.WaitAsync(OneManyMode.Exclusive);
                Console.WriteLine("I'm a writer");
                Thread.Sleep(3000);
                m_lock.Release();
            });

            Thread.Sleep(500);

            Task.Run(async() =>
            {
                await m_lock.WaitAsync(OneManyMode.Shared);
                Console.WriteLine("I'm a reader3");
                Thread.Sleep(3000);
                m_lock.Release();
            });

            Task.Run(async() =>
            {
                await m_lock.WaitAsync(OneManyMode.Shared);
                Console.WriteLine("I'm a reader4");
                Thread.Sleep(3000);
                m_lock.Release();
            });
        }
Exemplo n.º 2
0
    private static void OneManyDemo()
    {
        var         asyncLock = new AsyncOneManyLock();
        List <Task> tasks     = new List <Task>();

        for (int x = 0; x < 5; x++)
        {
            var y = x;

            tasks.Add(Task.Run(async() =>
                {
                    var mode = (y < 3) ? OneManyMode.Shared : OneManyMode.Exclusive;
                    Console.WriteLine("ThreadID={0}, OpID={1}, await for {2} access",
                                      Environment.CurrentManagedThreadId, y, mode);
                    var t = asyncLock.WaitAsync(mode);
                    await t;
                    Console.WriteLine("ThreadID={0}, OpID={1}, got access at {2}",
                                      Environment.CurrentManagedThreadId, y, DateTime.Now.ToLongTimeString());
                    Thread.Sleep(5000);
                    asyncLock.Release();
                }));
            Thread.Sleep(200);
        }
        Task.WaitAll(tasks.ToArray());
        Console.WriteLine("All operations done");
        Console.ReadLine();
    }
Exemplo n.º 3
0
    private static async Task AccessResourceViaAsyncSynchronization(AsyncOneManyLock asyncLock)
    {
        // Execute whatever code you want here...

        // Pass OneManyMode.Exclusive or OneManyMode.Shared depending on the concurrent access you need
        await asyncLock.WaitAsync(OneManyMode.Shared);     // Request shared access to a resource via its lock

        // When we get here, no threads are writing to the resource; other threads may be reading
        // Read from the resource...

        // When done accessing resource, relinquish lock so other code can access the resource
        asyncLock.Release();

        // Execute whatever code you want here...
    }