Groups | Blog | Home
all groups > dotnet framework > september 2006 >

dotnet framework : Algorithm of lock (obj) { ...} statement in C#


Carl Daniel [VC++ MVP]
9/28/2006 10:19:02 PM
[quoted text, click to view]

No, you're not.

The C# lock state is a shortcut for a pattern using the
System.Threading.Monitor object.

lock(this)
{
statement(s)
}

is identical to

Monitor.Enter(this);
try
{
statement(s)
}

finally
{
Monitor.Exit(this);
}

Under the covers, Monitor uses a pool of kernel synchronization objects and
attaches them to objects as needed. There are only as many low-level
synchronization objects in use as there are active monitors at any given
time.

-cd

Hyun-jik Bae
9/29/2006 12:00:00 AM
What is the algorithm of lock(obj) { ... }?

The statement above roughly makes me guess that the lock/unlock algorithm in
CLR is inefficient because it adds a critical section for each CLR object
instance.

Am I right? Please reply. Thanks in advance.

Hyun-jik Bae

AddThis Social Bookmark Button