locked
Difference between Lock statement and Monitor Class RRS feed

  • Question

  • Hi All,

    i know we use Lock and Monitor class for synchronisation.I know Lock is shortcut for Monitor class we can say.But there would be some difference between these two atleast from usage point of view.That's why microsoft introduces these two terms.I got some answer through net even it is present in this forum too but not satisfied.Please help to get this clear.any help will be much appreciated.

    Wednesday, March 16, 2011 12:34 PM

Answers

  • I suggest you following reference which describes many questions related to synchronization, among them you will find explanation of lock construct:

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en

    On page 108 you will find:

    The code:

     

    lock (someObject)
    {
    // ... critical region of code
    }
    

    will be compiled into following code (.NET 4):

    bool lockTaken = false;
    try
    {
    Monitor.Enter(someObject, ref lockTaken);
    // ... critical region of code
    }
    finally
    {
    if (lockTaken) Monitor.Exit(someObject);
    }
    

    in contrast to the following code produced in (.NET 3):

    var lockObj = someObject;
    Monitor.Enter(lockObj);
    try
    {
    // ... critical region of code
    }
    finally
    {
    Monitor.Exit(lockObj);
    }
    
    Which is not as exception safe as a code produced for .NET 4.

     

    So, whenever possible I suggest using lock construct in favor of manually playing with Monitor class (especially when using .NET >= 4). Thus, getting benefit of future improvements, and well tested code generated by compiler.

     


    still under construction...
    • Proposed as answer by XavierAP_old Friday, March 18, 2011 10:30 AM
    • Marked as answer by Paul Zhou Thursday, March 24, 2011 11:51 AM
    Wednesday, March 16, 2011 1:25 PM
  • Hi friend,

     

    Welcome to the MSDN forum!

     

    Like the lock keyword, monitors prevent blocks of code from simultaneous execution by multiple threads. The Enter method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls Exit. This is just like using the lock keyword. In fact, the lock keyword is implemented with the Monitor class.

     

    Using the lock keyword is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock insures that the underlying monitor is released, even if the protected code throws an exception. This is accomplished with the finally keyword, which executes its associated code block regardless of whether an exception is thrown.

     

    Much further information about Thread Synchronization


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, March 24, 2011 11:51 AM
    Friday, March 18, 2011 8:22 AM

All replies

  • I suggest you following reference which describes many questions related to synchronization, among them you will find explanation of lock construct:

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=86b3d32b-ad26-4bb8-a3ae-c1637026c3ee&displaylang=en

    On page 108 you will find:

    The code:

     

    lock (someObject)
    {
    // ... critical region of code
    }
    

    will be compiled into following code (.NET 4):

    bool lockTaken = false;
    try
    {
    Monitor.Enter(someObject, ref lockTaken);
    // ... critical region of code
    }
    finally
    {
    if (lockTaken) Monitor.Exit(someObject);
    }
    

    in contrast to the following code produced in (.NET 3):

    var lockObj = someObject;
    Monitor.Enter(lockObj);
    try
    {
    // ... critical region of code
    }
    finally
    {
    Monitor.Exit(lockObj);
    }
    
    Which is not as exception safe as a code produced for .NET 4.

     

    So, whenever possible I suggest using lock construct in favor of manually playing with Monitor class (especially when using .NET >= 4). Thus, getting benefit of future improvements, and well tested code generated by compiler.

     


    still under construction...
    • Proposed as answer by XavierAP_old Friday, March 18, 2011 10:30 AM
    • Marked as answer by Paul Zhou Thursday, March 24, 2011 11:51 AM
    Wednesday, March 16, 2011 1:25 PM
  • Hi friend,

     

    Welcome to the MSDN forum!

     

    Like the lock keyword, monitors prevent blocks of code from simultaneous execution by multiple threads. The Enter method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls Exit. This is just like using the lock keyword. In fact, the lock keyword is implemented with the Monitor class.

     

    Using the lock keyword is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock insures that the underlying monitor is released, even if the protected code throws an exception. This is accomplished with the finally keyword, which executes its associated code block regardless of whether an exception is thrown.

     

    Much further information about Thread Synchronization


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, March 24, 2011 11:51 AM
    Friday, March 18, 2011 8:22 AM