Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

14 Experts available now in Live!

Get 1:1 Help Now