lock(list) vs. lock(list.syncroot)


J

Jacob

Hi there,

Say I have an ArrayList named list. What's the difference between using

lock(list) { .... }

and

lock(list.SyncRoot) { .... }
 
Ad

Advertisements

W

William Stacey [MVP]

In 1.1, IIRC, there would be no difference as list.SyncRoot was "this".
However, in 2.0, SyncRoot is not "this" but a seperate object. Use SyncRoot
if you want to lock on that. However, I would just use my own syncRoot
object in your containing class.

--
William Stacey [MVP]

| Hi there,
|
| Say I have an ArrayList named list. What's the difference between using
|
| lock(list) { .... }
|
| and
|
| lock(list.SyncRoot) { .... }
|
|
|
|
 
B

Barry Kelly

Jacob said:
Say I have an ArrayList named list. What's the difference between using

lock(list) { .... }

and

lock(list.SyncRoot) { .... }
You may have used ArrayList.ReadOnly or ArrayList.Synchronized to wrap
the list. SyncRoot filters through the wrappers to get the underlying
object.

Generally, ArrayList itself isn't recommended any more, and List<T>
doesn't have a SyncRoot property because it doesn't have virtual methods
- it's not designed for wrapping in the same way as ArrayList, for
performance reasons.

-- Barry
 
J

Jacob

William Stacey said:
In 1.1, IIRC, there would be no difference as list.SyncRoot was "this".
However, in 2.0, SyncRoot is not "this" but a seperate object. Use
SyncRoot
if you want to lock on that. However, I would just use my own syncRoot
object in your containing class.
So it doesn't matter which way I do it as long as I always do it the same
way. Is that right?
 
B

Barry Kelly

Jacob said:
So it doesn't matter which way I do it as long as I always do it the same
way. Is that right?
Like I said, it depends on whether you've got the list wrapped up by
ArrayList.Synchronized or ArrayList.ReadOnly. That's why SyncRoot was
invented.

-- Barry
 
Ad

Advertisements

W

William Stacey [MVP]

Yes. You can use any object, as long as you use the same one and you know
it does not change behind the covers somehow. For clarity, I would use my
own syncRoot in the class such as this simple example:

public class Class1
{
private readonly object syncRoot = new object();
private readonly List<int> list = new List<int>();

public object SyncRoot
{
get{return this.syncRoot;}
}
public void Add(int i)
{
lock(syncRoot)
{
list.Add(i);
}
}
//...
}

--
William Stacey [MVP]

|
| | > In 1.1, IIRC, there would be no difference as list.SyncRoot was "this".
| > However, in 2.0, SyncRoot is not "this" but a seperate object. Use
| > SyncRoot
| > if you want to lock on that. However, I would just use my own syncRoot
| > object in your containing class.
| >
|
| So it doesn't matter which way I do it as long as I always do it the same
| way. Is that right?
|
|
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top