I have a class with a field of type collection.
Questions:
- if I
lock(this)
, do I effectively lock the collection too? - what is more efficient, to do
lock(this)
or to create aSyncRoot
object and dolock(SyncRoot)
?
I have a class with a field of type collection.
Questions:
lock(this)
, do I effectively lock the collection too?lock(this)
or to create a SyncRoot
object and do lock(SyncRoot)
?Don't lock
on this
. It could be the case that someone else has used the instance as a lock
object too. Use specifically designated lock
objects.
1) if I lock(this), do I effectively lock the collection too?
No.
2) what is more efficient, to do lock(this) or to create a SyncRoot object and do lock(SyncRoot) ?
Efficient? Focus on semantics. lock
ing on this
is dangerous. Don't do it. The difference in performance, if any, is not material.
Seriously, it's akin to asking, what will get me to my destination faster, driving 100 MPH the wrong way down the freeway, or walking?
Always use lock(_syncRoot)
.
Where _syncRoot
is a private field (just has to be an object).
This is no difference in terms of efficiency, but you're better to have a private field that you're in control of to lock on. If you lock on this
, another object may also be locking on it.
See Why is lock(this) {...} bad? for a much better explanation. Also have a look at the msdn article on lock.
By locking on a collection, you aren't doing anything to stop it from being changed. A misunderstanding you might have, is that lock doesn't do anything special to stop that object being changed, it only works if every critical piece of code also calls lock.
When using lock
you aren't doing anything magical to the object you put inside the lock - it isn't making it read only or anything like that. It is just making a note that something has a lock reference to that object. So if anybody else tries to get a lock on that object at the same time it will do what you expect (prevent synchronous access).
What lock
doesn't do is care about any properties, fields or anything else in the object you are locking. So no, you aren't locking the collection at all.
This is explained at greater length in this question: Why is lock(this) {...} bad? (which I got from other answers but is an excellent answerand I felt it should be included here too).
As for efficiency I wouldn't expect there to be a lot of performance difference between the two. However as others have said you should not lock on something that might be locked by something outside of your control. This is why more often than not you will find private variables being created for this.
Personally I'd give it a more descriptive name than synclock
to describe exactly what the locking process is for (eg saveLock
).
A lot of people are saying that lock(this)
is dangerous. However The MSDN description of ICollection.SyncRoot states:
For collections whose underlying store is not publicly available, the expected implementation is to return the current instance
If a class follows this guideline, then yes, lock(this)
is effectively the same as lock(SyncRoot)
. But you shouldn't rely on implementation details like this, and should use the more explicit lock(SyncRoot)
.
Of course, if you don't want to publicly expose locking semantics, but use lock within your class implementation, then you should lock on a private object as others have recommended, and as MSDN recommends. But that doesn't seem to be what you're asking.
Both locking on an instance (lock(this)
) and locking on a public property (lock(SyncRoot)
) expose you to having the lock taken by code you don't control. If your intention is to expose locking semantics to callers, you have no choice but to do this.
Always lock on something over which the locking code has control. You have no control over a type or an instance of a type.