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
Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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