<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Threading</title>
        <link>http://darkside.co.za/category/11.aspx</link>
        <description>Threading</description>
        <language>en-ZA</language>
        <copyright>Ryan Schreiber</copyright>
        <generator>Subtext Version 2.1.0.5</generator>
        <item>
            <title>An example of speculative execution</title>
            <link>http://www.darkside.co.za/archive/2009/11/19/example-speculative-execution.aspx</link>
            <description>&lt;p&gt;Speculative execution is a term applied to the execution of code that you may not need the results of. With the abundance of spare processing power on servers these days (and workstations for that matter), you can easily make an application be far more responsive to the user or make processing tasks on a server application quicker.&lt;/p&gt;
&lt;p&gt;A contrived example that I've come up: Consider a process that runs periodically on a server that:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Finds a list of all your customers from the SQL Server-based accounting system that have purchased an item from your online store in the past 24 hours (quick DB call) (1 sec in total) &lt;/li&gt;
    &lt;li&gt;For each customer, sum up the values of all their transactions for the past 6 months - it's a longer process because you have to fetch the data from data warehouse tables on a different SQL Server (2 sec per call) &lt;/li&gt;
    &lt;li&gt;For customers who have purchased more than ZAR100*, check if they have received a cheesy gift from the online loyalty app that you outsource to. (2 sec per call). &lt;/li&gt;
    &lt;li&gt;If they haven't received the gift yet, make another call to the online loyalty app to request that a gift be delivered (2 sec) &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Most likely you have these running sequentially; looking at the tasks that need to be performed, they all seem to be dependant on the previous step. A quick tally based on a made of set of results returned in each step:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;5 customers are returned. Total running time elapsed: &lt;em&gt;1 sec&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Sum up the values of each customer @ 2 seconds/call. Total running time elapsed: &lt;em&gt;11 sec&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Let's say only 2 make it to this step that need to be checked. Total running time elapsed: &lt;em&gt;15 sec&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;One has already received a gift, so it's only 1 call left. Total running time elapsed: &lt;em&gt;16 sec&lt;/em&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here's an example of what the code may look like: &lt;/p&gt;
&lt;div class="Code"&gt;
&lt;pre style="MARGIN: 0px"&gt;&lt;span style="COLOR: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;gt; customers = GetListOfCustomers();&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; customer &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; customers)&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;{&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;    &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (GetPurchaseTotalForSixMonths(customer) &amp;gt; 100)&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;    {&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;        &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (!HasCustomerReceivedGift(customer))&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;            RequestGiftForCustomer(customer);&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;    }&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt; Considering that in every step from the process your server application is waiting for a response from another server, it's most likely idling away. So let's run some of the tasks in parallel. We can run each call made in (2) at the same time as the call that may or may not have been made initially in (3). We can make the call to the online loyalty app, and if the result from the call in two requires the data from the call in (3), you use it. If not, too bad - you just discard the results. Your code may look something like this: &lt;/p&gt;
&lt;div class="Code"&gt;
&lt;pre style="MARGIN: 0px"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Speculative()&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;        {&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Customer&lt;/span&gt;&amp;gt; customers = GetListOfCustomers();&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; customer &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; customers)&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;            {&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; c = customer;   &lt;span style="COLOR: green"&gt;//**              &lt;/span&gt;&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; meetsPurchaseLimit=&lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; hasReceivedGift=&lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #2b91af"&gt;Fork&lt;/span&gt;.Begin()&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                    .Call(() =&amp;gt; meetsPurchaseLimit = (GetPurchaseTotalForSixMonths(c) &amp;gt; 100))&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                    .Call(() =&amp;gt; hasReceivedGift = HasCustomerReceivedGift(c))&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                    .End();&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (meetsPurchaseLimit &amp;amp;&amp;amp; !hasReceivedGift)&lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;                    RequestGiftForCustomer(customer); &lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;            } &lt;/pre&gt;
&lt;pre style="MARGIN: 0px"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt; The Fork code that you see here is the same as what I've described in the article "&lt;a title="Asynchronous fork article" target="_blank" href="http://www.darkside.co.za/archive/2009/04/24/easy-to-use-asynchronous-fork.aspx"&gt;An easy to use asynchronous fork&lt;/a&gt;". &lt;/p&gt;
&lt;p&gt;Re-doing the calculation on the elapsed time, but now using speculative execution on the call made in (3), we get: &lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;5 customers are returned. Total running time elapsed: &lt;em&gt;1 sec&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Sum up the values of each customer @ 2 seconds/call, but at the same time check if the customer received a gift. Total running time elapsed: &lt;em&gt;11 sec&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Marrying up the results from the parallel calls, make the single call for the gift. Total running time elapsed: &lt;em&gt;12 sec&lt;/em&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Back to the example: As you can see, you have a 25% saving in time. Yes, this is a contrived example with naive timing, but the general idea is what matters most. Don't feel bad about using your CPU's idle time to get work done, even if you're going to throw it away later.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;*&lt;/strong&gt; ZAR100 is equivalent to about USD500 or GBP300. Ok, it isn't so. Check &lt;a title="South African Forex" target="_blank" href="http://www.southafrica.co.za/forex/"&gt;here&lt;/a&gt; if you really want to. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;**&lt;/strong&gt; You may or may not be wondering why I have the line:  &lt;/p&gt;
&lt;div class="Code"&gt;
&lt;pre style="MARGIN: 0px"&gt;                &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; c = customer;  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt; It's to remove the warning I get from &lt;a title="Jetbrains ReSharper" target="_blank" href="http://www.jetbrains.com/resharper/index.html"&gt;ReSharper&lt;/a&gt; - it complains about a possible "access to a modified closure". Have a look at &lt;a title="Closing over the loop variable considered harmful" target="_blank" href="http://blogs.msdn.com/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx"&gt;this article&lt;/a&gt; from &lt;a title="Eric Lippert's blog" target="_blank" href="http://blogs.msdn.com/ericlippert/about.aspx"&gt;Eric Lippert&lt;/a&gt; explaining it. &lt;/p&gt;&lt;img src="http://www.darkside.co.za/aggbug/110.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Schreiber</dc:creator>
            <guid>http://www.darkside.co.za/archive/2009/11/19/example-speculative-execution.aspx</guid>
            <pubDate>Thu, 19 Nov 2009 17:53:39 GMT</pubDate>
            <comments>http://www.darkside.co.za/archive/2009/11/19/example-speculative-execution.aspx#feedback</comments>
            <wfw:commentRss>http://www.darkside.co.za/comments/commentRss/110.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An easy to use asynchronous fork</title>
            <link>http://www.darkside.co.za/archive/2009/04/24/easy-to-use-asynchronous-fork.aspx</link>
            <description>&lt;p&gt;I’ve been making use of &lt;a href="http://rogeralsing.com/2008/05/09/async-calls-fork/" target="_blank" rel="nofollow"&gt;Roger Alsing’s Async Fork&lt;/a&gt; for a few months as an alternative to the “for testing purposes only” &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3&amp;amp;displaylang=en" target="_blank" rel="nofollow"&gt;Microsoft Parallel Extensions&lt;/a&gt;. With the release of C# 4.0 on the horizon, I decided to modify the internal workings of the Fork class to make use of the parallel extensions, which would result in very few modifications to my existing code. My reason for the modifications are to take advantage of the inner working of parallels extensions. &lt;/p&gt;  &lt;p&gt;Here is the modified version of the &lt;strong&gt;Fork&lt;/strong&gt; class:&lt;/p&gt;  &lt;div class="CodeCollapse"&gt;   &lt;pre style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fork&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;    {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;gt; _Actions ;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; Fork()&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            _Actions = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        }&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Starts an async fork&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Returns a new fork instance&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fork&lt;/span&gt; Begin()&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fork&lt;/span&gt;();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        }&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name="action"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Delegate that should be executed async&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Returns self&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fork&lt;/span&gt; Call(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt; action)&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            _Actions.Add(action);&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        }&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Executes all the calls async and waits untill all of them are finished&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; End()&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;var&lt;/span&gt; taskManager = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TaskManager&lt;/span&gt;();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: #2b91af"&gt;Parallel&lt;/span&gt;.Invoke(_Actions.ToArray(), taskManager, &lt;span style="color: #2b91af"&gt;TaskCreationOptions&lt;/span&gt;.Detached);&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;        }&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;    }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And making use of the exact same example in the original article, you can use it like this:&lt;/p&gt;

&lt;div class="CodeCollapse"&gt;
  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: green"&gt;//declare the variables we want to assign&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;string&lt;/span&gt; str = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;int&lt;/span&gt; val = 0;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: green"&gt;//start a new async fork&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: green"&gt;//assign the variables inside the fork &lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: #2b91af"&gt;Fork&lt;/span&gt;.Begin()&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;                .Call(() =&amp;gt; str = CallSomeWebService(123, &lt;span style="color: #a31515"&gt;"abc"&lt;/span&gt;))&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;                .Call(() =&amp;gt; val = ExecSomeStoredProc(&lt;span style="color: #a31515"&gt;"hello"&lt;/span&gt;))&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;                .End();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: green"&gt;//the fork has finished &lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: green"&gt;//we can use the variables now&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;            &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"{0} {1}"&lt;/span&gt;, str, val);&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://www.darkside.co.za/aggbug/100.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ryan Schreiber</dc:creator>
            <guid>http://www.darkside.co.za/archive/2009/04/24/easy-to-use-asynchronous-fork.aspx</guid>
            <pubDate>Fri, 24 Apr 2009 07:15:00 GMT</pubDate>
            <comments>http://www.darkside.co.za/archive/2009/04/24/easy-to-use-asynchronous-fork.aspx#feedback</comments>
            <wfw:commentRss>http://www.darkside.co.za/comments/commentRss/100.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>