public static async Task TrivialLock()
        {
            using var @lock = new AsyncExclusiveLock();
            True(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(10)));
            False(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(100)));
            await ThrowsAsync <TimeoutException>(() => @lock.AcquireAsync(TimeSpan.FromMilliseconds(100)));

            @lock.Release();
            True(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(100)));
        }
Exemplo n.º 2
0
        public static async Task ConcurrentLock()
        {
            using var are   = new AutoResetEvent(false);
            using var @lock = new AsyncExclusiveLock();
            await @lock.AcquireAsync(TimeSpan.Zero);

            var task = new TaskCompletionSource <bool>();

            ThreadPool.QueueUserWorkItem(async state =>
            {
                False(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(10)));
                True(ThreadPool.QueueUserWorkItem(static ev => ev.Set(), are, false));
                await @lock.AcquireAsync(DefaultTimeout);
                @lock.Release();
                task.SetResult(true);
            });