I know that the int wont have a fixed position in memory so it simply cant work like that. But The exact same portion of code will be run concurrently with different names, parameters e.t.c
I need to essentially pass a string of "Name" and then somehow increment one of the items in my int array.
Dictionary<string, int> intStats = new Dictionary<string, int>();
This dictionary stores all the stats based on the "Name" supplied as the dictionaries string key.
And since im using a LOT of multi-threading, I wish to keep the int count as synchronized as possible. Which is why im attempting to use Interlocked.Increment(ref intStats[theName]);
But unfortunately this wont work.
Is there any alternatives that would work for my situation?
First, I suggest creating a custom type that captures the semantics of your abstract data type. That way you can experiment with different implementations, and that way your call sites become self-documenting.
So: what implementation choices might you make, given that this must be threadsafe?
a private
Dictionary<string, int>would work but you'd have to lock the dictionary on every access, which could get expensive.a private
ConcurrentDictionary<string, int>, but keep in mind that you have to useTryUpdatein a loop to make sure you don't lose values.make a wrapper type:
This is one of the rare cases when you'd want to make a public field. Now make a
ConcurrentDictionary<string, MutableInt>, and thenInterlockedIncrementthe public field. Now you don't have toTryUpdate, but there is still a race here: if two threads both attempt to add the same name at the same time for the first time then you have to make sure that only one of them wins. UseAddOrUpdatecarefully to ensure that this race doesn't happen.Implement your own concurrent dictionary as a hash table that indexes into an int array;
InterlockedIncrementon elements of the array. Again, you'll have to be extremely careful when a new name is introduced into the system to ensure that hash collisions are detected in a threadsafe manner.Hash the string to one of n buckets, but this time the buckets are immutable dictionaries. Each bucket has a lock; lock the bucket, create a new dictionary from the old one, put it back in the bucket, unlock the bucket. If there is contention, increase n until it goes away.