- List Source Analysis
- Dictionary Source Code Analysis
- Concurrentdictionary Source Code Analysis
Analysis: http://www.cnblogs.com/brookshi/p/5583892.html
Following the dictionary source analysis, the previous article said here will not repeat
Concurrentdictionary Source Address: https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/ System/collections/concurrent/concurrentdictionary.cs
Concurrentdictionary A major feature is thread safety, before concurrentdictionary in the multi-threaded under the Dictionary, regardless of read and write to add a lock, not only trouble, performance is not very good, Because in the previous analysis we know that dictionary is composed of several buckets, the operation of different buckets even in multi-threaded can not affect each other, if a lock to the entire dictionary lock is a bit wasteful.
But there are two sides to everything, it is not advisable to add a lock to each bucket, the number of buckets and the number of dictionary elements is the same, and the bucket may be part of the empty, and access to the dictionary thread if the number is not too much also use not so many locks, think even if there are 10 threads in the operation of this dictionary, while the operation of the maximum of 10, even if 22 conflict access to the same bucket,5 lock is enough, Of course this is the best case, and the worst case scenario is that the 5 buckets use the same lock. Therefore, to get the best results, we need to try to take an optimal solution, while the factors are the number of buckets and the number of threads. The result we want is a lock enough but not a waste.
The result of Microsoft is that the default number of locks is the amount of CPU cores, the thread pool is the same number of default threads. With the expansion of the dictionary, the number of locks can also be increased, which can be specified in the constructor itself.
Here's a look at what the concurrentdictionary element does in the package.
. NET source Analysis-Concurrentdictionary<tkey, tvalue>