CLR series: represented by a piece of code

Source: Internet
Author: User

CLRThe series has not been updated for some time, mainly because the latest project is too busy. So it cannot be updated. Will continue later. Let's take a look.Code:

1 Class Testworker
2 {
3 Public   Void Domultithreadedwork ( Object Someparameter)
4 {
5 Lock (Lockobject)
6 {
7 // Lots of work
8 }
9 }
10
11 Private   String Lockobject =   " Lockit " ;
12 }

This code is very simple. Let's take a look at the problem with this code?

at first, it seems that there is no big problem. However, by carefully analyzing the code, you can see that there are still some serious problems. Assume that you lock mechanism understanding and string If the type has been studied, you will find a problem:

lock the keyword parameter must be an object of the reference type. This object is used to define the lock range. In the above example, the lock range is limited to this function because no reference to this object exists outside the function. Strictly speaking, it is provided to lock lock , and lock the subsequent synchronization code block accesses the container. As long as other threads lock the container before accessing the container, access to the object will be synchronized securely. Generally, it is best to avoid locking Public type or lock object instances that are not controlled by the application Program . Because uncontrolled Code may also lock the object. This may cause a deadlock, that is, two or more threads are waiting to release the same object. For more information about lock compiled by the compiler, see Article : Inside C #

.

StringInCLRThere are two important attributes: immutability and string resident. This means that there is only one instance for any given string in the entire program, and the same object represents the text in all threads in all running application domains. Therefore, as long as a lock is placed on a string with the same content anywhere in the application process, all instances of the string in the application will be locked. Therefore, it is best to lock private or protected members that are not temporarily retained.

Therefore, locking strings is especially dangerous.

Let's take a look.What is the meaning of string resident. Take a look at the following code

1 String A =   " Lockit " ;
2 String B =   " Lockit " ;
3 String C =   " Lockit " . Tolower ();
4 String D =   " Lock "   +   " It " ;
5 String E1 =   " Lock " ;
6 String E2 =   " It " ;
7 String EE = E1 +   " It " ;
8 String E = E1 + E2;
9 Console. writeline ( Object . Referenceequals (a, B ));
10 Console. writeline ( Object . Referenceequals (a, c ));
11 Console. writeline ( Object . Referenceequals (a, d ));
12 Console. writeline ( Object . Referenceequals (A, ee ));
13 Console. writeline ( Object . Referenceequals (a, e ));

The output result is as follows:

True, false, true, false, false

We know CLR When processing each object in the memory, an additional Syncblockindex Space, which is used for Object synchronization. Is one of the most commonly used types, CLR Department of MS In order to simplify the operation and optimize the performance, the first is String The creation process is simplified. When creating an object Keyword to implement; and String We don't need to do this. We only need to assign the corresponding characters to the corresponding string variable. Then they use Msil Different commands -- Generally, the reference object is created through newobj. Such Il Command to create a string variable Il The command is ldstr. ( Load string ). The second is to consider performance improvements and memory savings. for the creation of the same string, they generally do not allocate memory blocks for them separately. On the contrary, they will share a piece of memory. CLR Internal Maintenance Hashtable , This Hashtable Most maintainers create String . This Hashtable Of Key Corresponding String Itself, and Value Is assigned to this String Memory block reference. We know that after a managed process is created, the memory space of the managed process includes System domain , Shared domain And so on. Hashtable Is placed in System domain So it exists and is shared throughout the life cycle of the program. Generally, if you want to create String , CLR Based on this String Of Hash code Try Hashtable Find the same String If yes String To the corresponding variable. If not, create String , CLR Will first Managed heap Create Strng , And Hashtable Create Key-value, key For this String Itself, Value Is String Memory address, which is the most heavily assigned to the response variable.

The following two examples are as follows:FalseIt tells us that the resident mechanism does not work for a dynamically created string. In this caseMsilDifferent. But we can useSystem. StringStatic Method inIntern.

Through the above analysis, we will return to the first problem, and we will know what problems will occur in this Code. This shows that you should be careful with your code during your work. You can see some small code.. NetInternal implementation mechanism and negligence during self-writing.

Reference: http://msdn.microsoft.com/zh-cn/library/ms173179 (vs.80). aspx

CLR continues .. Let's talk about what we want to see about Clr and see if we can write it. I can't write it. I can ask experts to write it to satisfy everyone's needs.

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.