Rx Challenge #11–Solution

Thursday, September 15, 2016

Rx Challenge #11–Solution You can find the solution for Challenge 11 below. Previous challenges available here. The following code snippet implement the base functionality which is similar to the functionality of SemaphoreSlim. Code Snippet public class AsyncLock {     private int _counter = 0;     private readonly int _limit;     // semantic Task     private ConcurrentQueue<TaskCompletionSource<object>> _tcsQueue =         new ConcurrentQueue<TaskCompletionSource<object>>();       #region Ctor       /// <summary>     /// Initializes a new instance of the <see cref="AsyncLock"/> class.     /// </summary>     /// <param name="limit">The limit.</param>     public AsyncLock(int limit)     {         _limit = limit;     }       #endregion // Ctor       #region WaitAsync  ...
no comments

Rx Challenge #11

Thursday, September 8, 2016

Rx Challenge #11 This challenge is practice of building something already exists, by your own. The challenge is to build async lock similar to the functionality of SemaphoreSlim WaitAsync / Release. You can check this post for the functionality of SemaphoreSlim. You can consider the following building block for this challenge: - Concurrent Collection - Interlocked - TaskCompletionSource You can validate your solution against this Test Code Snippet public async Task AsyncLock_Test() {     // arrange     const int LIMIT = 3;     var gate = new TaskCompletionSource<object>();     var barrier = new CountdownEvent(LIMIT);     var asyncLock =...
no comments

Semaphore Slim and Async

Tuesday, August 16, 2016

Semaphore Slim and Async On this post I will focus on async waiting on Semaphore (using SemaphoreSlim). In general .NET is having 2 Semaphore implementations: - Semaphore which is the legacy implementation (but support cross process operations) - SemaphoreSlim which is lightweight version. It bring some performance improvements but it don’t support cross process operation. this post will focus on SemaphoreSlim WaitAsync API. Let start analyzing the following code snippet, think what’s wrong with it: Code Snippet private static SemaphoreSlim _gate = new SemaphoreSlim(4); static void Main(string args) {     for (int i = 0;...
no comments