473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Array/ArrayList/Genreic List Threadsafe

Hi,

I need a data structure such as an Array/ArrayList/Generic List to hold
multiple instances of a user derfined class.

This array will be accessed across multiple threads. However I dont want to
lock the array itself as this will cause poor performance due to the array
being accessed frequently.

If I make the User defined class threadsafe will this be enough to make
operations on the array thread safe and will this work?

Thanks
Macca
May 31 '06 #1
2 3437
The two things are unrelated; making the user-defined class thread-safe will
protect against threaded operations on the instances of the user-defined
class; making the container thread-safe will protect against threaded
operations on the container.

The real question is: what are you going to be doing with the container? If
it is populated once, and then left alone (i.e. no Add / Remove / {reassign
element}), then you are probably OK. However, if the container contents
change, then you must make the container thread-safe. Otherwise the
following can fail:

if(!container.Contains(myUser)) {
container.Add(myUser);
}

This can fail if another thread is doing something similar, and inserts
myUser after you checked (Contains) and before you added (Add). You could
get around this by locking, but then - yes - you get a pinch-point. You can
mitigate this by minimising the time holding locks, e.g. (assuming I have
wrapped the container and exposed a SyncLock):

lock(container.SyncLock) {
SomeUnrelatedMethodThatDoesntAffectContainer();
UserClass myUser = container[17];
myUser.SomeLongOperation();
}

should be written:

SomeUnrelatedMethodThatDoesntAffectContainer();
UserClass myUser;
lock(container.SyncLock) {
myUser = container[17];
}
myUser.SomeLongOperation();

Another option is to look at reader/writer locks. Some sing thier praises;
personally I like to keep it simple and just use a standard lock; as long as
you minimise the lock time you generally get plenty of performance.

Marc
May 31 '06 #2
Macca <Ma***@discussions.microsoft.com> wrote:
I need a data structure such as an Array/ArrayList/Generic List to hold
multiple instances of a user derfined class.

This array will be accessed across multiple threads. However I dont want to
lock the array itself as this will cause poor performance due to the array
being accessed frequently.
Do you have any evidence that it'll cause poor performance? Uncontested
locking is incredibly quick - it would be much simpler to use locking
(or ReaderWriterLock if you can prove that's faster - it may well not
be) than to try to avoid it.
If I make the User defined class threadsafe will this be enough to make
operations on the array thread safe and will this work?


How were you planning to make the user-defined class threadsafe without
using locking?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Dixon - Depictions.net | last post by:
Hi Everyone. Bit new to Java and I have question about arrays. I have wrote some code to look for certain substrings within a larger string which work fine. I would like to store each substring...
3
by: Jack Addington | last post by:
Quite new to C# but I am getting quite confused with Array's and ArrayLists. This is probably the same old iteration of a basic question but I can't seem to find a clear answer/example of this...
3
by: vooose | last post by:
Consider a class, ClassA that is used to perform a task using multiple threads...ie ClassA aObj = new ClassA(param1); Thread t = new Thread(new ThreadStart(aObj.DoStuff));...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.