469,272 Members | 1,627 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

Lock and Synchronized

Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to when to use one and not the other? For instance, if I wanted to remove an item in a SortedList, would it be better to lock() it or do it via the Synchronized wrapper? Why choose one over the other

SortedList sl

lock(sl

sl.Remove(item)
O

SortedList sl2 = SortedList.Synchronized(sl)
sl2.Remove(item)

Same question for iterations. Does lock() and synchronized do the same thing

lock(sl

for(int i = 0; i < sl.Count; i++


O

SortedList sl2 = SortedList.Synchronized(sl)
for(int i = 0; i < sl2.Count; i++

Thanks

Rich Sienkiewic
Dictaphone Corp
Jul 21 '05 #1
4 7559
Synchronized? IsSynchronized is a property that returns a bool. It doesn't
return "a synchronized object". You're thinking of SyncRoot, maybe?

But anyway, the correct way is to lock on the SyncRoot object returned by
the hash/list class. Locking on the type instance itself is not recommended
and is actually unsafe.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"Rich Sienkiewicz" <an*******@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock()
statement does the same thing. Is there any rules as to when to use one and
not the other? For instance, if I wanted to remove an item in a SortedList,
would it be better to lock() it or do it via the Synchronized wrapper? Why
choose one over the other.
SortedList sl;

lock(sl)
{
sl.Remove(item);
}

OR

SortedList sl2 = SortedList.Synchronized(sl);
sl2.Remove(item);
Same question for iterations. Does lock() and synchronized do the same thing?
lock(sl)
{
for(int i = 0; i < sl.Count; i++)
{
}
}

OR

SortedList sl2 = SortedList.Synchronized(sl);
for(int i = 0; i < sl2.Count; i++)
{
}
Thanks,

Rich Sienkiewicz
Dictaphone Corp

Jul 21 '05 #2
Klaus H. Probst <us*******@vbbox.com> wrote:
Synchronized? IsSynchronized is a property that returns a bool. It doesn't
return "a synchronized object". You're thinking of SyncRoot, maybe?
No, he's thinking of Synchronized, rather than IsSynchronized.
SortedList.Synchronized, however, is a method which returns a
synchronized wrapped for the SortedList.
But anyway, the correct way is to lock on the SyncRoot object returned by
the hash/list class. Locking on the type instance itself is not recommended
and is actually unsafe.


The latter is certainly true. The former is "sort-of" true.

The synchronized wrapper make each call to the list synchronized.
However, most of the time that isn't actually enough - you typically
want to synchronize a *sequence* of operations rather than one
operation at a time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Thanks for the reply guys. But I'm more confused. I should not lock the instance like this

lock(sl

but lock the SyncRoot, like this

lock(sl.SyncRoot

But the help system (C# Programmer's Reference) describing the lock statement says "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." It never mentions the SyncRoot, just lock the instance itself.

The help system is misleading then in talking about the Synchronized method. It says, for the SortedList.Synchronized method, "Returns a synchronized (thread-safe) wrapper for the SortedList.". Then in the Remarks section it says "To guarantee the thread safety of the SortedList, all operations must be done through this wrapper only." That to me says get and use the wrapper for thread safety. If this is not true then this must be reworded, no

So my question remains, if I have one thread iterating over a SortedList and another thread can come along and add/delete an item from it, what should each thread do to the SortedList, lock or Synchronize

Thanks

Rich
Jul 21 '05 #4
Locking the object is fine.

If you were thinking of using:

lock(this)

You should know that because you're looking on your instance, users of your
class could also lock on it and screw you up. Whether or not that is an
issue depends on what you think your users might do. The same situation
exists if you're doing:

lock(typeof(MyClass))

If you don't want to do this, you can easily allocate a static or instance
object (ie object myLock = new object()), and then lock on that.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Rich Sienkiewicz" <an*******@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
Thanks for the reply guys. But I'm more confused. I should not lock the instance like this:
lock(sl)

but lock the SyncRoot, like this?

lock(sl.SyncRoot)

But the help system (C# Programmer's Reference) describing the lock statement says "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." It never mentions the SyncRoot,
just lock the instance itself.
The help system is misleading then in talking about the Synchronized method. It says, for the SortedList.Synchronized method, "Returns a
synchronized (thread-safe) wrapper for the SortedList.". Then in the Remarks
section it says "To guarantee the thread safety of the SortedList, all
operations must be done through this wrapper only." That to me says get and
use the wrapper for thread safety. If this is not true then this must be
reworded, no?
So my question remains, if I have one thread iterating over a SortedList and another thread can come along and add/delete an item from it, what
should each thread do to the SortedList, lock or Synchronize?
Thanks,

Rich

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by memememe | last post: by
14 posts views Thread by Sharon | last post: by
4 posts views Thread by Rich Sienkiewicz | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.