Re: Thread safety of collections

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 09/14/04


Date: Tue, 14 Sep 2004 15:17:10 +0100

Nic <nic_edward@hotmail.com> wrote:
> The documentation for most collection classes state that
> public static members are thread safe, but to what extent
> can a collection be accessed safely via multiple threads
> calling non static methods if the collection is not
> changed (written) by any other thread?
>
> E.g. when using either a Hashtable or an ArrayList with
> fixed contents (not changed during the test), I seem to
> be able to safely read from them via multiple threads
> without any synchronisation. Given a collection with
> fixed contents, are multiple readers thread safe even
> though the read operations call non static methods?

I suspect that for true honest-to-goodness thread safety you should
make sure that there's a volatile write in whatever thread sets up the
collections to start with, and then a volatile read in the reading
threads, but thereafter you should be fine.

In practice, you're unlikely to have to make sure you do the volatile
write/read. I can't envisage it actually being necessary in the near
future, even though in theory it just might be. (You don't need a
volatile read every time you're going to read - just once, after the
volatile write.)

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Thread safety and atomic assignment (again)
    ... By making the internal field volatile, you have made your class data- ... Are the following code snippets thread safe and why? ... Atomicity and volatileness are two different issues: ... guarantee both visibility and atomicity; ...
    (comp.lang.java.programmer)
  • Re: Thread safety and atomic assignment (again)
    ... Are the following code snippets thread safe and why? ... What is your definition of thread safeness in the case of your ... volatile int, you only solve the so-called visibility problem but I ... Atomicity and volatileness are two different issues: ...
    (comp.lang.java.programmer)
  • Re: hashtable - thread safety clarification
    ... "Hashtable is thread safe for use by multiple reader threads and a single writing thread." ... if I am using a hashtable just like described above, multiple threads read from in but only one thread changes it, should I still be declaring it as 'volatile'? ... internal structure - it does not cover thread safety in general. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interlocked.Increment ?
    ... My understanding is that volatile word needs to be specified so compiler ... > InterLocked .Increment is not targeted to be used with volatile modifier. ... >> Interlocked.Increment increments the variable in thread safe manner. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Thread safety of collections
    ... public static members are thread safe, ... can a collection be accessed safely via multiple threads ... calling non static methods if the collection is not ...
    (microsoft.public.dotnet.framework)