{"id":131,"date":"2012-01-25T22:13:16","date_gmt":"2012-01-25T22:13:16","guid":{"rendered":"http:\/\/www.silverclaw.net\/?p=131"},"modified":"2012-01-25T22:13:16","modified_gmt":"2012-01-25T22:13:16","slug":"how-is-c-threading-like-peeling-potatoes","status":"publish","type":"post","link":"http:\/\/www.silverclaw.net\/?p=131","title":{"rendered":"How is C# threading like peeling potatoes?"},"content":{"rendered":"<p>I&#8217;ve been working on the Threading lessons for the C#, ASP.NET and Visual Studio Expert Skills book, and an interesting analogy struck me.<\/p>\n<p>I have a sack of potatoes that need to be peeled as quickly as possible. I also have two chefs, Jo and Bill, who are in kitchens on opposite sides of a building.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-148\" title=\"JoBillOnePotato\" src=\"http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillOnePotato.jpg\" alt=\"\" width=\"500\" height=\"100\" srcset=\"http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillOnePotato.jpg 500w, http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillOnePotato-300x60.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>In the first scenario, I deliver the potatoes to Jo and Bill one at a time, running backwards and forwards from the sack. It takes so long for me to carry each potato to the chefs that it would actually be faster to peel them myself.<\/p>\n<p>This is the same when working with C# threads. Using two threads to accomplish a lot of small tasks is actually slower than doing all of the tasks using a single thread because of the overhead that&#8217;s added by managing the threads.<\/p>\n<p>This code uses the &#8220;single potato&#8221; approach to check prime numbers up to MaximumValue:<\/p>\n<pre>while (CurrentValue &lt; MaximumValue)\r\n{\r\n\u00a0\u00a0\u00a0 if (!FirstThread.IsAlive)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FirstThread = CreateThread();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FirstThread.Start(CurrentValue);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CurrentValue++;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 if (!SecondThread.IsAlive)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SecondThread = CreateThread();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SecondThread.Start(CurrentValue);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CurrentValue++;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 Thread.Sleep(1000);\r\n}<\/pre>\n<p>This code gives each prime number to the two threads individually. The prime numbers are the &#8216;potatoes&#8217; in this instance. Because there&#8217;s so much overhead starting and stopping the threads, this code is actually much slower than it would be if it didn&#8217;t use any threading code at all!<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-149\" title=\"JoBillSolution\" src=\"http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillSolution.jpg\" alt=\"\" width=\"500\" height=\"100\" srcset=\"http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillSolution.jpg 500w, http:\/\/www.silverclaw.net\/wp-content\/uploads\/2012\/01\/JoBillSolution-300x60.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>So what&#8217;s the solution to the potato problem?\u00a0 Well of course, it doesn&#8217;t make any sense to carry the potatoes one at a time. It would be much more logical to give half of the potatoes to Bill and the other half to Jo. That way they can do their jobs and there&#8217;s no time wasted running backwards and forward from the potato sack.<\/p>\n<p>In C# terms, that means our solution is to divide up the prime numbers between our two threads and have each thread process them all in one go instead of starting a new thread for each number.<\/p>\n<p>Here&#8217;s how the code looks with the prime numbers divided up between the two threads.<\/p>\n<pre>Thread Thread1 =\r\nnew Thread(new ParameterizedThreadStart(CalculatePrimes));\r\nThread1.Start(new int[]{2,StartingPointForThread2-1});\r\nThread Thread2 =\r\nnew Thread(new ParameterizedThreadStart(CalculatePrimes));\r\nThread2.Start(new int[] { StartingPointForThread2, MaximumValue });\r\n\r\nwhile (Thread1.IsAlive || Thread2.IsAlive)\r\n{\r\n\u00a0\u00a0\u00a0 Thread.Sleep(1000);\r\n}<\/pre>\n<p>In this code, the two threads only start once and don&#8217;t need to restart for each prime number. This implementation is much faster than working with a single thread (or peeling all of the potatoes yourself!).<\/p>\n<p>There&#8217;s a lot more about threading in the Expert Skills book, including using the ThreadPool class to manage threads more easily. &#8216;Learn ASP.NET 4.0, C# and Visual Studio Expert Skills with The Smart Method&#8217; will be available for purchase around June 2012.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on the Threading lessons for the C#, ASP.NET and Visual Studio Expert Skills book, and an interesting analogy struck me. I have a sack of potatoes that need to be peeled as quickly as possible. I also &hellip; <a href=\"http:\/\/www.silverclaw.net\/?p=131\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,3,12],"tags":[],"_links":{"self":[{"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/posts\/131"}],"collection":[{"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":11,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":155,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions\/155"}],"wp:attachment":[{"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.silverclaw.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}