このページの目次
「Enumeratorが、thread safeで、MoveNext()が、非thread safe」
であることについて理解を深めるための資料集め。
IEnumerator, IEnumerableについてMSDNの記載 http://msdn.microsoft.com/ja-jp/library/system.collections.ienumerator%28v=vs.110%29.aspx
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
列挙子はコレクションへの排他アクセス権を持たないため、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。 コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
MonoでのIEnumerable.GetEnumeratorの実装(eg. ArrayList.cs)を読んでみても
特に排他処理をしていないので、thread safeには見えない。
2012の記事:c# - Is this collection actually thread safe? Is concurrent Iterating, querying and modifying safe? - Code Review Stack Exchange
http://codereview.stackexchange.com/questions/7276/is-this-collection-actually-thread-safe-is-concurrent-iterating-querying-and-m
C#: How can I make an IEnumerable<T> thread safe? - Stack Overflow
http://stackoverflow.com/questions/1605745/c-how-can-i-make-an-ienumerablet-thread-safe
未読: c# - Is returning an IEnumerable<> thread-safe? - Stack Overflow
http://stackoverflow.com/questions/10232631/is-returning-an-ienumerable-thread-safe
未読:Iterating atomically - Jon Skeet: Coding Blog
http://msmvps.com/blogs/jon%5Fskeet/archive/2009/10/23/iterating-atomically.aspx
(2010) Thread-safe enumeration in C#
http://www.codeproject.com/Articles/56575/Thread-safe-enumeration-in-C
解決策だけどたぶんふつう使えない
Microsoft.TeamFoundation.Framework.Serverに、TryMoveNextっていうthread safeなメソッドがあるっぽい?
TeamFoundationDataReader.TryMoveNext Method (Microsoft.TeamFoundation.Framework.Server)
http://msdn.microsoft.com/ja-jp/library/microsoft.teamfoundation.framework.server.teamfoundationdatareader.trymovenext(v=vs.100).aspx
LINQ Enumerable
http://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.110).aspx
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
この型のすべてのパブリック static (Visual Basic では Shared) メンバーは、スレッド セーフです。インスタンス メンバーの場合は、スレッド セーフであるとは限りません。
拡張メソッドは、thread safe
instance members (?)は、not thread safe