473,426 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

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 20357
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
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
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
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
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
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
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
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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
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...

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.