Search code examples
c#.netmultithreadinglockingmonitor

Monitor vs lock


When is it appropriate to use either the Monitor class or the lock keyword for thread safety in C#?

EDIT: It seems from the answers so far that lock is short hand for a series of calls to the Monitor class. What exactly is the lock call short-hand for? Or more explicitly,

class LockVsMonitor
{
    private readonly object LockObject = new object();
    public void DoThreadSafeSomethingWithLock(Action action)
    {
        lock (LockObject)
        {
            action.Invoke();
        }
    }
    public void DoThreadSafeSomethingWithMonitor(Action action)
    {
        // What goes here ?
    }
}

Update

Thank you all for your help : I have posted a another question as a follow up to some of the information you all provided. Since you seem to be well versed in this area, I have posted the link: What is wrong with this solution to locking and managing locked exceptions?


Solution

  • Eric Lippert talks about this in his blog: Locks and exceptions do not mix

    The equivalent code differs between C# 4.0 and earlier versions.


    In C# 4.0 it is:

    bool lockWasTaken = false;
    var temp = obj;
    try
    {
        Monitor.Enter(temp, ref lockWasTaken);
        { body }
    }
    finally
    {
        if (lockWasTaken) Monitor.Exit(temp);
    }
    

    It relies on Monitor.Enter atomically setting the flag when the lock is taken.


    And earlier it was:

    var temp = obj;
    Monitor.Enter(temp);
    try
    {
       body
    }
    finally
    {
        Monitor.Exit(temp);
    }
    

    This relies on no exception being thrown between Monitor.Enter and the try. I think in debug code this condition was violated because the compiler inserted a NOP between them and thus made thread abortion between those possible.