Solved

VB.NET - Is this collection "thread safe" ?

Posted on 2008-11-01
1
1,046 Views
Last Modified: 2012-05-05
If i declare a global collection, C and add items to it from separate threads - is there any chance of a problem arising? Or is this safe to do.

And for that matter, is there a simple way to know for sure if a built-in datastructure is "thread safe" ?

what about custom data structures? Let's say I have a class that contains one single integer and I have multiple threads adjusting the integer. Is that safe?

Thanks.
0
Comment
Question by:hamlin11
1 Comment
 
LVL 19

Accepted Solution

by:
elimesika earned 500 total points
Comment Utility
HI

You can determine if a collection is thread safe by googling "msdn <collection name>" and seeing the class definition of the collection. Each such class has a Thread Safety section that fully describes if the collection supports multiple thread and if it supports it only for reader (and just one writer) threads or also supports multiple writers threads as well.
For example , this is the definition of the hastable class (taken the 1st entry of googling "msdn hastable")
http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

If the collection dos not supports thread-safety or you have implemented your own class and want it to be thread-safe.

1) You can use the [Synchronize] directive on the class methods that handles your collections.

2) you can use the lock keyword and a static object and lock only the relevant code sections.

See code sample at
http://www.c-sharpcorner.com/UploadFile/ahmedalkayali/SyncThreading11212005042506AM/SyncThreading.aspx

For other methods , see also
http://www.developerfusion.com/article/5184/multithreading-in-vbnet/2/
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now