8.24.2012

Difference between lock and Monitor.Enter()

Difference between lock and Monitor.Enter()
S.No
lock
Monitor.Enter()
1 lock keyword basically provides a shortcut to Enter method of Monitor class. Monitor is used to provide thread synchronization.It means till the Thread in which the method is being used finishes its task, no other thread can access the object.

Example:

lock (object)
{

}

It is compiled into

Monitor.Enter(object);
try
{
//code
}
finally
{
Monitor.Exit(object);
}

No comments:

Post a Comment