Thread Safety of C# List<T> for readers

Posted by ILIA BROUDNO on Stack Overflow See other posts from Stack Overflow or by ILIA BROUDNO
Published on 2010-05-12T21:46:58Z Indexed on 2010/05/12 21:54 UTC
Read the original article Hit count: 273

Filed under:
|
|
|

I am planning to create the list once in a static constructor and then have multiple instances of that class read it (and enumerate through it) concurrently without doing any locking.

In this article http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx MS describes the issue of thread safety as follows:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A List can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

The "Enumerating through a collection is intrinsically not a thread-safe procedure." Statement is what worries me.

Does this mean that it is thread safe for readers only scenario, but as long as you do not use enumeration?

Or is it safe for my scenario?

© Stack Overflow or respective owner

Related posts about list

Related posts about c#