The differences between monitor and lock in C #

Source: Internet
Author: User

Monitor object

1. monitor. the enter (object) method is to obtain the lock, monitor. the exit (object) method is to release the lock, which is the two most commonly used methods of monitor. Of course, to avoid the lock being released due to exceptions after the lock is obtained, therefore, you need to release the lock (Monitor. exit ()).

2. Common attributes and methods of monitor:

Enter (object) gets the exclusive lock on the specified object.

Exit (object) releases the exclusive lock on the specified object.

Isentered determines whether the current thread retains the specified object lock.

The pulse notifies the thread in the waiting queue of changing the status of the locked object.

Pulseall notifies all pending thread object status changes.

Tryenter (object) attempts to obtain the exclusive lock of the specified object.

Tryenter (object, Boolean) attempts to obtain the exclusive lock on the specified object, and automatically sets a value to indicate whether the lock has been obtained.

Wait (object) releases the lock on the object and blocks the current thread until it acquires the lock again.

 
Lock keyword

1. The lock keyword is actually a syntactic sugar. It encapsulates the monitor object and adds a mutex lock to the object. process a enters thisCodeThe object is added with a mutex lock. When other B processes enter this code segment, check whether the object has a lock? If there is a lock, process B will continue to wait until process a finishes running the code segment and unlocks the object, so that process B can obtain the object and add the lock to it to access the code segment.

2. The structure of the monitor object encapsulated by the lock keyword is as follows:

 
Try{Monitor. Enter (OBJ); dosomething ();}Catch(Exception ex ){}Finally{Monitor. Exit (OBJ );}

3. The locked object should be declared as private static object OBJ = new object (); Do not use public variables and strings, this, and value types whenever possible.

Differences between monitor and lock

1. Lock is the syntactic sugar of monitor.

2. Lock can only be applied to the reference type.

3. Monitor can lock the value type, which is essentially a boxed value type during monitor. Enter (object.

4. Monitor has other functions.

Sample Code:

     Class  Program {  Private   Static   Object OBJ = New   Object  ();  Public   Void  Locksomething (){  Lock (OBJ) {dosomething ();}}  Public   Void  Monitorsomething (){  Try  {Monitor. Enter (OBJ); dosomething ();}  Catch  (Exception ex ){}  Finally  {Monitor. Exit (OBJ );}}  Public  Void  Dosomething (){  //  Do specific things  }} 

 

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.