Skip to content

Instantly share code, notes, and snippets.

@xuanye
Created March 25, 2018 00:35
Show Gist options
  • Save xuanye/9f1922500e868ea812d7b77af34bb1ff to your computer and use it in GitHub Desktop.
Save xuanye/9f1922500e868ea812d7b77af34bb1ff to your computer and use it in GitHub Desktop.
异步情况下线程同步
SemaphoreSlim mutex = new SemaphoreSlim(1);
int value;
Task<int> GetNextValueAsync(int current);
async Task UpdateValueAsync()
{
await mutex.WaitAsync().ConfigureAwait(false);
try
{
value = await GetNextValueAsync(value);
}
finally
{
mutex.Release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment