Re: Thread safety of collections
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 09/14/04
- Next message: Imran Koradia: "Re: Thread safety of collections"
- Previous message: Florida Coder: "Configuration files for Services and Class Libraries"
- In reply to: Nic: "Thread safety of collections"
- Next in thread: anonymous_at_discussions.microsoft.com: "Re: Thread safety of collections"
- Reply: anonymous_at_discussions.microsoft.com: "Re: Thread safety of collections"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Imran Koradia: "Re: Thread safety of collections"
- Previous message: Florida Coder: "Configuration files for Services and Class Libraries"
- In reply to: Nic: "Thread safety of collections"
- Next in thread: anonymous_at_discussions.microsoft.com: "Re: Thread safety of collections"
- Reply: anonymous_at_discussions.microsoft.com: "Re: Thread safety of collections"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|