Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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