ArrayList

  • Thread starter Thread starter Igor
  • Start date Start date
I

Igor

Hi.
I have ArrayList shared by many threads, I synchronized it with mutex. Now I
discovered in MSDN that ArrayList should be synchronized with
lock() and SyncRoot. Is it a problem that I synchronized it with mutex and
what is the difference?

Thanks.
 
Hi, Igor!

You are better to use ArrayList.Synchronized(); method it makes all work for
you.

I> Hi.
I> I have ArrayList shared by many threads, I synchronized it with mutex.
I> Now I discovered in MSDN that ArrayList should be synchronized with
I> lock() and SyncRoot. Is it a problem that I synchronized it with mutex
I> and what is the difference?

I> Thanks.


Me.
 
Igor said:
I have ArrayList shared by many threads, I synchronized it with
mutex. Now I discovered in MSDN that ArrayList should be synchronized
with lock() and SyncRoot. Is it a problem that I synchronized it with
mutex and what is the difference?

A mutex is rather "heavier", but I imagine it should still work. (It'll
certainly stop simultaneous access, but I don't know for sure whether
it'll cause a memory barrier. I'd imagine it would.)

lock(...) is much easier to work with, of course :)

See
http://www.pobox.com/~skeet/csharp/threads/waithandles.shtml for more
info.
 
Ivan G. said:
You are better to use ArrayList.Synchronized(); method it makes all work for
you.

No it doesn't. It synchronizes each individual operation. It doesn't
synchronize blocks of code, which is very often what you want to do -
for instance, if you want to iterate through the list.

Personally I'd stick with locking on SyncRoot - you'll have to do it in
some circumstances anyway, so it's easier to be consistent about it
than use Synchronized() and have to work out when that's not
sufficient.
 
Back
Top