I'm trying to understand how the new C # 5 asynchrony function works. Suppose I want to create an atomic increment function to increment an integer in a dummy IntStore. Several calls are made for this function in only one thread.
async void IncrementKey(string key) {
int i = await IntStore.Get(key);
IntStore.Set(key, i+1);
}
It seems to me that this function is erroneous. Two calls to IncrementKey can get the same number back from the IntStore (say 5), and then set it to 6, thereby losing one of the increments?
How could this be rewritten if IntStore.Get is asynchronous (returns a task) to work correctly?
Performance is crucial, is there a solution that avoids blocking?
source
share