Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SomeClass
- {
- private Task<ValueType> foo = null;
- private readonly SemaphoreSlim sem = new SemaphoreSlim();
- /* ... */
- Task<ValueType> getFoo()
- {
- var foo = this.foo;
- if (!foo) {
- return asyncInitFoo();
- }
- return foo;
- }
- async Task<ValueType> asyncInitFoo()
- {
- await sem.WaitAsync();
- try
- {
- if (foo == null) {
- foo = Task.FromResult(/* ... */);
- }
- return foo;
- }
- finally
- {
- sem.Release();
- }
- }
- /* ... */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement