Go C # Understanding Lock

Source: Internet
Author: User

Original: http://www.cnblogs.com/apsnet/archive/2012/07/08/2581475.html

I. Why do you have to Lock,lock?

When we use threads, the most efficient way is, of course, asynchronous, where each thread runs at the same time and does not depend on and waits on each other. However, when different threads need to access a resource, it is necessary to synchronize the mechanism, that is, when reading and writing the same resource, we have to make the resource can only be manipulated by one thread at a time to ensure that each operation is effective and instantaneous, that is, the atomicity of its operation. Lock is the most commonly used synchronization method in C #, in the form of lock (OBJECTA) {codeb}.


Lock (OBJECTA) {Codeb} looks simple and actually has three meanings, which is essential for proper use:
1. Has objecta been lock? No, I'll lock it, or I'll wait until Objecta is released.
2. After lock is executed codeb other threads cannot call Codeb and cannot use Objecta.
3. Objecta is released after execution of Codeb, and codeb can be accessed by other threads.


Two. What happened to lock (this)?


Let's look at an example:

1 usingSystem;2 usingSystem.Threading;3 namespaceNamespace14 {5     classC16     {7         Private BOOLdeadlocked =true;8         //This method uses lock, and we want lock's code to be accessible only by one thread at a time9          Public voidLockme (Objecto)Ten         { One             Lock( This) A             { -                  while(deadlocked) -                 { thedeadlocked = (BOOL) O; -Console.WriteLine ("foo:i am Locked:("); -Thread.Sleep ( -); -                 } +             } -         } +         //methods that all threads can access concurrently A          Public voidDonotlockme () at         { -Console.WriteLine ("I am not locked:)"); -         } -     } -     class Program -     { in         Static voidMain (string[] args) -         { toC1 C1 =NewC1 (); +             //call Lockme in the T1 thread and set deadlock to True (deadlock will occur) -Thread T1 =NewThread (C1. Lockme); theT1. Start (true); *Thread.Sleep ( -); $             //in the main thread, lock C1Panax Notoginseng             Lock(C1) -             { the                 //call a method that is not lock + C1. Donotlockme (); A                 //call the Lock method and attempt to dismiss the deadlock theC1. Lockme (false); +             } -         } $}

In the T1 thread, Lockme calls lock (this), which is the C1 in the main function, when you call Lock (C1) in the main thread, you must wait for the lock block in T1 to complete before you can access C1, that is, all C1-related operations cannot be completed. So we see even C1. None of the Donotlockme () has been executed.


Make a slight change to the C1 code:

1 classC12     {3         Private BOOLdeadlocked =true;4         Private ObjectLocker =New Object();5         //This method uses lock, and we want lock's code to be accessible only by one thread at a time6          Public voidLockme (Objecto)7         {8             Lock(Locker)9             {Ten                  while(deadlocked) One                 { Adeadlocked = (BOOL) O; -Console.WriteLine ("foo:i am Locked:("); -Thread.Sleep ( -); the                 } -             } -         } -         //methods that all threads can access concurrently +          Public voidDonotlockme () -         { +Console.WriteLine ("I am not locked:)"); A         } at}

This time we use a private member as the lock variable (locker), and in Lockme we only lock the private locker, not the entire object. At this time rerun the program, you can see that although T1 deadlock, Donotlockme () can still be accessed by the main thread, Lockme () is still inaccessible, because the locked locker has not been released by T1.


Key points:
1. The disadvantage of lock (this) is that after a thread (for example, T1) locks an object by executing a method of that class using "lock" (for example, Lockme () of this example), the entire object cannot be accessed by another thread (for example, the main thread of this example)- This is because many people use lock (c1)-like code when using the class in other threads, such as the main thread of this example.
2. Locking is not just the code in the lock segment, the lock itself is thread -safe.
3. We should use private objects that do not affect other operations as locker.
4. When using lock, the lock object (locker) must be a reference type, if it is a value type, will cause the object to be boxed into a new reference object each time lock is used (in fact, if you use a value type, the C # compiler (3.5.30729.1) An error will be given at compile time).

Go C # Understanding Lock

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.