
{"id":1065,"date":"2012-08-22T17:18:16","date_gmt":"2012-08-22T21:18:16","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1065"},"modified":"2012-08-22T17:18:16","modified_gmt":"2012-08-22T21:18:16","slug":"plinq-and-source-ienumerable-thread-safety","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1065","title":{"rendered":"PLinq and source IEnumerable thread safety"},"content":{"rendered":"<p>As I discovered that the source IEnumerable is called by PLINQ from multiple threads, it made me thinking: how can it possibly work? E.g. when someone writes code like this:<\/p>\n<p><code>Enumerable.Range(1,1000).AsParallel()...<\/code><\/p>\n<p>the Range iterator must keep its current state in some variable. If this state is accessed from multiple threads, how are race conditions avoided? Here are my findings in order:<\/p>\n<p>1. <code>Enumerable.Range()<\/code> returns an <code>IEnumerable&lt;int&gt;<\/code>. This enumerable&#8217;s <code>GetEnumerator()<\/code> will return different enumerators for different threads. This appears to be a built-in feature of enumerables generated from <code>yield return<\/code> keyword. Reflector is my witness. <a href=\"http:\/\/startbigthinksmall.wordpress.com\/2008\/06\/09\/behind-the-scenes-of-the-c-yield-keyword\/\">Proof link<\/a>.<\/p>\n<p>Each enumerator, however, is <b>not<\/b> thread safe and does not have any locking code.<\/p>\n<p>2. <code>AsParallel()<\/code> calls <code>GetEnumerator()<\/code> only once, and then uses it from multiple threads, so the above protection does not work.<\/p>\n<p>3. Stephen Toub of Microsoft <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/parallelextensions\/thread\/6079e1cb-7136-43df-8b64-c0852c5a9974\/\">says<\/a>, that as <code>IEnumerable<\/code> is inherently thread unsafe, PLINQ has locking built in.<\/p>\n<p>4. As per Reflector it appears to be implemented in the <code>PartitionedDataSoruce&lt;T&gt;.MoveNext()<\/code> method.<\/p>\n<p>So, relax folks. Your enumerator can and will be thread unsafe, PLINQ has its own locks to take care of that.<\/p>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I discovered that the source IEnumerable is called by PLINQ from multiple threads, it made me thinking: how can it possibly work? E.g. when someone writes code like this: <a href=\"https:\/\/ikriv.com\/blog\/?p=1065\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,8,4],"tags":[],"class_list":["entry","author-ikriv","post-1065","post","type-post","status-publish","format-standard","category-dotnet","category-cs","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1065"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1065\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}