Monitor and lock in C # and differences

Source: Internet
Author: User

 Monitor Object

The 1.monitor.enter (object) method is to acquire the lock, the Monitor.Exit (object) method is to release the lock, which is the most commonly used two methods of Monitor, of course, in order to avoid acquiring locks after the lock because of the exception, the lock cannot be released, Therefore, you need to release the lock (Monitor.Exit ()) in the structure body of finally{} after try{} catch () {}.

Common properties and methods for 2.Monitor:

Enter (object) to obtain an exclusive lock on the specified object.

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

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

Pulse notifies the thread in the wait queue to lock changes in the state of the object.

PulseAll notifies all waits for thread object state changes.

TryEnter (object) attempted to obtain an exclusive lock on the specified object.

TryEnter (object, Boolean) attempts to obtain an exclusive lock on the specified object and automatically sets a value that indicates whether the lock was obtained.

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

                                       Lock keyword

The 1.Lock keyword is actually a syntactic sugar that encapsulates the monitor object and adds a mutex to object, and when a process enters this snippet, the object object is given a mutex, while other B processes enter this snippet to check if the object has a lock? If a lock continues to wait until the a process finishes running the code snippet and unlocks the object, the B process is able to get the object objects to lock on it and access the code snippet.

The monitor object structure in the 2.Lock keyword package 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 (); Try not to use public variables and strings, this, and value types.

The difference between monitor and lock

1.Lock is the syntax sugar of monitor.

2.Lock can only be locked for reference types.

3.Monitor is able to lock a value type, essentially boxing a value type when Monitor.Enter (object).

4.Monitor has other features as well.

This article code example:

    classProgram {Private Static Objectobj =New Object();  Public voidlocksomething () {Lock(obj) {dosomething (); }        }         Public voidmonitorsomething () {Try{monitor.enter (obj);            DoSomething (); }            Catch(Exception ex) {}finally{monitor.exit (obj); }        }         Public voiddosomething () {//to do something specific        }    }

Monitor and lock in C # and differences

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.