NightOwl888 commented on pull request #410: URL: https://github.com/apache/lucenenet/pull/410#issuecomment-774231417
I got it wrong. Upon closer inspection of the [`AddOrUpdate`](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.addorupdate?view=net-5.0) documentation: > If you call AddOrUpdate simultaneously on different threads, addValueFactory **may be called multiple times**, but its key/value pair might not be added to the dictionary for every call. > For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.) However, the addValueFactory and updateValueFactory delegates are **called outside the locks** to avoid the problems that can arise from executing unknown code under a lock. Therefore, AddOrUpdate is not atomic with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class. So in other words, this PR is **not** the equivalent functionality of the locks that exist now, as this method does not make the entire block atomic like the lock does. I am curious, what issues are you actually seeing with these locks? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
