Solved

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

Posted on 2008-11-01
1
1,071 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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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