views:

96

answers:

4

I have a static List in a class that will be accessed by different threads, each one adding, reading and removing its own unique item from the list. I would like to know if I have to worry about making this variable thread safe, as even though the same List object is shared between threads, they only modify their own unique items

+8  A: 

You definitely have to implement thread-safe access either via lock or via e.g. ReaderWriterLockSlim.

Variable itself (List<>) is thread-safe if you not modifies it after first allocation (via new) but element access have to be thread-safe (since you say you adding/removing elements from list hence changing its state)

BTW, if threads modifies its own items why you want to share them? So what you have implemented? Why you organize this way? Maybe there are better advise if you show details.

Nick Martyshchenko
+1 especially for noting that removing/adding items is mentioned, meaning the state of the whole list itself is being changed.
Andrew Barber
Its a WCF Service that will be called by a couple of clients. Each client registers itself with the service by adding its details in the List. Since this a long duration (6 hours - 1 day) exchange between the client and the service, there is a possibility of loosing network connection etc. So in these cases the client can re-create a new session and get status of the old one from reading this list. This was the reason for having a static List
john
@john: why than the service details are not just local to the handling object/thread? The globally accessible list seems to be not needed.
Vlad
@Vlad, we want the client registration info to be still available even if the session ends say due to network faults, thats why its not tied to the local variable
john
@john: okay. then, any modification of the list (inserting/deleting) must be protected by the lock. If you can guarantee, that each individual value is added/removed/processed by the dedicated thread, you don't need the locking on modification (only on insert/delete to the List).
Vlad
Thanks all will do that
john
@Vlad, thank you to take care during my offline :)@john, you probably better can do your task with ConcurrentDictionary(TKey, TValue) http://bit.ly/c2tigi since you need lookup client's data in List. This way you will avoid any locks and receive great performance bonus on seeking value. ConcurrentDictionary also available for .NET 3.5 thru Rx.
Nick Martyshchenko
@Nick Thanks, that should take care of my issue
john
A: 

If your threads do not share the data in any way then why have them compete for access to the same list. This might be an acceptable usage of the thread local storage pattern. .NET offers the following mechanisms.

  • Thread.SetData and Thread.GetData
  • ThreadStaticAttribute

Or perhaps if you could abstract the threads into their own individual instances of a custom class and then use instance variables instead of a static list variable.

Brian Gideon
A: 

You only need to worry when you are changing the list (adding / removing). However if you are using the object which you have just got / added, then you need not worry (unless it needs to be used on multiple threads)

static List<ClientState> _clientStates;
static readonly Object _clientStatesLock = new Object();


/// Gets the state of a client for a given Id.
/// Returns null if no state exists.
/// This method is threadsafe.
ClientState GetState (ClientIdentifier id){

    lock (_clientStatesLock)
        return  _clientStates.FirstOrDefault (cs => cs.Id == id);
}

The same goes for adding / removing items.

Courtney de Lautour
A: 

If you are on .NET 4, use the provided Concurrent collections whenever possible rather than implementing your own.

See http://msdn.microsoft.com/en-us/library/dd997305.aspx

Hightechrider