By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,343 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,343 IT Pros & Developers. It's quick & easy.

Making a Collection Multithread safe

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.