Monitor vs lock
C#.NetMultithreadingLockingMonitorC# Problem Overview
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: https://stackoverflow.com/questions/4979331/what-is-wrong-with-this-solution-to-locking-and-managing-locked-exceptions
C# Solutions
Solution 1 - C#
Eric Lippert talks about this in his blog: https://docs.microsoft.com/en-us/archive/blogs/ericlippert/locks-and-exceptions-do-not-mix">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.
Solution 2 - C#
lock
is just shortcut for Monitor.Enter
with try
+ finally
and Monitor.Exit
. Use lock statement whenever it is enough - if you need something like TryEnter, you will have to use Monitor.
Solution 3 - C#
A lock statement is equivalent to:
Monitor.Enter(object);
try
{
// Your code here...
}
finally
{
Monitor.Exit(object);
}
However, keep in mind that Monitor can also Wait() and Pulse(), which are often useful in complex multithreading situations.
Update
However in C# 4 its implemented differently:
bool lockWasTaken = false;
var temp = obj;
try
{
Monitor.Enter(temp, ref lockWasTaken);
//your code
}
finally
{
if (lockWasTaken)
Monitor.Exit(temp);
}
Thanx to CodeInChaos for comments and links
Solution 4 - C#
Monitor
is more flexible. My favorite use case of using monitor is when you don't want to wait for your turn and just skip:
//already executing? forget it, lets move on
if(Monitor.TryEnter(_lockObject))
{
//do stuff;
Monitor.Exit(_lockObject);
}
Solution 5 - C#
As others have said, lock
is "equivalent" to
Monitor.Enter(object);
try
{
// Your code here...
}
finally
{
Monitor.Exit(object);
}
But just out of curiosity, lock
will preserve the first reference you pass to it and will not throw if you change it. I know it's not recommended to change the locked object and you don't want to do it.
But again, for the science, this works fine:
var lockObject = "";
var tasks = new List<Task>();
for (var i = 0; i < 10; i++)
tasks.Add(Task.Run(() =>
{
Thread.Sleep(250);
lock (lockObject)
{
lockObject += "x";
}
}));
Task.WaitAll(tasks.ToArray());
...And this does not:
var lockObject = "";
var tasks = new List<Task>();
for (var i = 0; i < 10; i++)
tasks.Add(Task.Run(() =>
{
Thread.Sleep(250);
Monitor.Enter(lockObject);
try
{
lockObject += "x";
}
finally
{
Monitor.Exit(lockObject);
}
}));
Task.WaitAll(tasks.ToArray());
Error:
> An exception of type 'System.Threading.SynchronizationLockException' > occurred in 70783sTUDIES.exe but was not handled in user code > > Additional information: Object synchronization method was called from > an unsynchronized block of code.
This is because Monitor.Exit(lockObject);
will act on lockObject
which has changed because strings
are immutable, then you're calling it from an unsynchronized block of code.. but anyway. This is just a fun fact.
Solution 6 - C#
Both are the same thing. lock is c sharp keyword and use Monitor class.
http://msdn.microsoft.com/en-us/library/ms173179(v=vs.80).aspx
Solution 7 - C#
The lock and the basic behavior of the monitor (enter + exit) is more or less the same, but the monitor has more options that allows you more synchronization possibilities.
The lock is a shortcut, and it's the option for the basic usage.
If you need more control, the monitor is the better option. You can use the Wait, TryEnter and the Pulse, for advanced usages (like barriers, semaphores and so on).
Solution 8 - C#
Lock Lock keyword ensures that one thread is executing a piece of code at one time.
lock(lockObject)
{
// Body
}
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement and then releasing the lock
If another thread tries to enter a locked code, it will wait, block, until the object is released.
Monitor The Monitor is a static class and belongs to the System.Threading namespace.
It provides exclusive lock on the object so that only one thread can enter into the critical section at any given point of time.
Difference between Monitor and lock in C#
The lock is the shortcut for Monitor.Enter with try and finally. Lock handles try and finally block internally Lock = Monitor + try finally.
If you want more control to implement advanced multithreading solutions using TryEnter()
Wait()
, Pulse()
, and PulseAll()
methods, then the Monitor class is your option.
C# Monitor.wait()
: A thread wait for other threads to notify.
Monitor.pulse()
: A thread notify to another thread.
Monitor.pulseAll()
: A thread notifies all other threads within a process
Solution 9 - C#
In addition to all above explanations, lock is a C# statement whereas Monitor is a class of .NET located in System.Threading namespace.