Attention should be paid to the usage of the Dictionary in C,

Source: Internet
Author: User

Attention should be paid to the usage of the Dictionary in C,

A system that has been installed and used to handle large data volumes. This system was reported for the first time in several months:

「 Added a category containing the same index 」
An item with the same key has already been added.

Cause analysis:

The Dictionary of C # indicates the change of the structure. If it is declared as static, pay attention to before loading or adding the data, if ContainsKey is used only to determine whether a thread is not running (Example 2 is shown below), because it is not "concurrent row loading; thread" is safe (if there are multiple users logging in at the same time, it may cause frequent and even outgoing calls. The machine rate is not high, but it may occur once in a long time ).

Therefore, a lock action should be performed before a Dictionary operator is input or added, as shown in Example 3 below. Only one person (one individual row) is allowed at a time ), can enter the lock area, other people have to wait.

 


When 1 declares a Dictionary change



Example 2: Before the change (for a long time, I was able to develop a Failover method)



3. After Renewal


C # lock,
However, it must be declared as a static variable to be valid.
(Static changes indicate that all users on the entire website share the same website, rather than each user has a new website ).

-------------------------------------------------------------------
Dictionary moderate (msdn ):
Https://msdn.microsoft.com/zh-tw/library/xfhwa508 (v = vs.110). aspx
-------------------------------------------------------------------
Author refers to the article about Dictionary ):
Http://blog.darkthread.net/post-2012-01-31-dictionary-thread-safe.aspx

As mentioned above,. NET 4.0 adds a new System. Collections. Concurrent. ConcurrentDictionary <TKey, TValue>,
I cannot solve this problem. I suggest you do not use it.
-------------------------------------------------------------------
Lock mediation:
Http://www.dotblogs.com.tw/yc421206/archive/2011/01/07/20624.aspx
Https://msdn.microsoft.com/zh-tw/library/c5kehkcz.aspx
-------------------------------------------------------------------
ConcurrentDictionary <TKey, TValue> Differences:
Https://msdn.microsoft.com/zh-tw/library/dd287191 (v = vs.110). aspx

ConcurrentDictionary <TKey, TValue>. GetOrAdd method (TKey, TValue ):
Https://msdn.microsoft.com/zh-tw/library/ee378674 (v = vs.110). aspx

Dictionary + Locking versus ConcurrentDictionary
Http://www.codeproject.com/Articles/548406/Dictionary-plus-Locking-versus-ConcurrentDictionar
-------------------------------------------------------------------
ConcurrentDictionary makes your multi-threaded code more elegant
Http://www.bkjia.com/C_jc/968538.html

When this attribute is upgraded to static (Class-level), we need to consider its thread security because it may be accessed by multiple threads at the same time, of course, if this object is read-only, it does not matter thread security, but if this attribute can be written, it needs to be locked.

But in terms of performance, it cannot be received. We know that the lock will lock other threads out, whether it is read or write, it will be locked, and the performance is very poor.
-------------------------------------------------------------------

 

Related Article

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.