By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Locking in C#

P: n/a
Does anyone know whether or not it is faster to lock a simple object,
rather than a complex type?

For example:

Dictionary<string, SomeOtherClassdict = new Dictionary<string,
SomeOtherClass>();
object lockObject = new object();

--- Example 1 ---

lock (dict)
{
// Do something with dict
}

--- Example 2 ---

lock (lockObject)
{
// Do something with dict
}

I have seen a improvment in performance in the simple object locking
v.s. locking the entire dictionary, but what are the side affects?
And why am I seeing that improvment in the first place?

Thanks
Nov 15 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Akula,

I doubt you have seen a performance improvement, as what is actually
being locked on is an internal member of the object which has nothing to do
with anything else in the object (it's type, members, etc, etc).

If you are seeing a difference, I would wager that it is in the cost of
constructing the different objects, and not actually locking on them.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Akula" <ar********@gmail.comwrote in message
news:84**********************************@b40g2000 prf.googlegroups.com...
Does anyone know whether or not it is faster to lock a simple object,
rather than a complex type?

For example:

Dictionary<string, SomeOtherClassdict = new Dictionary<string,
SomeOtherClass>();
object lockObject = new object();

--- Example 1 ---

lock (dict)
{
// Do something with dict
}

--- Example 2 ---

lock (lockObject)
{
// Do something with dict
}

I have seen a improvment in performance in the simple object locking
v.s. locking the entire dictionary, but what are the side affects?
And why am I seeing that improvment in the first place?

Thanks

Nov 15 '07 #2

P: n/a
Akula <ar********@gmail.comwrote:
Does anyone know whether or not it is faster to lock a simple object,
rather than a complex type?
You don't lock an object - you lock a reference. The locking itself
doesn't really interact with the object.

<snip>
I have seen a improvment in performance in the simple object locking
v.s. locking the entire dictionary, but what are the side affects?
And why am I seeing that improvment in the first place?
I suspect you're seeing the improvement due to flaws in your
benchmarking methodology, although it's hard to say without seeing the
code.

However, I personally almost always use a separate object to provide
the reference for locking, as this means I can ensure it's never made
publically available, which helps to avoid threading issues.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '07 #3

P: n/a
Interesting... but there is a definite performance increase when I
merely switch the object being locked (from a class, to a simple
object).

Is there a resource on the web that describes, in detail, the
internals of the lock statement? I have looked at my complied code
using ildasm, and like you said, there appears to be no difference in
how it locks.
Nov 15 '07 #4

P: n/a
Akula <ar********@gmail.comwrote:
Interesting... but there is a definite performance increase when I
merely switch the object being locked (from a class, to a simple
object).
Could you post a short but complete program which demonstrates this?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Is there a resource on the web that describes, in detail, the
internals of the lock statement? I have looked at my complied code
using ildasm, and like you said, there appears to be no difference in
how it locks.
Lock just compiles into pair of calls to Monitor.Enter/Exit with
try/finally.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '07 #5

P: n/a
Ok, so after some more investigation, and downloading some new
profiling tools, I have determined that it was the profiler that I was
using.

So, those of you who were suggesting that the metrics I was gathering
were wrong are correct.

Thanks for the help, all.
Nov 15 '07 #6

P: n/a
Akula <ar********@gmail.comwrites:
Interesting... but there is a definite performance increase when I
merely switch the object being locked (from a class, to a simple
object).

Is there a resource on the web that describes, in detail, the
internals of the lock statement? I have looked at my complied code
using ildasm, and like you said, there appears to be no difference in
how it locks.
I found Jon's article here very helpful in understanding how C#
locking works:

http://www.yoda.arachsys.com/csharp/.../locking.shtml

----Scott.
Nov 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.