Custom thread-safe dictionary

Source: Internet
Author: User
Let's say that a data structure means thread security means that only one thread can rewrite it at a time. In this way, even if multiple threads access it, it will not generate data that is very unexpected to the thread. Dictionary in C # Is Not thread-safe. In the following example, a dictionary object is used as a Global static variable. There will be multiple threads to access it. So I need to wrap the dictionrary that comes with. net. The conflicting part is nothing more than the write part, so add a lock in the closest place to the write dictionary. Other outer code can have the same built-in dictionary. Let's look at the implementation interface of dictionary. syncroot is a mutex object in the Custom saftedictionary. Which thread can get it to operate on the internal dictionary. For example, new dictionary (). add a syncroot lock and change it to public void add (tkey key, tvalue value) {lock (syncroot) {d. add (Key, value) ;}} only one thread can access D at the same time point. add (Key, value); defines a global safedictionray object, it will not be operated by multiple threads at the same time. The example in the attachment defines a global safedictionray object helper. dicnetstatus indicates the connection status of the device corresponding to the IP address. The IP address is key, and the value is true/false. When the program starts, A dealingdata thread keeps refreshing helper. the value of dicnetstatus, Which is flushed once every 1 second. Mainform is the main interface thread. In addition, the mainform background thread displaydicstatus keeps reading helper. the value of dicnetstatus, Which is flushed once every one second, is displayed on the interface once, and the connection status of an IP device is displayed. Just like displaying different connection statuses of friends based on different portraits in the QQ list. Summarize the knowledge points involved in the appendix example: 1. If you solve the problem of multi-threaded operations on a global data object. Solution: Customize a thread-safe data object class. 2. How to call interface controls across threads. Solution: This. Invoke (sets the proxy of the interface control) and attaches the code. Correct the code. Example of safedictionary

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.