. NET source Analysis-Concurrentdictionary<tkey, tvalue>

Source: Internet
Author: User

  • 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.