Lock or Synchronized ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Rich Sienkiewicz said:
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.

Sometimes you would want to use the wrapper, sometimes just locking,
sometimes both.

The Syncronized wrapper does not make the obejct thread-safe. It just
replaces each method

SortedList sl;
sl.Remove(item);

with

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

You would still need to lock the object during iteration.

lock(sl.SyncRoot)
{
for(int i = 0; i < sl.Count; i++)
{
}
}


Oh, and lock the SyncRoot, not the object, because if another thread has a
Syncronized wrapper that thread will be using the SyncRoot's monitor, not
the container's.

David
 
Thanks for the reply.

You said that the wrapper does not make the object thread safe. But the help file, in the SortedList.Synchronized method, says "Returns a synchronized (thread-safe) wrapper for the SortedList". It states later on in the Remarks "To guarantee the thread safety of the SortedList, all operations must be done through this wrapper only." Now I'm confused

And lock the SyncRoot not the object? Don't do

lock(sl

but do this instead

lock(sl.SyncRoot

Thank

Rich
 
In looking up the SortedList.SyncRoot in the help system they say "To create a synchronized version of the SortedList, use the Synchronized method". They talk about the need to use the SyncRoot in derived classes. But their own example shows the lock

SortedList myCollection = new SortedList()
lock( myCollection.SyncRoot )
foreach ( Object item in myCollection )
// Insert your code here



Adds to the confusion for me

Rich
 
Rich Sienkiewicz said:
In looking up the SortedList.SyncRoot in the help system they say "To
create a synchronized version of the SortedList, use the Synchronized
method". They talk about the need to use the SyncRoot in derived classes.
But their own example shows the lock:
SortedList myCollection = new SortedList();
lock( myCollection.SyncRoot ) {
foreach ( Object item in myCollection ) {
// Insert your code here.
}
}

The synchronized wrapper locks the SyncRoot when performing any atomic
action on the collection.
These include insert an item, remove an item, create an iterator.

But the whole iteration loop is not an atomic action, and so you still need
to lock the collection's SyncRoot before you start the iteration.

This is why the synchronized wrapper does not, by itself, make the
collection completely thread-safe.

It is critical that other threads use the Syncronized wrapper or else when
you lock the collection's SyncRoot it won't stop them from accessing the
collection.

David
 
Back
Top