Last active
November 22, 2019 07:18
-
-
Save AdoraNwodo/fbe0b99e437217fb6088d20ea946d072 to your computer and use it in GitHub Desktop.
This file contains 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
class Store | |
{ | |
private static Mutex mutex = new Mutex(); | |
private List<string> storeProducts; | |
public Store() | |
{ | |
storeProducts = new List<string>(); | |
} | |
async Task<string> GetOrAddProduct(Product product) | |
{ | |
try | |
{ | |
mutex.WaitOne(); // controls access to code that isn't thread-safe | |
if(storeProducts.Contains(product.Name)) | |
{ | |
return product; | |
} | |
var token = HelperClass.GenerateTokenOrSomething(); | |
await HelperClass.UploadProductDetails(token, product); | |
storeProducts.Add(product.Name); | |
} | |
finally | |
{ | |
mutex.ReleaseMutex(); | |
return product.Name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment