Array/ArrayList/Genreic List Threadsafe

G

Guest

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
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

Macca said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top