473,503 Members | 4,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi there,

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

lock(list) { .... }

and

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


Jul 14 '06 #1
5 20365
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]

"Jacob" <no****@nospamforme.comwrote in message
news:Ot**************@TK2MSFTNGP04.phx.gbl...
| Hi there,
|
| Say I have an ArrayList named list. What's the difference between using
|
| lock(list) { .... }
|
| and
|
| lock(list.SyncRoot) { .... }
|
|
|
|
Jul 14 '06 #2
"Jacob" <no****@nospamforme.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 14 '06 #3

"William Stacey [MVP]" <wi************@gmail.comwrote in message
news:ON**************@TK2MSFTNGP05.phx.gbl...
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?
Jul 14 '06 #4
"Jacob" <no****@nospamforme.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 14 '06 #5
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<intlist = new List<int>();

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

--
William Stacey [MVP]

"Jacob" <no****@nospamforme.comwrote in message
news:e9**************@TK2MSFTNGP04.phx.gbl...
|
| "William Stacey [MVP]" <wi************@gmail.comwrote in message
| news:ON**************@TK2MSFTNGP05.phx.gbl...
| 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?
|
|
Jul 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9885
by: Not Me | last post by:
Hi, I'm sure this is a common problem.. to create a single field from a whole column, where each row would be separated by a comma. I can do this for a specified table, and column.. and I've...
0
3368
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
1
4309
by: Tom Dauria | last post by:
I have a form that allows the user to select some number of People from a list. The user can select one or all of the people in the list. The list has thousands of names. Once the user makes...
14
6298
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
1
1376
by: Stu Lock | last post by:
Hi, Is there any way to databind a hashtable to a drop down list box using the key/value pair within the hashtable to correspond to the value/text of the drop down list box? ie: hashtable...
7
2667
by: situ | last post by:
Hi, we i get a snapshot for lock on db i'm getting LOCK_LIST_IN_USE =5560 and my LOCKLIST parameter = 50, is it ok for OLTP database or do i have to do any tuning here. thanks sridhar
0
1490
by: Geoffrey | last post by:
Hello, I work with .net remoting,to simplify, when the client connected, he send an handle to an object, the object is used to exchange message and events. no problem. In my server, I keep a...
16
6117
by: castironpi | last post by:
Any ideas for a thread-safe list.removeall( X ): removing all occurrences of X within list L, when L might be modified concurrently? Sincerely, Aaron
3
30768
by: Marco | last post by:
Hi all, with db2 snapshot I know the lock waits number but I can not see the tables with locks an I can not see queries that produces locks. How can I know those details? Thanks Marco...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.