0

Possible Duplicate:
Are IEnumerable Linq methods thread-safe?

quick question. Will using Where,Single,SingleorDefault on a list while other threads access it (read but also write) cause collection changes while enumerating exception?

why doesn't the c# 4.0 have concurrent List?

Community
  • 1
  • 1
Lior
  • 171
  • 2
  • 11
  • I think you are not actually asking for a concurrent list but for a list with synchronized access: [`SynchronizedCollection`](http://msdn.microsoft.com/en-us/library/ms668265.aspx) – Daniel Hilgarth Nov 23 '12 at 09:06

1 Answers1

3

List<T> is safe to read from multiple threads, but doesn't support writing at all. So you can execute multiple LINQ queries against the list so long as they don't make any changes. You could use ReaderWriterLockSlim to obtain a write lock only for times where you need to write.

why doesn't the c# 4.0 have concurrent List?

I believe it's hard to create an implementation which keeps the same guarantees as List<T> in terms of ordering, but is also thread-safe and efficient. The concurrent collections in .NET 4 are efficient, at the cost of some guarantees (such as ordering in the case of ConcurrentBag<T>).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194