c# - How do I refactor this IEnumerable<T> to be thread-safe? -
i looking @ skeet's atomicenumerable i'm not sure how integrate current ienumerable exmaple below (http://msmvps.com/blogs/jon_skeet/archive/2009/10/23/iterating-atomically.aspx)
basically want foreach blahs type in thread-safe way.
thanks
so .. foreach on multiple threads ... giving correct order
blahs b = new blahs();
foreach (string s in b) { }
public sealed class blahs : ienumerable<string> { private readonly ilist<string> _data = new list<string>() { "blah1", "blah2", "blah3" }; public ienumerator<string> getenumerator() { return _data.getenumerator(); } ienumerator ienumerable.getenumerator() { return getenumerator(); } }
do mean want each thread see same sequence, or between threads, each item should seen once?
are able use .net 4? if so, parallel extensions (e.g. parallel.foreach
) friend - it's safer come :)
edit: if want iterate on list in each thread, and nothing going modifying list, thread-safe already. if do need modify list, should create copy of list , iterate on instead.
Comments
Post a Comment