Analysis of the differences between Monitor objects and Lock keywords in C

  • 2020-05-12 03:13:49
  • OfStack

Monitor object

1. Monitor.Enter (object) method is to acquire the lock, and Monitor.Exit (object) method is to release the lock, which are the two most commonly used methods of Monitor. Of course, in the process of using try{} catch(){} in finally{} structure after try{} catch(){}, in order to avoid the lock cannot be released due to an exception.
2. Common properties and methods of Monitor:

Enter(Object) gets an exclusive lock on the specified object.
Exit(Object) releases an exclusive lock on the specified object.
IsEntered determines whether the current thread retains the specified object lock.
Pulse notifies threads in the waiting queue of changes to the state of the locked object.
PulseAll notifies all pending thread object state changes.
TryEnter(Object) attempted to obtain an exclusive lock for the specified object.
TryEnter(Object, Boolean) attempts to obtain an exclusive lock on the specified object and automatically sets a value indicating whether the lock has been obtained.
Wait(Object) releases the lock on the object and blocks the current thread until it reacquires the lock.


Lock keyword

1. The Lock keyword is actually a grammar sugar, which encapsulates the Monitor object and adds a mutex to object. When the A process enters this code segment, it will add a mutex to the object object. If there is a lock, it continues to wait for the A process to finish running the code segment and unlock the object object before the B process can acquire the object object and lock it and access the code segment.

2. The structure of Monitor object encapsulated by 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(); Avoid public variables and strings, this, and value types.

The difference between Monitor and Lock

1.Lock is the grammatical sugar of Monitor.
2.Lock can only be locked for reference types.
3.Monitor is able to lock value types, essentially Monitor.Enter (object) when boxing value types.
4.Monitor also has some other functions.

Code examples for this article:


    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 articles: