I'm not sure it is so simple question, as it's stated in: When should I use ConcurrentDictionary and Dictionary?.
In my case, I have only one thread which calls Add
to add items, and several threads calls Remove
to remove items (one per item, actually Timer
s). There are many reading threads. Also, my logic ensures that items to be deleted are not in use anymore (time based key), i.e. it would not read them.
Also, it should handle a very high input rate.
So maybe Dictionary
is better here, because it has a better performance, and there's no real concurrent write on the same resource (I'm not sure). After all, it's not a linked list type of collection that needs to update pointers between items upon any change. Are concurrent Add
and Get
may be buggy in such case with a non thread-safe Dictionary
?
In other words, is the Add
operation non-atomic in a way that Get
operation for the same key would return a buggy object because the Dictionary
has a non stable state? Maybe it's enough to sync the Remove
operations only?
Update: Notice I have only 1 thread that adds items, several Timer threads that delete a single item each (it must be an item which was already added), and Get operations are not done on items which are behind the time scope (those which are to be deleted). Also, The single adding thread makes a list of already-added items to be read by the handling reading threads. That's why I think it may work without any lock (or only lock for the Remove operations).