开发者

Is List<T> constructor thread safe?

More specifically, is List(T)(IEnumerable(T)) thread-safe if the IEnumerable开发者_开发百科 used to initialize the list is modified during the construction of the new list?


That has nothing to do with the List constructor being thread safe, it only depends on whether the IEnumerable is thread safe.

The constructor is not thread safe, but that is not a problem in this case. The constructor is not doing anything that compromises it's thread safety, it's the thread safety of the IEnumerable that may be a problem.


This isn't really up to the list being constructed, but rather is up to the specific IEnumerable<T> being iterated. Is this thread safe? If it doesn't support concurrent iteration and edit, then expect an exception (or worse: unpredictable results). Most .NET iterators will not like this; 4.0 introduces more concurrent collections, or you can write your own.

If the list is createD successfully (iterating the source), then once constructed the origin has no impact - the two are disconnected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜