?
Solved

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

Posted on 2008-11-01
1
Medium Priority
?
1,097 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 19

Accepted Solution

by:
elimesika earned 2000 total points
ID: 22859031
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

741 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