Skip to content

Instantly share code, notes, and snippets.

@AdoraNwodo
Last active November 22, 2019 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdoraNwodo/fbe0b99e437217fb6088d20ea946d072 to your computer and use it in GitHub Desktop.
Save AdoraNwodo/fbe0b99e437217fb6088d20ea946d072 to your computer and use it in GitHub Desktop.
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