Thread Safety of C# List<T> for readers
- by ILIA BROUDNO
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?