471,073 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Lock generic SortedList

Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar
Oct 3 '06 #1
4 2560
Well, unless there are some missing lines that make the list available to
another thread, you don't really need to lock it anyway... of course, by
that reasoning the list is also empty, which I'm sure it isn't ;-p

IIRC, the base foreach of a dictionary is a KeyValuePair<TKey,TValue>; you
might choose to enumerate .Values or .Keys, which will allow direct access
to the strings; otherwise, using the base foreach I would cast "item"
correctly - i.e. as a KeyValuePair<string,string>.

If the list is reasonably short, and you want to minimise lock times, and
the specific scenario permits, then you could perhaps take a spanshot of the
values as an array first (within the lock), and then (outside the lock)
enumerate the items. This assumes you aren't reassigning / adding /
removing, which you can't do inside a foreach anyway (but you can do inside
the same lock, but it isn't shown).

Marc
Oct 3 '06 #2
seems OK.. except it may keep the list locked for some extra time depending
the operation you do there..

Nirosh.

"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar

Oct 3 '06 #3
Ok! Great! Thanks a bunch!
Just wanted a second opinion since its the first time I needed to deal with
thread saftey and generic collections..

/Oscar

"Champika Nirosh" <te**@tc.comwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
seems OK.. except it may keep the list locked for some extra time
depending the operation you do there..

Nirosh.

"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
>Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar


Oct 3 '06 #4
If this is all you're doing, you don't need the lock (but you already knew
that).

That said, if you need to lock the collection this is a valid way to do it.
You can also look into Reader-Writer Locks depending on your use case.

You could also wrap your sorted list in the generic ReadOnlyCollection class
(this also implements IList), that way you could avoide the locks
altogether.

--
Chris Mullns, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
"Oscar Thornell" <no****@internet.comwrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
Hi,

Any comments regarding this implementation...

SortedList<String, Stringlist = new SortedList<String, String>();

lock (((IList)list).SyncRoot)
{
foreach (Object item in list)
{
//Do some stuff...
}
}

Regards
/Oscar

Oct 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rich Sienkiewicz | last post: by
2 posts views Thread by newscorrespondent | last post: by
2 posts views Thread by Bruce Arnold | last post: by
6 posts views Thread by Nick Valeontis | last post: by
2 posts views Thread by active | last post: by
1 post views Thread by Ethan Strauss | last post: by

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.