This is the async-ready equivalent of Semaphore, similar to Stephen Toub's AsyncSempahore. Alternatively, you can use the SemaphoreSlim class, which is async-ready on .NET 4.5 and Windows Store platforms.

An AsyncSemaphore keeps a count, which is the number of open "slots" it has available to satisfy waiters. Any task may increase the number of slots available by calling Release.

The task returned from WaitAsync will enter the Completed state when the AsyncSemaphore has given it a slot. That same task will enter the Canceled state if the CancellationToken is signaled before the wait is satisfied; in that case, the AsyncSemaphore does not lose a slot.

You can call WaitAsync with an already-cancelled CancellationToken to attempt to acquire a slot from the AsyncSemaphore immediately without actually entering the wait queue.


// An async-compatible semaphore.
public sealed class AsyncSemaphore
  // Creates a new async-compatible semaphore with the specified initial count.
  public AsyncSemaphore(int initialCount);

  // Gets a semi-unique identifier for this asynchronous semaphore.
  public int Id { get; }

  // Gets the number of slots currently available on this semaphore.
  public int CurrentCount { get; }

  // Asynchronously waits for a slot in the semaphore to be available.
  public Task WaitAsync(CancellationToken cancellationToken = new CancellationToken());

  // Releases the semaphore.
  public void Release(int releaseCount = 1);

Last edited Nov 17, 2012 at 2:24 PM by StephenCleary, version 1


No comments yet.