469,906 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

Making a Collection Multithread safe

I need some help in trying to understand how to make myCollection (inherited
from CollectionBase) multithread safe.

Taking my implementation of the Add Sub and a readonly property Item.

Public Sub Add(ByVal aDoc As myDocument)
List.Add(aDoc)
End Sub

Can I make this multithread safe by and is this the best way to do it.

Public Sub Add(ByVal aDoc As myDocument)
Synclock myCollection.SyncRoot
List.Add(aDoc)
End Synclock
End Sub

I notice that the ArrayList Class has a Synchronized method. How would I
implement such a function for myCollection.
As for the Item property:

Default Public ReadOnly Property Item(ByVal index As Integer) As myDocument
Get
Return CType(List.Item(index), myDocument)
End Get
End Property

Am I correct in thinking that since it is readonly I do not need to make
this thread safe in order to protect myCollection?
If I do make it thread safe as per the Add Sub, then will it prevent
multiple access to the Item (myDocument) through the same instance of
myCollection? I realise that there may be many other ways of accessing the
Item.

Does using Synclock as per the Add Sub above have much affect on
performance?
Thanks
Fred
Nov 20 '05 #1
0 1127

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Grandma Wilkerson | last post: by
reply views Thread by r_obert | last post: by
9 posts views Thread by craig | last post: by
23 posts views Thread by Shalini Joshi | last post: by
6 posts views Thread by Frank Rizzo | last post: by
11 posts views Thread by Daniel Billingsley | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.