Created
March 25, 2018 00:35
-
-
Save xuanye/9f1922500e868ea812d7b77af34bb1ff to your computer and use it in GitHub Desktop.
异步情况下线程同步
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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