<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Generalities &amp; Details: Adventures in the High-tech Underbelly</title>
    <link>http://www.bluebytesoftware.com/blog/</link>
    <description>Joe Duffy's Weblog</description>
    <language>en-us</language>
    <copyright>Joe Duffy</copyright>
    <lastBuildDate>Mon, 01 Mar 2010 04:52:59 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.8.5223.2</generator>
    <managingEditor>joe@bluebytesoftware.com</managingEditor>
    <webMaster>joe@bluebytesoftware.com</webMaster>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=14b37ade-3110-4596-9d6e-bacdcd75baa8</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=14b37ade-3110-4596-9d6e-bacdcd75baa8</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <title>On (Haskell) Type Classes and (C#) Interfaces</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/03/01/OnHaskellTypeClassesAndCInterfaces.aspx</link>
      <pubDate>Mon, 01 Mar 2010 04:52:59 GMT</pubDate>
      <description>&lt;p&gt;
   Simon Peyton Jones was in town a couple weeks back to deliver a repeat of his ECOOP’09
   keynote, &lt;a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-retrospective/"&gt;“Classes,
   Jim, but not as we know them. Type classes in Haskell: what, why, and whither”&lt;/a&gt;,
   to a group of internal Microsoft language folks. It was a fantastic talk, and pulled
   together multiple strains of thought that I’ve been pondering lately, most notably
   the common thread amongst them.
&lt;/p&gt;
&lt;p&gt;
   In the talk, he compared polymorphism in Java-like languages (including C# which I
   will switch to referring to over Java hereforth) with ML and Haskell. In other words,
   how does a programmer commonly write code in each language that is maximally reusable?
   Of course, C# programmers primarily achieve this through subclassing, whereas functional
   programmers rely on type parameterization. Over the years, however, the former group
   has begun to borrow a great deal from the latter; as evidence, witness the growingly-pervasive
   use of generics in both Java and C# over the past decade. The talk was given mainly
   through the lens of this evolution, which appears to approach an interesting limit
   if projected far enough into the future.
&lt;/p&gt;
&lt;p&gt;
   Type classes came on the scene towards the end of the 1980’s, and immediately became
   a fertile seed for research and exploration in the relationship between subclass and
   parametric polymorphism. Type classes are much closer to subclass polymorphism than
   Haskell’s borrowed ML-style, which is to say parametric polymorphism. This is most
   intriguing because Haskell does not rely on subclassing, and so the mixture of two
   breeds new patterns.
&lt;/p&gt;
&lt;p&gt;
   I thought that it might be interesting to compare the mixture of subclass and parametric
   polymorphism in Haskell vis-à-vis type classes with the same in C# vis-à-vis a mixture
   of interfaces, generics, and generic constraints. Hence this post. We shall proceed
   by examining some basic type classes in Haskell with their equals in C#. Though similar,
   the dissimilarities are as stark as the similarities. And the lack of higher kinds
   -- particularly when combined with type classes -- means that some Haskell patterns
   simply are not expressible in C#.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;The Simple Case: Equality (or Lack Thereof)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   The most basic type class of all is Eq, which allows the comparison of two like-typed
   pieces of data. This may seem like a commodity if you ordinarily write code in languages
   like Java and C# which have a strong notion of object identity. In Haskell, however,
   equality is value equality over algebraic data types rather than objects, so polymorphism
   over equality operators is quite a bit more important. Indeed, as we shall see, Haskell’s
   approach is more powerful than == in Java-like languages. (Witness the neverending
   dichotomy between reference and value equality vis-à-vis Object.Equals in C#.) But
   alas, let us proceed by crawling in a series of logical steps, rather than leaping
   to the conclusions.
&lt;/p&gt;
&lt;p&gt;
   Haskell’s Eq type class is defined as such:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; class Eq a where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; (==), (/=) :: a -&gt; a -&gt; Bool&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
   &lt;o:p&gt;
      &lt;font color=#0f243e&gt;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x /= y = not (x == y)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x == y = not (x /= y)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   As you see, Eq provides two operators: == and /=. Default implementations of each
   define == as the inverse of /= and /= as the inverse of ==. Not only is this a convenience,
   but it also specifies the desired contract implementations ought to abide by. Other
   types may become members of the Eq class by mapping the one or both operators to type-specific
   functionality. You will immediately recognize the similarity to virtual methods in
   OOP languages, where the operators can be overridden by subclasses.
&lt;/p&gt;
&lt;p&gt;
   Of course all of the primitive data types already implement Eq, so you get value equality
   over numbers, strings, etc. Imagine we declared a new Coords type – comprised of two
   integers – and want to make it a member of Eq also – wherein equality is determined
   by a pairwise comparison of each’s members:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; data Coords = Coords { fst
   :: Integer, snd :: Integer }&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   We make Coords a member of the Eq type class, and thereby define equality over instance,
   through the ‘instance Eq Coords where’ construct. This maps type class functions to
   real implementation functions. The example here defines them inline, though you may
   of course refer to existing functions instead:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; instance Eq Coords where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; (Coords fst1 snd1) == (Coords
   fst2 snd2) = (fst1 == fs2) &amp;&amp; (snd1 == snd2)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Now we can take a ‘[Coords]’ and ask whether a particular ‘Coords’ exists within it.
&lt;/p&gt;
&lt;p&gt;
   A function may constrain a type variable to a certain class, and thereby access members
   of that class. For example, the following ‘isin’ function tests whether an instance
   of some type ‘a’ is contained within a list of type ‘[a]’. To do this, it demands
   that ‘a’ is a member of Eq using the syntax “Eq a =&gt;”:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; isin :: Eq a =&gt; a -&gt; [a]
   -&gt; Bool&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x `isin` [] = False&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x `isin` (y:ys) = x == y
   || (x `isin` ys)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   The moral equivalent to the Eq type class in C# is not so easy to decide. The most
   obvious first guess is 
   &lt;br&gt;
   the built-in == and != operators. However, we will quickly find that this is not quite
   right, because these operators are not polymorphic in C#. To illustrate this point,
   let’s try to write the ‘isin’ method in C#, using generics and the == operator, for
   example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This function will not compile. The reason is that == and != in C# are not defined
   over all types (specifically not for value types). You can get IsIn to compile by
   restricting the T to a reference type:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys) where T : class&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…
   same as above …&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Although this code is deceptively similar to the Haskell example, it is actually quite
   different. The == used to compare two instances compiles into the MSIL CEQ operator,
   effectively hard-coding an object identity comparison. Even if an overloaded == operator
   for a particular instantiated T is available, the compiler will not bind to it. Why?
   Because it is overloading and specifically *not* overriding. For example, say we had
   a MyData type and an overloaded == operator for comparing two instances:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class
   MyClass&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool operator ==(MyClass a, MyClass b) { return true; }&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool operator !=(MyClass a, MyClass b) { return false; }&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   According to this, all MyClass objects are equal. However, the following call yields
   the answer ‘false’:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;IsIn&amp;lt;MyClass&amp;gt;(new
   MyClass(), new MyClass[] { new MyClass() });&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   The same problem arises should instances of MyClass get referred to by Object references.
   == and != do not perform any kind of virtual dispatch; the selection of implementation
   is chosen statically.
&lt;/p&gt;
&lt;p&gt;
   Perhaps it is the Equals method inherited from System.Object, then? This, at least,
   is virtual. And indeed, this gets much closer to Eq. Any type may override Equals,
   and a generic definition defined in terms of it dispatches virtually and allows subclasses
   to change behavior on a type-by-type basis:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y || (x != null &amp;&amp; x.Equals(y)))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   (Even this is slightly different, because it assumes a certain type-agnostic behavior
   about nulls.)
&lt;/p&gt;
&lt;p&gt;
   This is cheating, however. We’ve taken advantage of the fact that someone thought
   to put an Equals method on System.Object, thereby giving all Ts such a method. There
   are clearly limits to how many crosscutting things can be added to System.Object before
   it becomes overwhelmed with concepts, not to mention the size (e.g. v-tables). Moreover,
   Equals on Object is weakly typed; a better solution is to use interfaces, like the
   IEquatable&amp;lt;T&amp;gt; interface that introduces a strongly typed Equals method:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IEquatable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   Equals(T other);&lt;br&gt;
   &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And to use a generic type constraint on IsIn’s T, much more akin to what ‘isin’ in
   Haskell above did:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : IEquatable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y || (x != null &amp;&amp; x.Equals(y)))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; return false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This is cheating a little less, because we can implement an interface after-the-fact
   without impacting a class’s type hierarchy. This, in fact, looks remarkably similar
   to the Haskell ‘isin’ shown earlier, using type classes and parametric polymorphism,
   where here we have used interfaces in place of type classes.
&lt;/p&gt;
&lt;p&gt;
   We might be tempted to define a default NotEquals method over all IEquatable&amp;lt;T&amp;gt;
   instances, just like Haskell does by implementing the defaults for == and /= as the
   inverse of each other:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static class Equatable&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool NotEquals&amp;lt;T&amp;gt;(this IEquatable&amp;lt;T&amp;gt; @this, IEquatable&amp;lt;T&amp;gt;
   other)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   !this.Equals(other);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This is not perfect. It is not polymorphic; see my previous post for &lt;a href="http://www.bluebytesoftware.com/blog/2010/02/10/ExtensionMethodsAsDefaultInterfaceMethodImplementations.aspx"&gt;an
   extensive discussion&lt;/a&gt; of this and related points. And what about nulls? If '@this'
   is null, the default implementation is going to AV. We’d need to bake in type-agnostic
   knowledge of null again. Sigh!
&lt;/p&gt;
&lt;p&gt;
   Sadly, it turns out this whole approach in general isn’t quite right anyway. For two
   reasons:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      First, we still infect the type in question with the interface being implemented;
      it cannot be done completely outside of the type’s definition, as with type classes.&lt;/li&gt;
   &lt;li&gt;
      Second, type classes in Haskell do not actually require a value of the type in question
      to dispatch against the class’s functions, whereas we clearly do in the above example:
      we need to virtually dispatch against the object, and rely on this virtual dispatch
      to execute different code for each type. This will come up as we look at the numeric
      classes, but it is a critical difference.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   A closer analogy is to use IEqualityComparer&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; public interface IEqualityComparer&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; bool Equals(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; }&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   (IEqualityComparer&amp;lt;T&amp;gt; in .NET also has a GetHashCode method on it. Let’s ignore
   that for now.)
&lt;/p&gt;
&lt;p&gt;
   Unfortunately, if our IsIn method were to use IEqualityComparer&amp;lt;T&amp;gt; to do its
   job, callers would be required to pass an instance explicitly; we cannot infer a “default”
   comparer based solely on the T:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys, IEqualityComparer&amp;lt;T&amp;gt; eq)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (eq.Equals(x, y))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Type classes actually function rather similarly, with two major differences:
&lt;/p&gt;
&lt;ol&gt;
   &lt;li&gt;
      The interface object – called a dictionary – is passed and used implicitly.&lt;/li&gt;
   &lt;li&gt;
      The mapping from types to dictionaries is done implicitly, whereas in .NET you’ll
      need to find an instance of the interface in question through other means.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
   This second difference is solved by a little hack in .NET. If you take a look at the
   EqualityComparer&amp;lt;T&amp;gt;.Default property, you shall see a lot of slightly gross
   reflection code to return an instance of IEqualityComparer&amp;lt;T&amp;gt; for any arbitrary
   T. The code checks some well-known types and conditions, and ultimately falls back
   to the aforementioned interfaces and default Equals method for the most general case.
   It’s not pretty, but it’s a beautiful hack given the tools at our disposal in C#.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;A Harder Case: Polymorphic Numbers, on Output Parameters&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   The Eq type class is easy. The functions it defines are polymorphic on their inputs,
   but not on their outputs; both == and /= return Bool values. Once we transition to
   polymorphic output parameters or return values, we encounter a pattern quite different
   from that which is found in most .NET interfaces.
&lt;/p&gt;
&lt;p&gt;
   Let’s illustrate these differences by looking at Haskell’s Num type class:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(Eq
   a, Show a) =&gt; Num a&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(+),
   (-), (*)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;:: a -&gt; a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;negate&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;abs,
   signum&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;:: a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fromInteger&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   Integer -&gt; a&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Here we see another feature of Haskell type classes: inheritance. Num derives from
   both Eq and Show – indicated by “(Eq a, Show a) =&gt; Num a” – the latter class of which
   we have not yet shown but is the moral equivalent to .NET’s Object.ToString method.
   It enables pretty printing of values, clearly something that would be expected to
   be common among all numeric data types. Haskell’s numeric class hierarchy is quite
   elegant, enabling highly polymorphic computations. A nice little tutorial of can be
   found here: &lt;a href="http://www.haskell.org/tutorial/numbers.html"&gt;http://www.haskell.org/tutorial/numbers.html&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   But the question at hand is what the C# equivalent would be.
&lt;/p&gt;
&lt;p&gt;
   Our first approach would be to mimic the IEquatable&amp;lt;T&amp;gt; solution above:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;interface
   INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Add(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Subtract(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Multiply(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Absolute();&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   FromInteger(int x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This works fine, and primitive types in .NET could presumably implement it:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   int : INumeric&lt;int&gt;
      &lt;INT&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   float : INumeric&lt;float&gt;
      &lt;FLOAT&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   double : INumeric&lt;double&gt;
      &lt;DOUBLE&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This enables polymorphic code, like a Sum method, through the use of generic type
   constraints:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T Sum&amp;lt;T&amp;gt;(params T[] values)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;T
   accum = default(T);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T v in values)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;accum
   = v.Add(accum);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   accum;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This example works great. Why then, you might wonder, doesn’t LINQ use this instead
   of providing special-case overloads of Average, Min, Max, Sum, etc. for all well-known
   primitive data types?
&lt;/p&gt;
&lt;p&gt;
   The primary reason is the performance hit taken to perform addition through O(N) interface
   calls versus O(N) MSIL ADD instructions. It is just a basic fact of life that today’s
   leading edge separate compilation techniques will not achieve parity with the hand
   specialized variants. While it is true that the JIT compiler *could* specialize the
   code for specific Ts and specific interfaces to emit more efficient instructions,
   like int, float, etc. over INumeric&amp;lt;T&amp;gt; calls, it will not do so today. This
   reduces the ability to share code – which admittedly is what we want here – and is
   tangled up in a judgment call based on heuristics. But I digress.
&lt;/p&gt;
&lt;p&gt;
   There is a larger problem that arises with other examples, at least from a language
   expressiveness point-of-view: the need to have an instance in hand to invoke interface
   methods. FromInteger, for example, is rather awkward to write. In fact, we cannot
   write a method with INumeric&amp;lt;T&amp;gt; like we could in Haskell:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T MakeT&amp;lt;T&amp;gt;(int value)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…
   ? …&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   How do we invoke FromInteger, given that no T is available at the time of MakeT’s
   invocation? You can’t; you need to arrange for an instance to be available. There
   are ways out of this corner. One solution is to mandate that T has a default constructor:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T MakeT&amp;lt;T&amp;gt;(int value)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;, new()&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   new T(value).FromInteger(value);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   That is always acceptable for structs, since they always have such a constructor;
   but this practice requires that classes be designed to possibly not hold invariants
   at all times, and so is not always acceptable or at the very least requires design
   accommodation.
&lt;/p&gt;
&lt;p&gt;
   The alternative is probably obvious. Use a similar approach to IEqualityComparer&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;interface
   INumericProvider&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Add(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Subtract(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Multiply(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Absolute(T x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   FromInteger(int x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And now, of course, each method that does polymorphic number crunching must accept
   an instance of INumericProvider&amp;lt;T&amp;gt;. That’s particularly cumbersome, so it’s
   more likely that .NET developers would prefer the aforementioned approach, where the
   type must provide a default constructor.
&lt;/p&gt;
&lt;p&gt;
   Admittedly, I seldom run into this particular problem in practice; but when I do,
   I really wish I had something like Haskell type classes to help me out.
&lt;/p&gt;
&lt;p&gt;
   Before moving on, it is worth pointing out one Haskell type class problem that explicit
   interface object passing in .NET helps to avoid. Should you need multiple implementations
   of a given class for the same type, as is relatively common with equality comparisons,
   you must disambiguate in Haskell by separation by module and being careful about what
   you import. This is similar to C#’s extension methods. With explicitly passed interface
   objects, however, it is trivial to manage and pass separate objects if you’d like.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Close, but No Cigar: Higher Kinds&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   There is one last feature that Haskell provides – a pretty big one, I might add –
   that C# simply cannot do: higher kinded types, or polymorphism over constructed types.
   This feature is orthogonal to type classes, but gets used pervasively in conjunction
   with them. An example will make this stunningly clear:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;Monad
   m&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(&gt;&gt;=)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   m a -&gt; (a -&gt; m b) -&gt; m b&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(&gt;&gt;)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   m a -&gt; m b -&gt; m b&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   a -&gt; m a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fail&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   String -&gt; m a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#0f243e&gt;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;m
   &gt;&gt; k&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;=&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;m
   &gt;&gt;= \_ -&gt; k&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fail
   s&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;= error s&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   Let’s try to transcribe the core of this class in C#, renaming &gt;&gt;= to Bind, and omitting
   the &gt;&gt; and ‘fail s’ operators because they have default implementations:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IMonad&amp;lt;M, A&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;A, M&amp;lt;B&amp;gt;&gt;M&amp;lt;B&amp;gt;
   Bind&amp;lt;B&amp;gt;(M&amp;lt;A&amp;gt; m, Func k);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;M&amp;lt;A&amp;gt;
   Return(A a);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;M&amp;lt;A&amp;gt;
   Fail(string s);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This approach is tantalizingly close. It suffers from the already-admitted problem
   that, for any M&amp;lt;A&amp;gt; instance, you will need to pass the appropriate IMonad&amp;lt;A&amp;gt;
   provider object – just as with the IEqualityComparer&amp;lt;T&amp;gt; and INumericProvider&amp;lt;T&amp;gt;
   examples above.
&lt;/p&gt;
&lt;p&gt;
   But the code of course won’t *actually* compile, because the type variable M cannot
   be constructed as shown here. We find references to M&amp;lt;A&amp;gt; and M&amp;lt;B&amp;gt;, which
   are complete nonsense to C#. M is just a plain type variable. M is required to be
   what Haskell calls a type constructor (* -&gt; *), which is a generic type that must
   be instantiated before it is a terminal type. &lt;a href="http://www.bluebytesoftware.com/blog/2008/11/04/LongingForHigherkindedC.aspx"&gt;I’ve
   written about this before&lt;/a&gt;. Although it seems like a trivial omission in C#’s language
   definition, it strikes at the heart of the type system.
&lt;/p&gt;
&lt;p&gt;
   A fictitious syntax for expressing this in C# might be:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IMonad&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;&gt;&lt;/FONT&gt;where
   M : 
   &lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; ...&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And if, say, M were expected to be a two- or three-parametered type, we would find,
   respectively:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; ... &lt;/span&gt;&lt;,&gt;where
   M : &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; ... &lt;/span&gt;&lt;,,&gt;where
   M : &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And so on.
&lt;/p&gt;
&lt;p&gt;
   This could in theory work. But C# -- and more worrisome .NET and the CLR – do not
   support this presently, and, to be quite honest, likely never will. It is immensely
   powerful, however. Life without monads is a life destined to continuous repetition.
   The “LINQ Pattern”, for example, is one example case in .NET where, for each ‘source’
   type, we must create a “copy” of the original System.Linq.Enumerable variant. And
   shame on those who wish to write polymorphic code that will work for any LINQ provider.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Winding Down&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Let’s wind down. I need to go grab dinner at Mama's Fish House on Maui right now.
&lt;/p&gt;
&lt;p&gt;
   I hope to have shown some of the similarities and dissimilarities between type classes
   and interfaces, and some patterns that arise when these things are mixed with parametric
   polymorphism. The mix of inheritance for type classes, but not for implementation
   types, in Haskell is unique. C#, of course, allows inheritance both amongst interfaces
   and implementations which is both a blessing and a curse.
&lt;/p&gt;
&lt;p&gt;
   I do think both camps have something to teach one another. For example, having a default
   interface lookup mechanism for arbitrary types in C# would be wonderful, and indeed
   might provide a replacement for extension methods that has more longevity. I’m sure
   much of this will happen with time; either “in place” as the respective languages
   evolve, or as new languages are created with time. 
&lt;/p&gt;
&lt;p&gt;
   But most importantly, I hope that the blog post was educational and fun. Enjoy.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=14b37ade-3110-4596-9d6e-bacdcd75baa8" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=b9072376-beaf-4d17-a6c4-4bfccfbd34b0</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=b9072376-beaf-4d17-a6c4-4bfccfbd34b0</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>Extension methods as default interface method implementations</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/02/10/ExtensionMethodsAsDefaultInterfaceMethodImplementations.aspx</link>
      <pubDate>Wed, 10 Feb 2010 02:21:49 GMT</pubDate>
      <description>&lt;p&gt;
   One of my comments in the 2nd edition of the .NET Framework design guidelines (on
   page 164) was that you can use extension methods as a way of getting default implementations
   for interface methods.&amp;nbsp; We've actually begun using these techniques here on my
   team.&amp;nbsp; To illustrate this trick, let's rewind the clock and imagine we were designing
   new collections APIs from day one.
&lt;/p&gt;
&lt;p&gt;
   Let's say we gave the core interfaces the most general methods possible.&amp;nbsp; These
   may neither be the most user friendly overloads nor the ones that most people use
   all the time.&amp;nbsp; They would, however, be those from which all the other convenience
   methods could be implemented.&amp;nbsp; An INewList&amp;lt;T&amp;gt; interface that was designed
   with these principles in mind may look like this:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;public
   interface INewList&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   Count { get; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;T
   this[int index] { get; set; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   InsertAt(int index, T item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   RemoveAt(int index);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This interface is missing all the nice convenience methods you will find on .NET's
   IList&amp;lt;T&amp;gt;, like Add, Clear, Contains, CopyTo, IndexOf, and Remove.&amp;nbsp; So it's
   not really as nice to use.&amp;nbsp; You can't write an API that takes in an INewList&amp;lt;T&amp;gt;
   and performs an Add against it, for example, like you can with IList&amp;lt;T&amp;gt;.
&lt;/p&gt;
&lt;p&gt;
   One approach to solving this might be to write a concrete class -- much like .NET's
   System.Collections.ObjectModel.Collection&amp;lt;T&amp;gt; -- that provides concrete implementations
   of all of these methods, and then other lists can simply subclass that.&amp;nbsp; But
   we can do better.
&lt;/p&gt;
&lt;p&gt;
   Instead, let's give INewList&amp;lt;T&amp;gt; default implementations of all of these methods.&amp;nbsp;
   How do we do this?&amp;nbsp; That's right: with extension methods.&amp;nbsp; Voila!
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   static class NewListExtensions&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Add&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.InsertAt(lst.Count,
   item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Clear&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = lst.Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static bool Contains&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   lst.IndexOf(item) != -1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void CopyTo&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T[] array, int arrayIndex)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; lst.Count; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;array[arrayIndex
   + i] = lst[i];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static int IndexOf&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   eq = EqualityComparer&amp;lt;T&amp;gt;.Default;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; lst.Count; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (eq.Equals(item, lst[i])) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   i;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   -1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static bool Remove&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   index = lst.IndexOf(item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (index == -1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(index);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   true;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Well isn't that neat.&amp;nbsp; We've now given any INewList&amp;lt;T&amp;gt; implementations
   all these common methods without dirtying their class hierarchies, built atop a tiny
   core of&amp;nbsp;extensibility.&amp;nbsp; This is much like .NET's Collection&amp;lt;T&amp;gt; which
   exposes the core as abstract methods.&amp;nbsp; Indeed, we can go even further.&amp;nbsp;
   Any convenience overloads, like the multitude of CopyTos on List&amp;lt;T&amp;gt; in .NET,
   can be given to all INewList&amp;lt;T&amp;gt;'s also.&amp;nbsp; And yet implementing INewList&amp;lt;T&amp;gt;
   remains as braindead simple as it was before: two properties and two methods.&amp;nbsp;
   In fact, it's simpler than doing a more feature-rich IList&amp;lt;T&amp;gt;, because the convenience
   methods come for free.
&lt;/p&gt;
&lt;p&gt;
   It would be even niftier if you could add these methods straight onto INewList&amp;lt;T&amp;gt;,
   and have the C# compiler emit the extension methods silently for you.&amp;nbsp; In other
   words:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   interface INewList&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   interface methods (as above) ...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Add(T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;InsertAt(Count,
   item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Clear()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   and so on ...&lt;br&gt;
   &lt;/font&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;font color=#000000&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/span&gt;Although
   this would just be sugar for the&amp;nbsp;NewListExtensions class shown earlier, it sure
   saves some typing and makes it the pattern more apparent and first class.
&lt;/p&gt;
&lt;p&gt;
   Though cool, this whole idea is certainly not perfect.
&lt;/p&gt;
&lt;p&gt;
   For one, there are no extension properties.&amp;nbsp; So you can't use this trick for
   properties.
&lt;/p&gt;
&lt;p&gt;
   But the more obvious and severe downside to this approach that these methods are not
   specialized for the given concrete type.&amp;nbsp; For example, the Clear method is potentially
   far less efficient than a hand-rolled List&amp;lt;T&amp;gt;, because it does O(N) RemoveAts
   rather than a single O(1) fixup of the count.
&lt;/p&gt;
&lt;p&gt;
   Recall now that the compiler binds more tightly to instance methods than extension
   methods.&amp;nbsp; So we could implement our own little list class with a faster Clear
   method if we'd like:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;class
   MyList : INewList&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   the two properties and two methods from INewList&amp;lt;T&amp;gt; ...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Clear()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;..
   efficient! ...&lt;br&gt;
   &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Now when someone calls Clear on a MyList&amp;lt;T&amp;gt; directly, the compiler will bind
   to the efficient Clear.
&lt;/p&gt;
&lt;p&gt;
   This is still not perfect.&amp;nbsp; If you pass the MyList&amp;lt;T&amp;gt; to an API that takes
   in an INewList&amp;lt;T&amp;gt;, any calls to Clear will fall back to the extension method.&amp;nbsp;
   Extension methods are not virtual in any way.&amp;nbsp; You can try to simulate virtual
   dispatch, but it gets messy quick.&amp;nbsp; For example, say we defined an IFasterList&amp;lt;T&amp;gt;
   that includes all those convenience methods that lists frequently want to make faster;
   we can then do a typecheck plus virtual dispatch in the extension method.
&lt;/p&gt;
&lt;p&gt;
   For now, let's pretend that's just the Clear method:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   interface IFasterList&amp;lt;T&amp;gt; : INewList&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Clear();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Of course, MyList&amp;lt;T&amp;gt; above would now implement IFasterList&amp;lt;T&amp;gt;.&amp;nbsp; Invocations
   through IFasterList&amp;lt;T&amp;gt; will automatically bind to the faster variant; but if
   objects that implement IFasterList&amp;lt;T&amp;gt; get passed around as IList&amp;lt;T&amp;gt;s,
   you lose this ability.&amp;nbsp; So the Clear extension method can now do a typecheck:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Clear&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;IFasterList&amp;lt;T&amp;gt;
   fstLst = lst as IFasterList&amp;lt;T&amp;gt;;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (fstLst != null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;fstLst.Clear();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = lst.Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This works but is obviously a tedious and hard-to-maintain solution.&amp;nbsp; It would
   be neat if someday C# figured out a way to "magically" reconcile virtual dispatch
   and extension methods.&amp;nbsp; I don't know if there is a clever solution out there.&amp;nbsp;
   I am skeptical.&amp;nbsp; Nevertheless, despite this flaw, the above techniques are certainly
   thought provoking and interesting enough to play around with and consider for your
   own projects.&amp;nbsp; And at the very least, it's fun.&amp;nbsp; Enjoy.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=b9072376-beaf-4d17-a6c4-4bfccfbd34b0" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=53e892af-d387-4283-9b10-a463f8ac3eba</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=53e892af-d387-4283-9b10-a463f8ac3eba</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Sometimes you need to wait for something before proceeding with a computation.
   </p>
        <p>
      Perhaps you need to know the value of some integer that is being computed concurrently. 
      Maybe you need to wait for the bytes to flush to disk before telling another process
      the file is consistent and ready to read.  Or you need to get that next row back
      from the database before painting it on the UI.  It could be that you need to
      wait for the missile to leave the bay before closing the bay door.  And so on.
   </p>
        <p>
      And sometimes there’s simply nothing better to do while waiting for these things to
      happen other than to let the CPU halt (or let other processes on the machine run). 
      You need to twiddle your thumbs a bit, and exhibit a little patience.  Or at
      least your program does.  This is simply an unfortunate fact of life.
   </p>
        <p>
      This manifests numerous ways in our programming models:
   </p>
        <p>
              1) Waiting on an event.<br />
              2) Waiting to acquire an already-held
      lock.<br />
              3) Finding that the GUI message queue
      is empty and doing a MsgWaitForMultipleObjectsEx.<br />
              4) Finding that the COM RPC queue
      is empty and doing a CoWaitForMultipleHandles.<br />
              5) Issuing an Ada rendezvous ‘accept’
      and finding that no messages await you, thus blocking.<br />
              6) Issuing an Erlang ‘receive’ and
      finding that no messages await you, thus blocking.<br />
              7) Waiting on a .NET 4.0 task.<br />
              8) Issuing a ContinueWith on a .NET
      4.0 task.<br />
              9) And so on.
   </p>
        <p>
      There are three big distinctions to make about the characteristic nature
      of this waiting: namely, (1) what condition's establishment is being sought --
      i.e. the reason for the wait, (2) whether multiple such conditions of interest
      may be waited on simultaneously, and, related, (3) whether waiting for said condition(s)
      necessarily means that the processing of some other conditions that may arise elsewhere,
      but require the blocked context to run, cannot occur.
   </p>
        <p>
      I will be the first to admit that this statement is rather abstract.  But it
      really does matter.
   </p>
        <p>
      For example, MsgWaitForMultipleObjectsEx is a pumping wait.  Not only do you
      wait for the occurrence of one of several events to get set, but the arrival
      of a new top-level message at the message queue (either GUI or COM RPC-related) causes
      immediate processing of that message, presuming the thread is blocked at that call
      at the time.  Although you can be deeply nested in some complicated code, you
      “jump” to the event loop to run the message handling code.  Vanilla WaitForMultipleObjectsEx
      works in a similar way vis-à-vis APCs, provided the wait is alertable.  This
      is quite different from a fully blocking non-pumping wait, which only waits for one
      or more very specific events, but does not dispatch messages simultaneously.
   </p>
        <p>
      Win32 esoterica aside, the concepts appear elsewhere.  The moral equivalent
      in Ada or Erlang is to do a selective-accept or -receive, intentionally not dispatching
      certain messages that might arrive in the meantime.  (To be fair, you can also
      do this in COM with message filters.)  This often happens when you nest accepts
      and receives.  You may be capable of processing messages A-Z at the top-level
      tail recursive loop; but if that nested accept only knows about message kinds M and
      N, then there are 24 other kinds that will not be picked up in the meantime.
   </p>
        <p>
      Not pumping for messages is dangerous.  And it can lead to deadlock if you pump
      for the wrong ones.  Like if you’re accepting M or N, yet the triggering of M
      or N depends on first processing some message K waiting in the queue.  COM RPCs
      with cycles run face first into this.  And/or not pumping can lead to responsiveness
      and scalability problems.  Perhaps M or N eventually does arrive, yet little
      old K needs to wait an indeterminate amount of time before it is seen.  Whereas
      we could have overlapped its processing.  This is why most STAs pump while waiting,
      and, similarly, why many Erlang processes consist of a main loop that is prepared
      to handle any message the process accepts at that top level loop.  They may seem
      very different but they are strikingly not.
   </p>
        <p>
      Yet paradoxically pumping for messages is also dangerous.  You must predict all
      the kinds of messages that may reentrantly get executed, and your state at the point
      of the blocking call must be consistent enough to tolerate them.  (At least those
      that will actually happen.)  In COM STAs, this can be wholly unpredictable and
      indeed because the CLR auto-pumps on STAs the blocking points can be hidden. 
      Overly aggressively admitting messages may seem like the right thing to do, until
      you’ve wedged yourself into some unforeseen inconsistent state.  You can avoid
      this by making each message handler atomic; see Argus.  But if you can't or don't
      have the discipline to do that, or aren't quite sure, you must not pump.  You
      either avoid pumping altogether or you selectively pump messages that do not
      touch the state encapsulated by the pump.  Or you lock access to state with a
      non-recursive lock and run the risk of deadlock.
   </p>
        <p>
      I have found it clarifying to think about blocking in event loop concurrency
      and state machine terms, advancing from one state to the next in between waits. 
      It’s a slippery model, but particularly when working in message passing systems that
      employ event loops, it can help to identify all the familiar problems with shared
      memory, blocking, and consistency.
   </p>
        <p>
      Indeed it is interesting how blocking and non-blocking systems can rapidly approach
      each other.  Starting from either extreme tempts you to tiptoe closer and closer
      to the middle.  The familiarity of the other extreme tempts you.  Until,
      alas, you just might meet in the middle.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=53e892af-d387-4283-9b10-a463f8ac3eba" />
      </body>
      <title>Musing on messages and blocking</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/01/08/MusingOnMessagesAndBlocking.aspx</link>
      <pubDate>Fri, 08 Jan 2010 08:11:39 GMT</pubDate>
      <description>&lt;p&gt;
   Sometimes you need to wait for something before proceeding with a computation.
&lt;/p&gt;
&lt;p&gt;
   Perhaps you need to know the value of some integer that is being computed concurrently.&amp;nbsp;
   Maybe you need to wait for the bytes to flush to disk before telling another process
   the file is consistent and ready to read.&amp;nbsp; Or you need to get that next row back
   from the database before painting it on the UI.&amp;nbsp; It could be that you need to
   wait for the missile to leave the bay before closing the bay door.&amp;nbsp; And so on.
&lt;/p&gt;
&lt;p&gt;
   And sometimes there’s simply nothing better to do while waiting for these things to
   happen other than to let the CPU halt (or let other processes on the machine run).&amp;nbsp;
   You need to twiddle your thumbs a bit, and exhibit a little patience.&amp;nbsp; Or at
   least your program does.&amp;nbsp; This is simply an unfortunate fact of life.
&lt;/p&gt;
&lt;p&gt;
   This manifests numerous&amp;nbsp;ways in our programming models:
&lt;/p&gt;
&lt;p&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1)&amp;nbsp;Waiting on an event.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2)&amp;nbsp;Waiting to acquire an already-held
   lock.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3)&amp;nbsp;Finding that the GUI message queue
   is empty and doing a MsgWaitForMultipleObjectsEx.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4)&amp;nbsp;Finding that the COM RPC queue
   is empty and doing a CoWaitForMultipleHandles.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5)&amp;nbsp;Issuing an Ada rendezvous ‘accept’
   and finding that no messages await you, thus blocking.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6)&amp;nbsp;Issuing an Erlang ‘receive’ and
   finding that no messages await you, thus blocking.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7)&amp;nbsp;Waiting on a .NET 4.0 task.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8)&amp;nbsp;Issuing a ContinueWith on a .NET
   4.0 task.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9)&amp;nbsp;And so on.
&lt;/p&gt;
&lt;p&gt;
   There&amp;nbsp;are three big&amp;nbsp;distinctions to make about the characteristic nature
   of this waiting: namely, (1) what condition's establishment&amp;nbsp;is being sought --
   i.e. the reason for the wait, (2) whether&amp;nbsp;multiple such conditions of interest
   may be waited on simultaneously, and, related, (3) whether waiting for said condition(s)
   necessarily means that the processing of some other conditions that may arise elsewhere,
   but require the blocked context to run,&amp;nbsp;cannot occur.
&lt;/p&gt;
&lt;p&gt;
   I will be the first to admit that this statement is rather abstract.&amp;nbsp; But it
   really does&amp;nbsp;matter.
&lt;/p&gt;
&lt;p&gt;
   For example, MsgWaitForMultipleObjectsEx is a pumping wait.&amp;nbsp; Not only do you
   wait for the occurrence of one of several events to get set, but&amp;nbsp;the arrival
   of a new top-level message at the message queue (either GUI or COM RPC-related) causes
   immediate processing of that message, presuming the thread is blocked at that call
   at the time.&amp;nbsp; Although you can be deeply nested in some complicated code, you
   “jump” to the event loop to run the message handling code.&amp;nbsp; Vanilla WaitForMultipleObjectsEx
   works in a similar way vis-à-vis APCs, provided the wait is alertable.&amp;nbsp; This
   is quite different from a fully blocking non-pumping wait, which only waits for one
   or more very specific events, but does not dispatch messages simultaneously.
&lt;/p&gt;
&lt;p&gt;
   Win32 esoterica aside, the concepts appear elsewhere.&amp;nbsp;&amp;nbsp;The moral equivalent
   in Ada or Erlang is to do a selective-accept or -receive, intentionally not dispatching
   certain messages that might arrive in the meantime.&amp;nbsp; (To be fair, you can also
   do this in COM with message filters.)&amp;nbsp; This often happens when you nest accepts
   and receives.&amp;nbsp; You may be capable of processing messages A-Z at the top-level
   tail recursive loop; but if that nested accept only knows about message kinds M and
   N, then there are 24 other kinds that will not be picked up in the meantime.
&lt;/p&gt;
&lt;p&gt;
   Not pumping for messages is dangerous.&amp;nbsp; And it can lead to deadlock if you pump
   for the wrong ones.&amp;nbsp; Like if you’re accepting M or N, yet the triggering of M
   or N depends on first processing some message K waiting in the queue.&amp;nbsp; COM RPCs
   with cycles run face first into this.&amp;nbsp; And/or not pumping can lead to responsiveness
   and scalability problems.&amp;nbsp; Perhaps M or N eventually does arrive, yet little
   old K needs to wait an indeterminate amount of time before it is seen.&amp;nbsp; Whereas
   we could have overlapped its processing.&amp;nbsp; This is why most STAs pump while waiting,
   and, similarly, why many Erlang processes consist of a main loop that is prepared
   to handle any message the process accepts at that top level loop.&amp;nbsp; They may seem
   very different but they are strikingly not.
&lt;/p&gt;
&lt;p&gt;
   Yet paradoxically pumping for messages is also dangerous.&amp;nbsp; You must predict all
   the kinds of messages that may reentrantly get executed, and your state at the point
   of the blocking call must be consistent enough to tolerate them.&amp;nbsp; (At least those
   that will actually happen.)&amp;nbsp; In COM STAs, this can be wholly unpredictable and
   indeed because the CLR auto-pumps on STAs the blocking points can be hidden.&amp;nbsp;
   Overly aggressively admitting messages may seem like the right thing to do, until
   you’ve wedged yourself into some unforeseen inconsistent state.&amp;nbsp; You can avoid
   this by making each message handler atomic; see Argus.&amp;nbsp; But if you can't or don't
   have the discipline to do that, or aren't quite sure, you must not pump.&amp;nbsp; You
   either&amp;nbsp;avoid pumping altogether or you selectively pump messages that do not
   touch the state encapsulated by the pump.&amp;nbsp; Or you lock access to state with a
   non-recursive lock and run the risk of deadlock.
&lt;/p&gt;
&lt;p&gt;
   I have found it clarifying to think&amp;nbsp;about blocking&amp;nbsp;in&amp;nbsp;event loop concurrency
   and state machine terms, advancing from one state to the next in between waits.&amp;nbsp;
   It’s a slippery model, but particularly when working in message passing systems that
   employ event loops, it can help to identify all the familiar problems with shared
   memory, blocking, and consistency.
&lt;/p&gt;
&lt;p&gt;
   Indeed it is interesting how blocking and non-blocking systems can rapidly approach
   each other.&amp;nbsp; Starting from either extreme tempts you to tiptoe closer and closer
   to the middle.&amp;nbsp; The familiarity of the other extreme tempts you.&amp;nbsp; Until,
   alas, you just might meet in the middle.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=53e892af-d387-4283-9b10-a463f8ac3eba" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=4cfad7df-ed84-46a4-a961-54aafdaeb9d7</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4cfad7df-ed84-46a4-a961-54aafdaeb9d7</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>A (brief) retrospective on transactional memory</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/01/03/ABriefRetrospectiveOnTransactionalMemory.aspx</link>
      <pubDate>Sun, 03 Jan 2010 19:05:12 GMT</pubDate>
      <description>&lt;p&gt;
   Rewind the clock to mid-2004.&amp;nbsp; Around this time awareness about the looming “concurrency
   sea change” was rapidly growing industry-wide, and indeed within Microsoft an increasing
   number of people – myself included – began to focus on how the .NET Framework, CLR,
   and Visual C++ environments could better accommodate first class concurrent programming.&amp;nbsp;
   Of course, our colleagues in MSR and researchers in the industry more generally had
   many years’ head start on us, in some cases dating back 3+decades.&amp;nbsp; It is safe
   to say that there was no shortage of prior art to understand and learn from.
&lt;/p&gt;
&lt;p&gt;
   One piece of prior art was particularly influential on our thoughts: software transactional
   memory.&amp;nbsp; (STM, or, in short just TM.)&amp;nbsp; In fact, right around that time,
   Tim Harris’s TM work grew in notoriety (my first exposure arriving by way of OOPSLA’03’s
   proceedings, which contained &lt;a href="http://research.microsoft.com/en-us/um/people/tharris/papers/2003-oopsla.pdf"&gt;the
   “Language Support for Lightweight Transactions” paper&lt;/a&gt;).&amp;nbsp; TM was immediately
   fascinating, and simultaneously promising.&amp;nbsp; For a number of reasons:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      TM hid sophisticated synchronization mechanisms under a simple veil. 
   &lt;li&gt;
      It could be implemented using sophisticated (and scalable) techniques, again under
      a simple veil. 
   &lt;li&gt;
      It built on decades of experience in building scalable and parallel transactional
      databases. 
   &lt;li&gt;
      Among others.&amp;nbsp; But most of all, it was a bright shiny light in a sea of complexity. 
   &lt;li&gt;
      And how fortunate: Tim was a colleague in our neighboring MSR Cambridge offices (and
      still is).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   In a nutshell, TM offered declarative concurrency-safety.&amp;nbsp; You declare what you’d
   like in as few simple words as possible, and you get what you want.&amp;nbsp; In this
   case, those simple words are ‘atomic { S; }’.
&lt;/p&gt;
&lt;p&gt;
   Many people latched onto TM rapidly and simultaneously, both inside and outside of
   Microsoft.&amp;nbsp; I hacked together a little prototype built atop SSCLI (“Rotor”),
   and another architect on our team built an even more feature-rich prototype using
   MSIL rewriting.&amp;nbsp; We compared notes, began jointly exploring the design space,
   and talking more regularly with other colleagues like Tim in MSR.&amp;nbsp; Soon thereafter
   we kicked off a small working group with about a dozen architects and researchers
   from around the company, aiming to articulate what a real productized TM might look
   like.&amp;nbsp; Fun times.
&lt;/p&gt;
&lt;p&gt;
   We were eventually given the OK for an official “incubation” project, and multiple
   years’ of exploration and hard work ensued.&amp;nbsp; In fact, the fruits of a team of
   many’s labor recently got released in the form of &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee334183.aspx"&gt;a
   Community Technology Preview&lt;/a&gt; -- a good conduit for experimentation, but with no
   commitment to add it to any of Microsoft’s products.&amp;nbsp; To be clear, I had only
   a small part to play in this ambitious project, and mostly towards the start.&amp;nbsp;
   Partway through, I stepped away to do &lt;a href="http://msdn.microsoft.com/concurrency/"&gt;PLINQ
   and Parallel Extensions to .NET&lt;/a&gt;, both of which are now part of the .NET Framework
   4.0.&amp;nbsp; Dozens of amazing people played a significant role in the project over
   the years.&amp;nbsp; But I am getting way ahead of myself…
&lt;/p&gt;
&lt;p&gt;
   I’ve been away from the nitty-gritty day-to-day details of TM for about 3 years now,
   which feels sufficiently long to develop a healthy perspective on the project.&amp;nbsp;
   So here it is.&amp;nbsp; What follows is of course in no way Microsoft’s “official position”
   on the technology, but rather my own personal one.&amp;nbsp; I’ve interspersed generalizations
   with specific details because that’s just how my brain thinks about TM.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Towards the North Star&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   A wondrous property of concurrent programming is the sheer number and diversity of
   programming models developed over the years.&amp;nbsp; Actors, message-passing, data parallel,
   auto-vectorization, ...; the titles roll off the tongue, and yet none dominates and
   pervades.&amp;nbsp; In fact, concurrent programming is a multi-dimensional space with
   a vast number of worthy points along its many axes.
&lt;/p&gt;
&lt;p&gt;
   This rich history is simultaneously a blessing and a daunting curse.&amp;nbsp; But in
   any case can make for some very interesting multi-year-long immersion.&amp;nbsp; &lt;a href="http://norfolk.cs.washington.edu/htbin-post/unrestricted/colloq/details.cgi?id=768"&gt;My
   UW talk&lt;/a&gt; from 1 1/2 years ago just barely touches on the sheer breadth.
&lt;/p&gt;
&lt;p&gt;
   TM’s greatest virtue is the first word in its name: transactional.&amp;nbsp; It turns
   out that, no matter your concurrent programming model du jour, three fundamental concepts
   crop up again and again: isolation (of state), atomicity (of state transitions), and
   consistency (of those atomic transitions).&amp;nbsp; We use locks in shared-memory programming,
   coarse grained messages in message-passing, and functional programming to achieve
   all of these things in different ways.&amp;nbsp; Transactions are another such mechanism,
   sure, but more than that, transactions are an all-encompassing way of thinking about
   how programs behave at their most fundamental core.&amp;nbsp; Transaction is a religion.
&lt;/p&gt;
&lt;p&gt;
   Not everybody believes this, and of course why would they: it is an immensely subjective
   and qualitative statement.&amp;nbsp; Some will claim that models like message passing
   entirely avoid the likes of “race conditions,” and such, but this is clearly false:
   state transitions are made, complicated state invariants are erected amongst a sea
   of smaller isolated states, and care must be taken, just as in shared memory.&amp;nbsp;
   Even Argus, a beautiful early incarnation of message-passing (via promises) demands
   that messages are atomic in nature.&amp;nbsp; This property is not checked and, if done
   improperly, leads to “races in the large.”&amp;nbsp; Even Argus introduced the notion
   of transactions and persistence in the form of guardians.
&lt;/p&gt;
&lt;p&gt;
   Of course, message passing helps push you in the right direction.&amp;nbsp; It is not,
   however, a panacea.
&lt;/p&gt;
&lt;p&gt;
   I was reading my ICFP proceedings recently and was reminded of research done in the
   context of Erlang that &lt;a href="http://portal.acm.org/citation.cfm?id=1596550.1596574"&gt;supports
   this assertion&lt;/a&gt;.&amp;nbsp; In it, they apply &lt;a href="http://research.microsoft.com/en-us/projects/chess/"&gt;CHESS&lt;/a&gt;-like
   techniques (with clever search space culling) to find race conditions.&amp;nbsp; Indeed
   we use similar techniques very successfully for our message-passing programming models
   on my team here at Microsoft.
&lt;/p&gt;
&lt;p&gt;
   Transactions are terrific because they are “automatic”.&amp;nbsp; You declare the boundaries,
   and the transactional machinery takes care of the rest.&amp;nbsp; This is true of databases
   and also TM.&amp;nbsp; Countless developers in the wild write massively concurrent programs
   by issuing operations against databases: they can do this so easily because they grok
   the simple façade that transactions provide.&amp;nbsp; Numerous server-side state-based
   applications use transactions to shield programmers from the pitfalls of concurrency.&amp;nbsp;
   Behold MSDTC.&amp;nbsp; The bet we were making is that similar models would scale down
   just as well “in the small”.
&lt;/p&gt;
&lt;p&gt;
   The canonical syntactic footprint of TM is also beautiful and simple.&amp;nbsp; You say:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;…
   concurrency-safe code goes here …&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   And everything in that block is magically concurrency-safe.&amp;nbsp; (Well, you still
   need to ensure the consistency part, but isolation and atomicity are built-in.&amp;nbsp;
   Mix this with Eiffel- or&amp;nbsp;&lt;a href="http://research.microsoft.com/en-us/projects/specsharp/"&gt;Spec#&lt;/a&gt;-style
   contracts and assertions like those in .NET 4.0, run at the end of each transaction,
   and you’re well on your way to verified consistency also.&amp;nbsp; The ‘check E’ work &lt;a href="http://research.microsoft.com/en-us/um/people/simonpj/Papers/stm/stm-invariants.pdf"&gt;in
   Haskell&lt;/a&gt; was right along these lines.)&amp;nbsp; You can read and write memory locations,
   call other methods, all without worrying about whether concurrency-safety will be
   at risk.
&lt;/p&gt;
&lt;p&gt;
   For example, consider three transactions running concurrently:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   x = 0, y = 0, z = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic {&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;z++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   No matter the order in which these run, the end result will be x == 3, y == 2, z ==
   1.
&lt;/p&gt;
&lt;p&gt;
   Contrast this elegant simplicity with the many pitfalls of locks:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      &lt;em&gt;Data races&lt;/em&gt;.&amp;nbsp; Like forgetting to hold a lock when accessing a certain
      piece of data.&amp;nbsp; And other flavors of data races, such as holding the wrong lock
      when accessing a certain piece of data.&amp;nbsp; Not only do these issues not exist,
      but the solution is not to add countless annotations associating locks with the data
      they protect; instead, you declare the scope of atomicity, and the rest is automatic. 
   &lt;li&gt;
      &lt;em&gt;Reentrancy&lt;/em&gt;.&amp;nbsp; Locks don’t compose.&amp;nbsp; Reentrancy and true recursive
      acquires are blurred together.&amp;nbsp; If a locked region expects reentrancy, usually
      due to planned recursion, life is good; if it doesn’t, life is bad.&amp;nbsp; This often
      manifests as virtual calls that reenter the calling subsystem while invariants remain
      broken due to a partial state transition.&amp;nbsp; At that point, you’re hosed. 
   &lt;li&gt;
      &lt;em&gt;Performance&lt;/em&gt;.&amp;nbsp; The tension between fine-grained locking (better scalability)
      versus coarse-grained locking (simplicity and superior performance due to fewer lock
      acquire/release calls) is ever-present.&amp;nbsp; This tension tugs on the cords of correctness,
      because if a lock is not held for long enough, other threads may be able to access
      data while invariants are still broken.&amp;nbsp; Scalability pulls you to engage in a
      delicate tip-toe right up to the edge of the cliff. 
   &lt;li&gt;
      &lt;em&gt;Deadlocks&lt;/em&gt;.&amp;nbsp; This one needs no explanation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   In a nutshell, locks are not declarative.&amp;nbsp; Not even close.&amp;nbsp; They are not
   associated with the data protected by those locks, but rather the code that accesses
   said data.&amp;nbsp; (For example: in the above code snippet, do we need three locks?&amp;nbsp;
   Or one?&amp;nbsp; Or …?&amp;nbsp; Imagine we choose three: one for each variable, x, y, and
   z.&amp;nbsp; What if we increment z, release its associated lock, and some other thread
   can now see the newly incremented z before the y and x get incremented.&amp;nbsp; Whether
   this is acceptable depends on the program.)&amp;nbsp; Sure, you can achieve atomicity
   and isolation, but only by intimately reasoning about your code by understanding the
   way they are implemented.&amp;nbsp; And if you care about performance, you are also going
   to need to think about hardware esoterica such as CMPXCHG, spin waiting, cache contention,
   optimistic techniques with version counters and memory models, ABA, and so on.
&lt;/p&gt;
&lt;p&gt;
   The contrast is stark.&amp;nbsp; Atomic-block-style transactions provide automatic serializability
   of whole regions of code, no matter what that code does, and the TM infrastructure
   does the rest, choosing between: optimistic, pessimistic, coarse, fine, etc.&amp;nbsp;
   The linearization point of a transaction is clear: the end of the atomic block.&amp;nbsp;
   TM can even adjust strategies based on the surrounding environment: hardware, dynamic
   program behavior, etc.&amp;nbsp; (“Policy”.)&amp;nbsp; In comparison to locks, TM is an order
   of magnitude simpler.&amp;nbsp; There have even been studies&amp;nbsp;whose conclusions&amp;nbsp;&lt;a href="http://www.cs.uoregon.edu/events/icse2009/images/postConf/pankratius-TMStudy-Pankratius-ICSE2009.pdf"&gt;support
   this assertion&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   (Transactions unfortunately do not address one other issue, which turns out to be
   the most fundamental of all: sharing.&amp;nbsp; Indeed, TM is insufficient – indeed, even
   dangerous – on its own is because it makes it very easy to share data and access it
   from multiple threads; first class isolation is far more important to achieve than
   faux isolation.&amp;nbsp; This is perhaps one major difference between client and server
   transactions.&amp;nbsp; Most server sessions are naturally isolated, and so transactions
   only interfere around the edges.&amp;nbsp; I’m showing my cards too early, and will return
   to this point much, much later in this essay.)
&lt;/p&gt;
&lt;p&gt;
   TM also has the attractive quality of automatic rollback of partial state updates.&amp;nbsp;
   (How did I get this far without discussing rollback?)&amp;nbsp; Concurrency aside, this
   avoids needing to write backout code to run in the face unhandled exceptions.&amp;nbsp;
   In retrospect this capability alone is almost enough to justify TM in limited quantities.&amp;nbsp;
   Reams of code “out there” contain brittle, untested, and, therefore, incorrect error
   handling code.&amp;nbsp; We have seen such code lead to problems ranging in severity:
   reliability issues leading to data loss, security exploits, etc.&amp;nbsp; Were we to
   replace all those try/catch/rethrow blocks of code with transactions, we could do
   away with this error prone spaghetti.&amp;nbsp; We’d also eliminate try/filter exploits
   thanks to Windows/Win32 2-pass SEH.&amp;nbsp; Sometimes I wish we focused on this simple
   step forward, forgot about concurrency-safety, and baby stepped our way forward.&amp;nbsp;
   Likely it wouldn’t have been enough, but I still wonder to this day.
&lt;/p&gt;
&lt;p&gt;
   We also toyed with the ability to replace reliability-oriented CER blocks with transactions.&amp;nbsp;
   As you go through a transaction, there is a log of forward progress and how to undo
   it.&amp;nbsp; So no matter the kind of failure, including OOM, you can rollback the partial
   state updates with zero allocation required.
&lt;/p&gt;
&lt;p&gt;
   At some point we began describing an ‘atomic’ block as though the program used a single
   global lock for all its concurrency operations.&amp;nbsp; This would be grossly inefficient,
   of course, and fails to capture the precise isolation and rollback properties, but
   nevertheless conveys the basic idea.&amp;nbsp; It also, as an aside, foreshadows a few
   of the difficult problems that lie ahead, namely strong vs. weak atomicity.&amp;nbsp;
   Even though there is only one, if you forget to hold this one global lock while accessing
   shared data, you’ve still got a data race on your hands.&amp;nbsp; This model won’t save
   you.&amp;nbsp; We will return to this later on.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Tough Decisions: Life as a Starving Artist &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   We faced some programming model decisions requiring artistic license early on.
&lt;/p&gt;
&lt;p&gt;
   One that we quickly decided was whether to automatically roll back a transaction in
   response to an unhandled exception thrown from within.&amp;nbsp; Such as with this code:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new Exception(“Whoops”);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If p evaluates to true, and hence an unhandled exception thrown, should that x++ be
   rolled back?
&lt;/p&gt;
&lt;p&gt;
   Most on the team said “Yes” as a gut reaction, whereas some argued we should require
   the programmer to catch-and-rollback by hand.&amp;nbsp; We settled on the automatic approach
   because it seemed to do what you would expect in all the cases we looked at.&amp;nbsp;
   Your transaction failed to complete normally and consistently.&amp;nbsp; We also debated
   whether to support a unilateral “Transaction.Abort()” capability; while we agreed
   a “Transaction.Commit()” would be silly – the only way to commit a transaction being
   to reach its end non-exceptionally – the jury remained split on unilateral abort.&amp;nbsp;
   We eventually found that, particularly when nesting is involved, the ability to detect
   a dire problem with the universe and bail unilaterally can be useful.
&lt;/p&gt;
&lt;p&gt;
   And we also hit some tough snags early on.&amp;nbsp; Some were trivial, like what happens
   when an exception is thrown out of an atomic block.&amp;nbsp; Of course that exception
   was likely constructed within the atomic block (‘throw new SomeException()’ being
   the most common form of ‘throw’), so we decided we probably need to smuggle at least
   some of that exception’s state out of the transaction.&amp;nbsp; Like its stack trace.&amp;nbsp;
   And perhaps its message string.&amp;nbsp; I wrote the initial incarnation of the CLR exception
   subsystem support, and stopped at shallow serialization across the boundary.&amp;nbsp;
   But this was a slippery slope, and eventually the general problem was seen, leading
   to more generalized nesting models (which I shall describe briefly below).&amp;nbsp; Another
   snag, which was quite non-trivial, was the impact to the debugging experience.&amp;nbsp;
   Depending on various implementation choices – like in-place versus buffered writes
   – you may need to teach the debugger about TM intrinsically.&amp;nbsp; And some of the
   challenges were fundamental to building a first class TM implementation.&amp;nbsp; Clearly
   the GC needed to know about TM and its logging, because it needs to keep both the
   “before” and “after” state of the transaction alive, in case it needed to roll back.&amp;nbsp;
   The JIT compiler was very quickly splayed open and on the surgery table.&amp;nbsp; And
   so on.
&lt;/p&gt;
&lt;p&gt;
   Throughout, it became abundantly clear that TM, much like generics, was a systemic
   and platform-wide technology shift.&amp;nbsp; It didn’t require type theory, but the road
   ahead sure wasn’t going to be easy.
&lt;/p&gt;
&lt;p&gt;
   So we knocked down many early snags, and kept plowing forward, eagerly and excitedly.&amp;nbsp;
   None of these challenges were insurmountable.&amp;nbsp; We remained hopeful and happy
   (perhaps even blissful) to continue exploring the space of possible solutions.&amp;nbsp;
   More irksome snags lurked right around the corner, however.&amp;nbsp; And little did we
   know that some decisions we were about to make would subject us to some of the biggest
   such snags.&amp;nbsp; TM’s greatest feature – slap an atomic around a block of code and
   it just gets better – would turn out to be its greatest challenge… but alas, I am
   again jumping ahead; more on that later.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Turtles, but How Far Down?&amp;nbsp; Or, Bounded vs. Unbounded Transactions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Not all transactions are equal.&amp;nbsp; There is a broad spectrum of TMs, ranging from
   those that are bounded to updating, say, 4 memory locations or cache lines, to those
   that are entirely unbounded.&amp;nbsp; Indeed TM blurs together with other hardware-accelerated
   synchronization techniques, like speculative lock elision (SLE).&amp;nbsp; The more constrained
   TM models are often hardware-hybrids, and the limitations imposed are typically due
   to physical hardware constraints.&amp;nbsp; Models can be pulled along other axes, however,
   such as whether memory locations must be tagged in order to be used in a transaction
   or not, etc.&amp;nbsp; Haskell requires this tagging (via TVars) so that side-effects
   are evident in the type system as with any other kind of monad.
&lt;/p&gt;
&lt;p&gt;
   We quickly settled on unbounded transactions.&amp;nbsp; Everything else looked like multi-word
   CAS and, although we knew multi-word CAS would be immensely useful for developing
   new lock-free algorithms, our aim was to build something radically new and with broader
   appeal.&amp;nbsp; If we ended up with a hardware-hybrid, we would expect the software
   to pick up the slack; you’d get nice acceleration within the hardware constraints,
   and then “fall off the silent cliff” to software emulation thereafter.&amp;nbsp; Thus
   the unbounded approach was chosen.
&lt;/p&gt;
&lt;p&gt;
   In hindsight, this was a critical decision that had far-reaching implications.&amp;nbsp;
   And to be honest, I now frequently doubt that it was the right call.&amp;nbsp; We had
   our hearts in the right places, and the entire industry was trekking down the same
   path at the same time (with the notable exception of Haskell).&amp;nbsp; But with the
   wisdom of age and hindsight, I do believe limited forms of TM could be wildly successful
   at particular tasks and yet would have avoided many of the biggest challenges with
   unbounded TM.
&lt;/p&gt;
&lt;p&gt;
   And believe me: many such challenges arose in the ensuing months.
&lt;/p&gt;
&lt;p&gt;
   An example of one challenge that didn’t threaten the model of TM per se, but sure
   did make our lives more difficult, is the compilation strategy we were forced to adopt.&amp;nbsp;
   Transactions cost something.&amp;nbsp; To transact a read or write entails a non-trivial
   amount of extra work; we spent a lot of time optimizing away redundant work, and developing
   new optimizations that reduced the overhead of TM.&amp;nbsp; But at the end of the day,
   the cost is not zero – and in fact, the common case is far from it.&amp;nbsp; Imagine
   you have an unbounded transaction model and are faced with compiling a particular
   method from MSIL to native code.&amp;nbsp; A simple separate-module -based compiler (i.e.
   not whole-program) will not necessarily know whether this method will get called from
   a transaction, or from non-transactional code, such that in the worst case the method
   must be prepared for transactional access.&amp;nbsp; There are a variety of techniques
   to use to produce code that supports both: the two extremes are (1) cloning, or (2)
   sharing w/ conditional dynamic checking.&amp;nbsp; Neither extreme is particularly attractive,
   and this choice represents a classic space-time tradeoff that entails finding a reasonable
   middle ground.&amp;nbsp; A JIT compiler can dynamically produce the version that is needed
   at the moment, but offline compilers – like the CLR’s NGEN – do not have this luxury.&amp;nbsp;
   And within Microsoft at least, and among shrink-wrap ISVs, offline compilation is
   of greater importance than JIT compilation.&amp;nbsp; For better or for worse.
&lt;/p&gt;
&lt;p&gt;
   The model of unbounded transactions is the hard part.&amp;nbsp; You surround any arbitrary
   code with ‘atomic { … }’ and everything just works.&amp;nbsp; It sounds beautiful.&amp;nbsp;
   But just think about what can happen within a transaction: memory operations, calls
   to other methods, P/Invokes to Win32, COM Interop, allocation of finalizable objects,
   I/O calls (disk, network, databases, console, …), GUI operations, lock acquisitions,
   CAS operations, …, the list goes on and on.&amp;nbsp; Versus bounded transactions, where
   we could say something like: if you do more than N things, the transaction will fail
   to run – deterministically.
&lt;/p&gt;
&lt;p&gt;
   Unbounded really was the golden nugget.&amp;nbsp;&amp;nbsp; But we should not be shy about
   what this decision implies.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Implementing the Idea&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   This leads me to a brief tangent on implementation.&amp;nbsp; Given that we didn’t implement
   TM with a single global lock, as the naïve mental model above suggests, you may wonder
   how we actually did do it.&amp;nbsp; Three main approaches were seriously considered:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      IL rewriting.&amp;nbsp; Use a tool that passes over the IL post-compilation to inject
      transaction calls. 
   &lt;li&gt;
      Hook the (JIT) compiler.&amp;nbsp; The runtime itself would intrinsically know how to
      inject such calls. 
   &lt;li&gt;
      Library-based.&amp;nbsp; All transactional operations would be explicit calls into the
      TM infrastructure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Approaches #1 and #2 would look similar, but the latter would be quite different.&amp;nbsp;
   Instead of: 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Or:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Atomic.Run(()
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;});&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   You might say something like:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Atomic.Run(()
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Atomic.Write(Atomic.Read(ref
   x) + 1); 
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;});&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   With enough language work, we could have tried to desugar the latter into the former,
   but when you start crossing method boundaries, everything gets more complicated.&amp;nbsp;
   (Do you create transactional clones of every method, and rewrite calls from ordinary
   methods to the transactional clone?&amp;nbsp; This is easy to do with a rewriter or compiler,
   but quite difficult with a pure library approach.)&amp;nbsp;&amp;nbsp; We also knew we’d need
   to do some very sophisticated compiler optimizations to get TM’s performance to the
   point of acceptable.&amp;nbsp; So we chose approach #2 for our “real” prototype, and never
   looked back.
&lt;/p&gt;
&lt;p&gt;
   After this architectural approach was decided, a vast array of interesting implementation
   choices remained.
&lt;/p&gt;
&lt;p&gt;
   We moved on to building the primitive library with all the TM APIs that the JIT would
   introduce calls into.&amp;nbsp; We quickly settled an approach much like Harris’s (and,
   at the time, pretty much the industry/research standard): optimistic reads, in-place
   pessimistic writes, and automatic retry.&amp;nbsp; That means reads do not acquire locks
   of any sort, and instead, once the end of the transaction has been reached, all reads
   are validated; if any locations read have been modified concurrently (or an uncommitted
   value was read), the whole transaction is thrown away and reattempted from the start.&amp;nbsp;
   Writes work like locks.&amp;nbsp; This approach makes reads cheap: a single read consists
   of reading the value, and a version number whose address is at a statically known
   offset.&amp;nbsp; No interlockeds.&amp;nbsp; This is great since reads typically far outnumber
   writes.&amp;nbsp; Down the line, we explored adding more sophisticated policy than this,
   which I will detail in brief below.
&lt;/p&gt;
&lt;p&gt;
   So the compiler would inject hooks for the above code like so:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;while
   (true) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;TX
   tx = new TX();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.OpenReadOptimistic(ref
   x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   tmp = x;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.OpenWritePessimistic(ref
   x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x
   = (tmp + 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (!tx.Validate())&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continue;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.Commit();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.Rollback();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Notice there are some obvious overheads in here:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      The atomic block becomes a loop (to support automated retry). 
   &lt;li&gt;
      A new transaction must be allocated and likely placed in TLS (if methods are called). 
   &lt;li&gt;
      A try/catch block is used to initiate rollback on unhandled exceptions. 
   &lt;li&gt;
      Each unique location read in a block requires at least one call to OpenReadOptimistic. 
   &lt;li&gt;
      Each unique location written requires at least one call to OpenWritePessimistic. 
   &lt;li&gt;
      Each location read must be validated (at Validate), and finally the transaction is
      committed (at Commit). 
   &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Much of the work in the compiler was meant to reduce these overheads.&amp;nbsp; For example,
   if the same location is read multiple times, there’s no need to call OpenReadOptimistic
   more than once.&amp;nbsp; If the compiler can statically detect this, it may elide some
   of the calls.&amp;nbsp; If the same location is read and then written – as in the above
   example – only the write lock must be acquired.&amp;nbsp; If no methods are called, the
   transaction object can be enregistered, and we needn’t add it to TLS so long as the
   exception trap code knows how to move it from register to TLS on demand.&amp;nbsp; Et
   cetera.
&lt;/p&gt;
&lt;p&gt;
   There are other overheads that are not so obvious.&amp;nbsp; Optimistic reads mandate
   that there is a version number for each location somewhere, and pessimistic writes
   mandate that there is a lock for each location somewhere.
&lt;/p&gt;
&lt;p&gt;
   A straightforward technique is to use a hashing scheme to associate locations with
   this auxiliary data: each address is hashed to index into a table of version numbers
   and locks.&amp;nbsp; This leads to false sharing, of course, but reduces space overhead
   and makes lookup fast.&amp;nbsp; Unfortunately, in a garbage collected environment, addresses
   are not stable and therefore hashing becomes complicated.&amp;nbsp; You can use object
   hash codes for this purpose, but .NET hash codes are overridable; and generating them
   is not nearly as cheap as using the memory location’s address, which by definition
   is already in-hand.&amp;nbsp; Other alternatives of course exist.&amp;nbsp; You can associate
   version numbers and locks with the objects themselves, just like monitors and object
   headers/sync-blocks in the CLR: this provides object-granularity locking.&amp;nbsp; Ahh,
   the age old tension of fine vs. coarse grained locking comes up again.
&lt;/p&gt;
&lt;p&gt;
   We eventually realized we’d want both optimistic and pessimistic reads, the latter
   of which worked a lot like reader/writer locks.&amp;nbsp; We crammed all these into a
   clever little word-sized data structure which worked a lot like Vista’s SRWL data
   structure.&amp;nbsp; Except that it also contained a version number.
&lt;/p&gt;
&lt;p&gt;
   It was always surprising to me what strange things in the runtime we bumped up against.&amp;nbsp;
   We realized a nice GC optimization: instead of keeping strong references to all intermediary
   states in a transaction log, we could keep weak references to all but the “before”
   and “after” state.&amp;nbsp; This is important when transacting synthetic situations like
   this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;static
   BigHonkinFoo s_f;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; 1000000; i++)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s_f
   = new BigHonkinFoo();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Of course you wouldn’t write that code exactly.&amp;nbsp; But there’s no need to keep
   alive all but the s_f that existed prior to entering the atomic block and the current
   one at any given time.&amp;nbsp; But this leads to particularly hairy finalization issues.&amp;nbsp;
   If a finalizable object is allocated within a transaction (say BigHonkinFoo), and
   is then reclaimed, its Finalize() method will be scheduled to run on a separate thread.&amp;nbsp;
   Yet the transaction log may contain references to it.&amp;nbsp; Thus there is a race between
   the transaction’s final outcome and the invocation of the finalizer.&amp;nbsp; We came
   up with a clever solution for this, but there were countless other clever solutions
   for various things not worth diving too deep into.
&lt;/p&gt;
&lt;p&gt;
   Hacking is fun.&amp;nbsp; However, it was not going to be what made or broke TM as a model.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part I: the I/O Problem&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   It wasn’t long before we realized another sizeable, and more fundamental, challenge
   with unbounded transactions.&amp;nbsp; Finalizers touched on this.&amp;nbsp; What do we do
   with atomic blocks that do not simply consist of pure memory reads and writes?&amp;nbsp;
   (In other words, the majority of blocks of code written today.)&amp;nbsp; This was not
   just a pesky question of how to compile a piece of code, but rather struck right at
   the heart of the TM model.
&lt;/p&gt;
&lt;p&gt;
   You already saw the OpenReadOptimistic, OpenWritePessimistic, Validate, Commit, and
   Rollback pseudo-TM infrastructure calls, each of which operated on memory locations.&amp;nbsp;
   But what about a read or write from a single block or entire file on the filesystem?&amp;nbsp;
   Or output to the console?&amp;nbsp; Or an entry in the Event Log?&amp;nbsp; What about a web
   service call across the LAN?&amp;nbsp; Allocation of some native memory?&amp;nbsp; And so
   on.&amp;nbsp; Ordinarily these kinds of operations will be composed with other memory
   operations, with some interesting invariant relationship holding between the disparate
   states.&amp;nbsp; A transaction comprised of a mixture still ought to remain atomic and
   isolated.
&lt;/p&gt;
&lt;p&gt;
   The answer seemed clear.&amp;nbsp; At least in theory.&amp;nbsp; The transaction literature,
   including &lt;a href="http://www.amazon.com/exec/obidos/ASIN/1558601902/bluebytesoftw-20"&gt;Reuter
   and Gray’s classic&lt;/a&gt;, had a simple answer for such things: on-commit and on-rollback
   actions, to perform or compensate the logical action being performed, respectively.&amp;nbsp;
   (Around this same time, the Haskell folks were creating just this capability in their
   STM, where ‘onCommit’ and ‘onRollback’ would take arbitrary lambdas to execute the
   said operation at the proper time.)&amp;nbsp; Because we were working primarily in .NET
   – with a side project targeting C++ -- we decided to use the new System.Transactions
   technology in 2.0 to hook into inherently transactional resources, like transacted
   NTFS, registry, and, of course, databases.
&lt;/p&gt;
&lt;p&gt;
   (Digging through my blog, I found &lt;a href="http://www.bluebytesoftware.com/blog/2006/06/20/AVolatileTransactionResourceManagerForMemoryAllocationdeallocation.aspx"&gt;this
   article&lt;/a&gt; written back in June 2006 about building a volatile resource manager for
   memory allocation/free operations, just as an example.)
&lt;/p&gt;
&lt;p&gt;
   This worked, though we were quite obviously swimming upstream.&amp;nbsp; Numerous challenges
   confronted us.
&lt;/p&gt;
&lt;p&gt;
   A significant problem was that not all operations are inherently transactional, so
   in many cases we were faced with the need to add faux transactions on top of existing
   non-transactional services.&amp;nbsp; (Already-transactional services were easy, like
   databases.&amp;nbsp; Except that mixing fine-grain TM transactions with distributed DTC
   transactions makes my skin crawl.)&amp;nbsp; For example, how would you undo a write to
   the console?&amp;nbsp; Well, you can’t, really.&amp;nbsp; So we decided maybe the right default
   for Console.WriteLine was to use an on-commit action to perform the actual write only
   once the transaction had committed.
&lt;/p&gt;
&lt;p&gt;
   But in even thinking this thought, we realized we were standing on shaky ground.&amp;nbsp;
   What if the WriteLine was followed by something like a ReadLine, for example, where
   the program was meant to wait for the user to enter something into the console (likely
   in response to the prompt output by WriteLine)?&amp;nbsp; (This example is a toy, of course,
   but represents a more fundamental pattern common in networked programs.)&amp;nbsp; The
   basic problem was immediately clear.&amp;nbsp; Adding isolation to an existing non-isolated
   operation is not always behavior-preserving, particularly when I/O is involved.&amp;nbsp;
   Sometimes it is necessary to step outside of the isolation that would otherwise get
   poured on top by a simple transactional model.
&lt;/p&gt;
&lt;p&gt;
   This particular problem isn’t specific to traditional I/O per se.
&lt;/p&gt;
&lt;p&gt;
   Foreign function interface calls through.NET’s P/Invoke suffer from like problems.&amp;nbsp;
   A call to CreateEvent may be compensatable (via an on-rollback action) with a call
   to CloseHandle.&amp;nbsp; But this is flawed.&amp;nbsp; Once that event’s HANDLE is requested,
   and/or it is passed to other Win32 APIs like MsgWaitForMultipleObjects, then the isolation
   of the faux transaction is broken, and real state must be provided to the Win32 APIs.&amp;nbsp;
   And if another thread were to look up that HANDLE – perhaps through a name given to
   it in the call to CreateEvent – it may be able to see and interact with that event
   before the enclosing transaction has been committed.&amp;nbsp; The abstraction leaks.&amp;nbsp;
   And even if the abstraction is perfect, it is obvious there’s quite a bit of work
   to be had in order to transact all the touch points between .NET and Win32, of which
   there are many.&amp;nbsp; And I mean many.
&lt;/p&gt;
&lt;p&gt;
   Other issues wait just around the corner.&amp;nbsp; For example, how would you treat a
   lock block that was called from within a transaction?&amp;nbsp; (You might say “that’s
   just not supported”, but when adding TM to an existing, large ecosystem of software,
   you’ve got to draw the compatibility line somewhere.&amp;nbsp; If you draw it too carelessly,
   large swaths of existing software will not work; and in this case, that often meant
   that we claimed to provide unbounded transactions, and yet we would be placing bounds
   on them such that a lot of existing software could not be composed with transactions.&amp;nbsp;
   Not good.)&amp;nbsp; A seemingly straightforward answer is to treat a lock block like
   an atomic block.&amp;nbsp; So if you encounter:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lock
   (obj) { … }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   it is logically transformed into:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { … }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   On the face of it, this looks okay.&amp;nbsp; (Forget problems like freeform use of Monitor.Enter/Exit
   for now.)&amp;nbsp; We’re strengthening the atomicity and isolation, so what could go
   wrong?&amp;nbsp; Well, it turns out that examples like this can also suffer from the “too
   much isolation” problem.&amp;nbsp; Adding transactions to a lock-block extends the lifetime
   of the isolation of that particular block’s effects, possibly leading to lack of forward
   progress.&amp;nbsp; In fact, you don’t need locks to illustrate the problem.&amp;nbsp; Imagine
   a simple lock-free algorithm that communicates between threads using shared variables:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;volatile
   int flag = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;flag
   = 1;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   (flag != 1) ;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;while
   (flag == 1) ;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;flag
   = 2;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If you invoke this code from within a transaction (on each thread), you’re apt to
   lead to deadlock.&amp;nbsp; Both transactions’ effects will be isolated from the others’,
   whereas we are quite obviously intending to publish the updates to the flag variable
   immediately.
&lt;/p&gt;
&lt;p&gt;
   Anyway, the whole lock thing is a bit of a digression.&amp;nbsp; The simple fact is that
   very little .NET code would actually run inside an atomic block but for things like
   collections and pure computations due to the I/O problem.&amp;nbsp; You can develop one-off
   solutions for each problem that arises – and indeed we did so for many of them – and
   even hang those solutions underneath one general framework – like System.Transactions
   – but you cannot help but eventually become overwhelmed by the totality of the situation.&amp;nbsp;
   The team experimented with static checking to turn these dynamic failures into static
   ones, but this only marginally improved matters.
&lt;/p&gt;
&lt;p&gt;
   I could go on and on about the I/O problem, its various incarnations, and what we
   did about it.&amp;nbsp; Instead I will sum it up: this problem was, and still is, the
   “elephant in the room” threatening unbounded TM’s broader adoption.
&lt;/p&gt;
&lt;p&gt;
   The question ultimately boils down to this: is the world going to be transactional,
   or is it not?
&lt;/p&gt;
&lt;p&gt;
   Whether unbounded transactions foist unto the world will succeed, I think, depends
   intrinsically on the answer to this question.&amp;nbsp; It sure looked like the answer
   was going to be “Yes” back when transactional NTFS and registry was added to Vista.&amp;nbsp;
   But the momentum appears to have slowed dramatically.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Nesting&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Let’s get back to some fun, less depressing material.&amp;nbsp; There are more surprises
   lurking ahead.
&lt;/p&gt;
&lt;p&gt;
   I already mentioned a great virtue of transactions is their ability to nest.&amp;nbsp;
   But I neglected to say how this works.&amp;nbsp; And in fact when we began, we only recognized
   one form of nesting.&amp;nbsp; You’re in one atomic block and then enter into another
   one.&amp;nbsp; What happens if that inner transaction commits or rolls back, before the
   fate of the outer transaction is known?&amp;nbsp; Intuition guided us to the following
   answer:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      If the inner transaction rolls back, the outer transaction does not necessarily do
      so.&amp;nbsp; However, no matter what the outer transaction does, the effects of the inner
      will not be seen. 
   &lt;li&gt;
      If the inner transaction commits, the effects remain isolated in the outer transaction.&amp;nbsp;
      It “commits into” the outer transaction, we took to saying.&amp;nbsp; Only if the outer
      transaction subsequently commits will the inner’s effects be visible; if it rolls
      back, they are not.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   For example, consider this code:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;void
   f() {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   g() {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p1)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;g();&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new BarException();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}
   catch {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p0)&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;throw;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p2)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new FooException();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Imagine x = y = 0 at the start, and we invoke f.&amp;nbsp; Many outcomes are possible.
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      If p1 is true, g will throw an exception, aborting Tx1’s write to y. There are then
      two possibilities.&amp;nbsp; (1)If p0 is true, the exception is repropagated and Tx0 will
      also abort, rolling back its write to x; this leaves x == y == 0.&amp;nbsp; (2) If p0
      is false, the exception is swallowed, and Tx0 proceeds to committing its write to
      x; this leaves x == 1, whereas y == 1. 
   &lt;li&gt;
      If p1 is false, on the other hand, g will not throw anything.&amp;nbsp; Tx1 will commit
      its write to y “into” the outer transaction Tx0.&amp;nbsp; One of two outcomes will now
      occur depending on the value of p2.&amp;nbsp; (1) If p2 is true, an exception is thrown
      out of f, and Tx0 rolls back both the inner transaction Tx1’s effects and its own,
      leaving x == y == 0.&amp;nbsp; (2) Else, f completes ordinarily, and Tx0 commits both
      Tx1’s and its own effects, leading to x == y == 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   We expected most peoples’ intuition to match this behavior.
&lt;/p&gt;
&lt;p&gt;
   The canonical working example was a BankAccount class:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;class
   BankAccount&lt;br&gt;
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;decimal
   m_balance;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Deposit(decimal delta) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { m_balance += delta; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Transfer(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BankAccount
   a, BankAccount b, decimal delta) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;a.Deposit(-delta);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b.Deposit(delta);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   This was an illustrative and beautiful example.&amp;nbsp; It made beautiful slide-ware.&amp;nbsp;
   We are composing the Deposit operations of two separate bank accounts into a single
   Transfer method.&amp;nbsp; Of course doing the a.Deposit(-delta) and b.Deposit(delta)
   must be made atomic, else a failure could either lead to missing money, and/or someone
   could witness the world with the money in transit (and nowhere except for one a thread’s
   stack) rather than having been transferred atomically.&amp;nbsp; And building the same
   thing with locks is frustratingly difficult: using fine-grained per-account locks
   can lead to deadlock very quickly.
&lt;/p&gt;
&lt;p&gt;
   Intuitively we walked down many variants of this mode of nesting.&amp;nbsp; We reacquainted
   ourselves with Moss’s &lt;a href="http://portal.acm.org/citation.cfm?id=3529"&gt;great dissertation
   on the topic&lt;/a&gt;, and remembered this intuitive nesting mode as closed nested transactions.&amp;nbsp;
   And we shortly recognized the need for another mode: &lt;a href="http://www.cs.utah.edu/wmpi/2006/final-version/wmpi-posters-1-Moss.pdf"&gt;open
   nested transactions&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   To motivate the need for open nesting, imagine we’ve got a hashtable whose physical
   storage is independent from its logical storage.&amp;nbsp; Resizing the table of buckets,
   for example, has little to do with whether a particular {key, value} pair exists within
   those buckets.&amp;nbsp; The resizing operation, in fact, is logically idempotent and
   isolated: the same set of keys will exist within the table both before and after such
   an operation.&amp;nbsp; So we can actually commit the physical effects of such an operation
   eagerly.&amp;nbsp; With a naïve TM implementation, two independent keys hashing to the
   same bucket will conflict, and the reads and writes for such operations will live
   as long as the enclosing user-level transactions.&amp;nbsp; Instead, we can serialize
   logical operations with respect to one another at a “higher level” than physically
   independent operations do, leading to greater concurrency.&amp;nbsp; Two transactions
   will only conflict in long-running transactions if they truly operate on the same
   keys, rather than just happening to hash to the same bucket.
&lt;/p&gt;
&lt;p&gt;
   Open nesting forced us to contemplate the sharing of state between outer and inner
   transactions more deliberately, and gave us some troubles syntactically.&amp;nbsp; We
   had wanted to say:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // ordinary closed nesting.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Foo
   f = new Foo();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic(open)
   { /// open nesting.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;…
   f? …&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But is it really legal for the inner transaction here to access the ‘f’, which has
   been constructed and is presumably uncommitted in the outer transaction?&amp;nbsp; With
   closed nested transactions there is lock compatibility between the outer and inner
   transactions.&amp;nbsp; An inner closed nested transaction can of course read a memory
   location write-locked by the outer transaction, for example.&amp;nbsp; However, the same
   must not true of open nesting, because an open nested transaction commits “to the
   world” rather than into its outer transaction.&amp;nbsp; Allowing it to read and then
   potentially publish uncommitted state would violate serializability.&amp;nbsp; It’s possible
   that the inner open nested transaction will commit, whereas the outer will roll back.&amp;nbsp;
   (The reverse situation is equally problematic.)&amp;nbsp; And yet it’s darn useful to
   pass state from an outer to an inner transaction – and indeed, often impossible to
   do anything otherwise – yet what if the key itself were a complicated object graph
   rather than value, and the key bleeds across transaction boundaries?
&lt;/p&gt;
&lt;p&gt;
   Many issues like this arose.&amp;nbsp; Our straightforward answer was that only pass-by-value
   worked across such a boundary.&amp;nbsp; I don’t think we ever found nirvana here.
&lt;/p&gt;
&lt;p&gt;
   We developed other transaction modes also.
&lt;/p&gt;
&lt;p&gt;
   As we added data parallel operations within a nested transaction, we realized that
   we’d need something a lot like closed nesting but with special accommodation for intra-transaction
   parallelism.&amp;nbsp; This led us to &lt;a href="http://www.cs.rochester.edu/meetings/TRANSACT07/papers/agrawal.pdf"&gt;parallel
   nested transactions&lt;/a&gt;, enabling lock sharing from a parent to its many data parallel
   children.&amp;nbsp; These children could not communicate with one another other than to
   “commit into” the parent, and subsequently reforking, thereby ensuring non-interference
   between them.&amp;nbsp; Of course children could share read-locks amongst one another,
   just not write locks.
&lt;/p&gt;
&lt;p&gt;
   And we continued to reject the temptation of adding weakened serializability modes
   a la relational databases (unrepeatable reads, etc).&amp;nbsp; Although we expected this
   to arise out of necessity with time, it never did; the various nesting modes we provided
   seemed to satisfy the typical needs.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;A Better Condition Variable&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Here’s a brief aside on one of TM’s bonus features.
&lt;/p&gt;
&lt;p&gt;
   Some TM variants also provide for “condition variable”-like facilities for coordination
   among threads.&amp;nbsp; I think Haskell was the first such TM to provide a ‘retry’ and
   ‘orElse’ capability.&amp;nbsp; When a ‘retry’ is encountered, the current transaction
   is rolled back, and restarted once the condition being sought becomes true.&amp;nbsp;
   How does the TM subsystem know when that might be?&amp;nbsp; This is an implementation
   detail, but one obvious choice is to monitor the reads that occurred leading up to
   the ‘retry’ – those involved in the evaluation of the predicate – and once any of
   them changes, to reschedule that transaction to run.&amp;nbsp; Of course, it will reevaluate
   the predicate and, if it has become false, the transaction will ‘retry’ again.
&lt;/p&gt;
&lt;p&gt;
   A simple blocking queue could be written this way.&amp;nbsp; For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;object
   TakeOne()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (Count == 0)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;retry;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   Pop();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If, upon entering the atomic block, Count is witnessed as being zero, we issue a retry.&amp;nbsp;
   The transaction subsystem notices we read Count with a particular version number,
   and then blocks the current transaction until Count’s associated version number changes.&amp;nbsp;
   The transaction is then rescheduled, and races to read Count once again.&amp;nbsp; After
   Count is seen as non-zero, the Pop is attempted.&amp;nbsp; The Pop, of course, may fail
   because of a race – i.e. we read Count optimistically without blocking out writers
   – but the usual transaction automatic-reattempt logic will kick in to mask the race
   in that case.
&lt;/p&gt;
&lt;p&gt;
   The ‘orElse’ feature is a bit less obvious, though still rather useful.&amp;nbsp; It enables
   choice among multiple transactions, each of which may end up issuing a ‘retry’.&amp;nbsp;
   I don’t think I’ve seen it in any TMs except for ours and Haskell’s.
&lt;/p&gt;
&lt;p&gt;
   To illustrate, imagine we’ve got 3 blocking queues like the one above.&amp;nbsp; Now imagine
   we’d like to take from the first of those three that becomes non-empty.&amp;nbsp; ‘orElse’
   makes this simple:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;BlockingQueue
   bq1 = …, bq2 = …, bq3 = …;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;object
   obj =&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;orElse
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq1.TakeOne(),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq2.TakeOne(),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq3.TakeOne()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   While ‘orElse’ is perhaps an optional feature, you simply can’t write certain kinds
   of multithreaded algorithms without ‘retry’.&amp;nbsp; Anything that requires cross-thread
   communication would need to use spin variables.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Deliberate Plans of Action: Policy&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I waved my hands a bit above perhaps without you even knowing it.&amp;nbsp; When I talk
   about optimistic, pessimistic, and automatic retry, I am baking in a whole lot of
   policy.&amp;nbsp; It turns out there is a wide array of techniques.&amp;nbsp; The simplest
   question we faced early on was, when an optimistic read fails to validate at the end
   of a transaction, when should we reattempt execution of that transaction?
&lt;/p&gt;
&lt;p&gt;
   The naïve answer is “immediately”.&amp;nbsp; But obviously that would lead to livelock
   under some conditions.&amp;nbsp; A more reasonable answer is “spin for N cycles and then
   retry”.&amp;nbsp; But this too can lead to livelock.&amp;nbsp; A better answer is to either
   choose some random strategy, or to make an intelligent adaptive choice.&amp;nbsp; We experimented
   with many such variants, including random backoff, sophisticated waiting and signaling
   based on the memory locations in question, among others.&amp;nbsp; We even played games
   like giving transactions karma points for cooperatively acquiescing to other competing
   transactions, and allowing those transactions with the most karma points to make more
   forward progress before interrupting them.
&lt;/p&gt;
&lt;p&gt;
   &lt;a href="http://lpd.epfl.ch/kapalka/files/robust-cm-scool05.pdf"&gt;A few good papers&lt;/a&gt; supplied
   useful (and entertaining) reading material on the topic, but to be honest, nobody
   had a good answer at the time.&amp;nbsp; Thankfully these are all implementation details.&amp;nbsp;
   So we were free to experiment.
&lt;/p&gt;
&lt;p&gt;
   Deadlock breaking also requires policy.&amp;nbsp; Thankfully we can actually roll back
   the effects of transactions engaged in a deadly embrace with TM, so we merely need
   to know how often to run the deadlock detection algorithm.&amp;nbsp; There was a similar
   problem when deciding to back off outer layers of nesting, and in fact this becomes
   more complicated when deadlocks are involved.&amp;nbsp; Imagine:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   This deadlock-prone example is tricky because rolling back the inner-most transactions
   won’t be sufficient to break the deadlock that may occur.&amp;nbsp; Instead the TM policy
   manager needs to detect that multiple levels of nesting are involved and must be blown
   away in order to unstick forward progress.
&lt;/p&gt;
&lt;p&gt;
   Another variant that went beyond deciding when to favor one transaction over another
   was to upgrade to pessimistic locking if optimistic let us down.&amp;nbsp; The whole justification
   behind optimistic is that, …well, we’re optimistic that conflicts won’t happen.&amp;nbsp;
   So it seems reasonable that, if they do occur, we fall back to something more, …well,
   pessimistic.&amp;nbsp; There is a dial here too.&amp;nbsp; Perhaps you only want to fall back
   to pessimistic after failing optimistically N times in a row, where N &amp;gt; 1.&amp;nbsp;
   As I mentioned above, our single-word lock associated with each object supported both
   locking and versioning cheaply.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part II: Weak or Strong Atomicity?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   All along, we had this problem nipping at our heels.&amp;nbsp; What happens if code accesses
   the same memory locations from inside and outside a transaction?&amp;nbsp; We certainly
   expected this to happen over the life of a program: state surely transitions from
   public and shared among threads to private to a single thread regularly.&amp;nbsp; But
   if some location were to be accessed transactionally and non-transactionally concurrently,
   at once, we’d (presumably) have a real mess on our hands.&amp;nbsp; A supposedly atomic,
   isolated, etc. transaction would no longer be protected from the evils of racey code.
&lt;/p&gt;
&lt;p&gt;
   For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;
   // No-Tx&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Can we make any statements about the value of x after Tx0 commits (or rolls back)?&amp;nbsp;
   Not really.&amp;nbsp; It depends on the way the particular TM being used has been implemented.&amp;nbsp;
   An in-place model that rolls back could not only roll back Tx0’s but also the unprotected
   x++’s write.&amp;nbsp; And so on.
&lt;/p&gt;
&lt;p&gt;
   On one hand, this code is racey.&amp;nbsp; So you could explain away the undefined behavior
   as being a race condition.&amp;nbsp; On the other hand, it was also troublesome.&amp;nbsp;
   All those problems with locks begin cropping up all over the place.&amp;nbsp; It would
   have been ideal if we could notify developers that they made a mistake.&amp;nbsp; Then
   we could have made the assertion that data races are simply not possible with TM.
&lt;/p&gt;
&lt;p&gt;
   (Except for consistency-related ones, of course.)
&lt;/p&gt;
&lt;p&gt;
   At the same time, many hardware models were being explored.&amp;nbsp; And of course in
   hardware you’ve got the physical addresses that variables resolve to and needn’t worry
   about aliasing.&amp;nbsp; So it was actually possible to issue a fault if a location was
   used transactionally and non-transactionally at once.&amp;nbsp; But given that our solution
   was software-based, we were uncomfortable betting the farm on hardware support.
&lt;/p&gt;
&lt;p&gt;
   Another approach was static analysis.&amp;nbsp; We could require transactional locations
   to be tagged, for example.&amp;nbsp; This had the unfortunate consequence of making reusable
   data structures less, well, reusable.&amp;nbsp; Collections for example presumably need
   to be usable from within and outside transactions alike.&amp;nbsp; After-the-fact analysis
   could be applied without tagging, but false positives were common.&amp;nbsp; We never
   really took a hard stance on this problem, but always assumed the combination of static
   analysis, tooling, and, perhaps someday, hardware detection would make this problem
   more diagnosable.&amp;nbsp; But I think we generally resolved ourselves to the fact that
   our TM would suffer from weak atomicity problems.
&lt;/p&gt;
&lt;p&gt;
   We thought this was explainable.&amp;nbsp; Sadly it led to something that surely was not.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part III: the Privatization Problem&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I still remember the day like it was yesterday.&amp;nbsp; A regular weekly team meeting,
   to discuss our project’s status, future, hard problems, and the like.&amp;nbsp; A summer
   intern on board from a university doing pioneering work in TM, sipping his coffee.&amp;nbsp;
   Me, sipping my tea.&amp;nbsp; Then that same intern’s casual statement pointing out an
   Earth-shattering flaw that would threaten the kind of TM we (and most of the industry
   at the time) were building.&amp;nbsp; We had been staring at the problem for over a year
   without having seen it.&amp;nbsp; It is these kinds of moments that frighten me and make
   me a believer in formal computer science.
&lt;/p&gt;
&lt;p&gt;
   Here it is in a nutshell:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;bool
   itIsOwned = false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;MyObj
   x = new MyObj();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Claim the state for my use:&lt;span style="mso-tab-count: 1"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (!itIsOwned)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itIsOwned
   = true;&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x.field
   += 42&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   z = x.field;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   The Tx0 transaction changes itIsOwned to true, and then commits.&amp;nbsp; After it has
   committed, it proceeds to using whatever state was claimed (in this case an object
   referred to by variable x) outside of the purview of TM.&amp;nbsp; Meanwhile, another
   transaction Tx1 has optimistically read itIsOwned as false, and has gone ahead to
   use x.&amp;nbsp; An update in-place system will allow that transaction to freely change
   the state&amp;nbsp;of x.&amp;nbsp; Of course, it will roll back here, because isItOwned changed
   to&amp;nbsp;true.&amp;nbsp; But by then it is too late: the other thread using x outside of
   a transaction will see constantly changing state&amp;nbsp;– torn reads even –&amp;nbsp;and
   who knows what will happen from there.&amp;nbsp;&amp;nbsp; A known flaw in any weakly atomic,
   update in-place&amp;nbsp;TM.
&lt;/p&gt;
&lt;p&gt;
   If this example appears contrived, it’s not.&amp;nbsp; It shows up in many circumstances.&amp;nbsp;
   The first one in which we noticed it was when one transaction removes a node from
   a linked list, while another transaction is traversing that same list.&amp;nbsp; If the
   former thread believes it “owns” the removed element simply because it took it out
   of the list, someone’s going to be disappointed when its state continues to change.
&lt;/p&gt;
&lt;p&gt;
   This, we realized, is just part and parcel of an optimistic TM system that does in-place
   writes.&amp;nbsp; I don’t know that we ever fully recovered from this blow.&amp;nbsp; It was
   a tough pill to swallow.&amp;nbsp; After that meeting, everything changed: a somber mood
   was present and I think we all needed a drink.&amp;nbsp; Nevertheless we plowed forward.
&lt;/p&gt;
&lt;p&gt;
   We explored a number of alternatives.&amp;nbsp; And so did the industry at large, because
   that intern in question published &lt;a href="http://portal.acm.org/citation.cfm?id=1281161"&gt;a
   paper on the problem&lt;/a&gt;.&amp;nbsp; One obvious solution is to have a transaction that
   commits a change to a particular location wait until all transactions that have possibly
   read that location have completed – a technique we called quiescence.&amp;nbsp; We experimented
   with this approach, but it was extraordinarily complicated, for obvious reasons.
&lt;/p&gt;
&lt;p&gt;
   We experimented with blends of pessimistic operations instead of optimistic, alternative
   commit protocols, like using a “commit ticket” approach that serializes transaction
   commits, each of which tended to sacrifice performance greatly.&amp;nbsp; Eventually the
   team decided to do buffered writes instead of in-place writes, because any concurrent
   modifications in a transaction will simply not modify the actual memory being used
   outside of the transaction unless that transaction successfully commits.
&lt;/p&gt;
&lt;p&gt;
   This, however, led to still other problems, like the granular loss of atomicity problem.&amp;nbsp;
   Depending on the granularity of your buffered writes – we chose object-level – you
   can end up with false sharing of memory locations between transactional and non-transactional
   code.&amp;nbsp; Imagine you update two separate fields of an object from within and outside
   a transaction, respectively, concurrently.&amp;nbsp; Is this legal?&amp;nbsp; Perhaps not.&amp;nbsp;
   The transaction may bundle state updates to the whole object, rather than just one
   field.
&lt;/p&gt;
&lt;p&gt;
   All these snags led to the realization that we direly needed a memory model for TM.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part IV: Where is the Killer App?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Throughout all of this, we searched and searched for the killer TM app.&amp;nbsp; It’s
   unfair to pin this on TM, because the industry as a whole still searches for a killer
   concurrency app.&amp;nbsp; But as we uncovered more successes in the latter, I became
   less and less convinced that the killer concurrency apps we will see broadly deployed
   in the next 5 years needed TM.&amp;nbsp; Most enjoyed natural isolation, like embarrassingly
   parallel image processing apps.&amp;nbsp; If you had sharing, you were doing something
   wrong.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;In Conclusion&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I eventually shifted focus to enforcing coarse-grained isolation through message-passing,
   and fine-grained isolation through type system support a la Haskell’s state monad.&amp;nbsp;
   This would help programmers to realize where they accidentally had sharing, I thought,
   rather than merely masking this sharing and making it all work (albeit inefficiently).
&lt;/p&gt;
&lt;p&gt;
   I took this path not because I thought TM had no place in the concurrency ecosystem.&amp;nbsp;
   But rather because I believed it did have a place, but that several steps would be
   needed before getting there.
&lt;/p&gt;
&lt;p&gt;
   I suspected that, just like with Argus, you’d want transactions around the boundaries.&amp;nbsp;
   And that you’d probably want something like open nesting for fine-grained scalable
   data structures, like shared caches.&amp;nbsp; These are often choke points in a coarse-grained
   locking system, and often cannot be fully isolated, at least in the small.&amp;nbsp; Ironically
   I am just now arriving there.&amp;nbsp; In the system I work on I see these issues actually
   staring us in the face.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   This is just my own personal view on TM.&amp;nbsp; You may also be interested in reading
   the current STM.NET team’s views also, available on &lt;a href="http://blogs.msdn.com/stmteam/"&gt;their
   MSDN blog&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   For me the TM project was particularly enjoyable.&amp;nbsp; And it was a great learning
   experience.&amp;nbsp; I worked with some amazing people, and it was a privilege.&amp;nbsp;
   You really had the sense that something big was right around the corner, and every
   day was a rush of enjoyment.&amp;nbsp; Despite running as fast as we could, it seemed
   like we could just barely keep pace with the research community.&amp;nbsp; Over time more
   and more researchers turned to TM, and I distinctly recall reading at least one new
   TM paper per week.
&lt;/p&gt;
&lt;p&gt;
   This was also the first time I realized that Microsoft, at its core, really does operate
   like a collection of many startups.&amp;nbsp; Our TM work was a grassroots movement, and
   there was no official sponsorship for our effort at the start.&amp;nbsp; It was just a
   group of people independently getting together to discuss how TM might fit into the
   direction the industry was headed.&amp;nbsp; Eventually TM started showing up on slide
   decks in presentations to management, followed by dedicated TM reviews, and even a
   BillG review.&amp;nbsp; I will never forget, a couple years after that review – during
   an overall concurrency review – Bill standing up at the whiteboard, drawing the code
   “atomic { … }” and asking something to the effect: “Why can’t you just use transactional
   memory for that?”&amp;nbsp; I guess the idea stuck with him too.
&lt;/p&gt;
&lt;p&gt;
   Who knows.&amp;nbsp; Maybe in 10 years, the world will be transactional after all.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=4cfad7df-ed84-46a4-a961-54aafdaeb9d7" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=9391c3c8-4935-476d-b2a6-bf7e4374ee47</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9391c3c8-4935-476d-b2a6-bf7e4374ee47</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <title>Lifting T out of Task&lt;T&gt; with dynamic dispatch</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/11/01/LiftingTOutOfTaskTWithDynamicDispatch.aspx</link>
      <pubDate>Sun, 01 Nov 2009 21:49:28 GMT</pubDate>
      <description>&lt;p&gt;
   Say you've got a Task&amp;lt;T&amp;gt;.&amp;nbsp; Well, now what?
&lt;/p&gt;
&lt;p&gt;
   You know that eventually a T will become available, but until then you're out of luck.&amp;nbsp;
   You could go ahead and be a naughty little devil by calling Wait on it -- blocking
   the current thread (eek!) -- or you could call ContinueWith on the task to get back
   a new Task&amp;lt;U&amp;gt;, representing the work you &lt;em&gt;would&lt;/em&gt; do to create some new
   U object if only you presently had a T in hand.&amp;nbsp; And then perhaps you will find
   yourself in the same situation for that U.
&lt;/p&gt;
&lt;p&gt;
   These are those dataflow graphs I mentioned in the previous blog post.&amp;nbsp; Things
   of beauty.
&lt;/p&gt;
&lt;p&gt;
   To be more concrete about the situation I describe, imagine you've got the following
   IFoo interface:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;interface
   IFoo&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string
   Baz(int x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Now, given a Task&amp;lt;IFoo&amp;gt;, you can't do anything related to an IFoo.&amp;nbsp; And
   yet presumably that's why you've got the task in the first place: because you care
   about the IFoo.&amp;nbsp; What if you ultimately want to invoke the Bar method, for example?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;IFoo&amp;gt;
   task = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   You can of course block the thread:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option A: block the thread.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   resultA = task.Result.Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Or you can choose to program in a very clunky way:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option B: use dataflow.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   resultB = task.ContinueWith(t =&amp;gt; t.Result.Bar());&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But what if, instead, you could do something like this?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option C: magic.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   resultC = task.Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Whoa, wait a minute.&amp;nbsp; We're calling Bar() on a Task&amp;lt;IFoo&amp;gt;?&amp;nbsp; Neat,
   but how can that be?
&lt;/p&gt;
&lt;p&gt;
   This is obviously a trick.&amp;nbsp; All of the members of T are somehow being made available
   on the Task&amp;lt;T&amp;gt; object, so that they can be called before the task has actually
   been resolved to a concrete value.&amp;nbsp; Of course, were we to allow this, what you
   get back to represent the result of such calls would need to be task objects too:
   hence we get back a Task&amp;lt;int&amp;gt; from the call on Bar(), instead of an int.&amp;nbsp;
   This is similar to call streams in Barbara Liskov's Argus language (her primary focus
   immediately after CLU).
&lt;/p&gt;
&lt;p&gt;
   This kind of lifting from the inner type outward is much like what you get in languages
   that allow generic mixins.&amp;nbsp; C# already has one semi-such type, though you may
   not realize it: Nullable&amp;lt;T&amp;gt; actually allows you to directly access interfaces
   implemented by T without needing to call Value on it.&amp;nbsp; It's almost like Nullable&amp;lt;T&amp;gt;
   was defined as deriving from T itself which is clearly not actually possible (for
   numerous reasons, not the least significant of which is that it's a struct).&amp;nbsp;
   Try it.&amp;nbsp; This works because the type system treats Nullable&amp;lt;T&amp;gt; and T somewhat
   uniformly (though you'd be surprised by some dangers lurking within -- effectively
   Nullable&amp;lt;T&amp;gt; mustn't implement any interfaces *ever* otherwise a type hole would
   result).&amp;nbsp; But I digress...
&lt;/p&gt;
&lt;p&gt;
   Unfortunately without deep language changes we can't get this to work the way we'd
   like.&amp;nbsp; I have found numerous occasions where a general lifting capability in
   C# would be useful: Lazy&amp;lt;T&amp;gt; is but one example.&amp;nbsp; That said, each time we
   run across an instance, it demands slightly different type system treatment, and it
   seems unlikely such a general facility would be as usable as the one off features.
&lt;/p&gt;
&lt;p&gt;
   Type systems aside, I am actually using a very dirty trick to make this work: I'm
   using the new System.Dynamic features in .NET 4.0 to do it all dynamically.&amp;nbsp;
   You may love or hate this, depending on your stance on type systems.&amp;nbsp; Being an
   ML guy, I'll let you figure out what I think.&amp;nbsp; (Hint: gross hack!)
&lt;/p&gt;
&lt;p&gt;
   We can go further.&amp;nbsp; (Although sadly I won't demonstrate how to do so in this
   blog post.&amp;nbsp; I had wanted to go all the way, but need to get some actual language
   work done today, in addition to a little Riemann study, instead of having endless
   fun tinkering with Visual Studio 2010.&amp;nbsp; Shucks.)&amp;nbsp; Notice that Baz accepts
   an int as input.&amp;nbsp; Well, what if all we've got is a Task&amp;lt;int&amp;gt;?&amp;nbsp; We
   can of course also allow that to get passed in too:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   resultD = task.Baz(42); // Real input.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Fine.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   arg = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   resultE = task.Baz(arg); // A task as input!&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Cool!&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But wait, there is more!&amp;nbsp; It slices and dices too.&amp;nbsp; The next trick is difficult
   -- if not impossible -- to do without far reaching language changes.&amp;nbsp; But we
   could also even bridge the world of ordinary methods too, not just those that have
   been accessed by tunneling through a Task&amp;lt;T&amp;gt;.&amp;nbsp; For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string
   f(int x) {...}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   task = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   result = f(task);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Not to even mention:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   x = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   y = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   z = x + y;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;div&gt;
   &lt;p&gt;
      This is deep.&amp;nbsp; What we are saying is that anywhere a T is expected, we can supply
      a Task&amp;lt;T&amp;gt;.&amp;nbsp; Of course once we've entered the world of tasks, we cannot
      escape until values actually begin resolving.&amp;nbsp; So when we invoke the method f
      in this example, we of course get back a Task&amp;lt;string&amp;gt; for its result.&amp;nbsp;
      Once we've stepped onto a turtle's back, well, it's turtles all the way down.
   &lt;/p&gt;
   &lt;p&gt;
      (Which reminds me of the well known tale:
   &lt;/p&gt;
   &lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
   &lt;p&gt;
      &lt;em&gt;A well-known scientist (some say it was Bertrand Russell) once gave a public lecture
      on astronomy. He described how the earth orbits around the sun and how the sun, in
      turn, orbits around the center of a vast collection of stars called our galaxy. At
      the end of the lecture, a little old lady at the back of the room got up and said:
      "What you have told us is rubbish. The world is really a flat plate supported on the
      back of a giant tortoise." The scientist gave a superior smile before replying, "What
      is the tortoise standing on?" "You're very clever, young man, very clever", said the
      old lady. "But it's turtles all the way down!"&lt;/em&gt;
   &lt;/p&gt;
   &lt;/blockquote&gt; 
   &lt;p&gt;
      Tasks are not greasy hamburgers after all, as I had claimed in the last post, but
      rather they are turtles.
   &lt;/p&gt;
   &lt;p&gt;
      I've wasted all of my energy speaking of turtle hamburgers drenched in asynchronous
      aioli, and have left only a little to go over the hacked up implementation of this
      idea.&amp;nbsp; Sigh.&amp;nbsp; Well, we had better get to it.)
   &lt;/p&gt;
   &lt;p&gt;
      In summary: we'll just rely on dynamic dispatch to do the lifting, thanks to the new
      .NET 4.0 DynamicObject class.&amp;nbsp; This is wildly less efficient than a proper type
      system design would yield, not to mention the utter lack of static type checking.&amp;nbsp;
      Of course a proper implementation that designed for this from Day One would also avoid
      the tremendous amount of object allocation that relying on the current Task&amp;lt;T&amp;gt;
      objects and ContinueWith overloads imply.&amp;nbsp; But nevertheless, this approach will
      allow us to at least have a good ole' time and stimulate the creative side of the
      noggin.
   &lt;/p&gt;
   &lt;p&gt;
      First, I shall provide an extension method for getting a DynamicTask&amp;lt;T&amp;gt; -- the
      thing that actually derives from DynamicObject and implements the custom dynamic binding:
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
      static class DynamicTask&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      static dynamic AsDynamic&amp;lt;T&amp;gt;(this Task&amp;lt;T&amp;gt; task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicTask&amp;lt;T&amp;gt;(task);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Notice that this changes our calling conventions ever so slightly.&amp;nbsp; Namely:
   &lt;/p&gt;
   &lt;p&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
      Option C: magic.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
      resultC = task.AsDynamic().Bar();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      The AsDynamic places the caller into the lifted context.&amp;nbsp; As invocations are
      made, the results become real tasks, and not dynamic ones, such that to continue the
      calling will require many AsDynamic()s.&amp;nbsp; This is a minor inconvenience and we
      could certainly automatically wrap the return values in DynamicTask&amp;lt;T&amp;gt; objects
      if we wanted to eliminate this problem, i.e. to make chaining less verbose.
   &lt;/p&gt;
   &lt;p&gt;
      Second, we must implement the DynamicTask&amp;lt;T&amp;gt; class.&amp;nbsp; We will do a very
      simple translation.&amp;nbsp; Given a member access expression 'x.m', where m is either
      a field or property of type U, we will morph this into the new expression 'x.Task.ContinueWith(v
      =&amp;gt; v.Result.m)', which is of type Task&amp;lt;U&amp;gt;.&amp;nbsp; Similarly, given a method
      invocation 'x.M(a1,...,aN)', whose return value is of type U, we will morph it into
      the new expression 'x.Task.ContinueWith(v =&amp;gt; v.Result.M(a1,...,aN))', which is
      of type Task&amp;lt;U&amp;gt; (or just Task if U is the void type).&amp;nbsp; To support the ability
      to pass a task argument where an actual one is expected would require packing the
      argument with the target into an array, and doing a ContinueWhenAll on it.
   &lt;/p&gt;
   &lt;p&gt;
      (Perhaps I will illustrate how to do these other tricks in a later post, but I'm tight
      for time right now.&amp;nbsp; I'm only sketching the general idea.&amp;nbsp; Even in what
      I show below, things will be incomplete, because topics such as getting exception
      propagation right when tasks begin failing are tricky.&amp;nbsp; Ideally the whole dataflow
      chain will be "broken" by such an exception.&amp;nbsp; Additionally, I've only implemented
      what was necessary to get a few interesting examples working.&amp;nbsp; The binder, for
      example, certainly has a few loose ends.&amp;nbsp; Blog reader beware.)
   &lt;/p&gt;
   &lt;p&gt;
      Here is the implementation of DynamicTask&amp;lt;T&amp;gt;:
   &lt;/p&gt;
   &lt;p&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
      class DynamicTask&amp;lt;T&amp;gt; : DynamicObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      Task&amp;lt;T&amp;gt; m_task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      DynamicTask(Task&amp;lt;T&amp;gt; task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (task == null) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new ArgumentNullException("task");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_task
      = task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      Task&amp;lt;T&amp;gt; Task {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;get
      { return m_task; }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject GetMetaObject(Expression parameter) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (parameter == null) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("parameter");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new TaskLiftedObject(this, parameter);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;class
      TaskLiftedObject : DynamicMetaObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Simple.&amp;nbsp; All of the dynamic magic resides in the implementation of TaskLiftedObject,
      which derives from the DynamicMetaObject class.&amp;nbsp; It is constructed with an instance
      of the DynamicTask&amp;lt;T&amp;gt; along with the expression tree that can be used to dynamically
      load up an instance of that task.&amp;nbsp; All of the dynamic features work with expression
      trees.&amp;nbsp; For example, in response to an attempt to invoke a method M on a DynamicTask&amp;lt;T&amp;gt;,
      our binder will need to find the right method M on the underlying T, and then return
      an expression tree that does the ContinueWith and so forth.
   &lt;/p&gt;
   &lt;p&gt;
      Let's start cracking open TaskLiftedObject:
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;class
      TaskLiftedObject : DynamicMetaObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      DynamicTask&amp;lt;T&amp;gt; m_task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      TaskLiftedObject(DynamicTask&amp;lt;T&amp;gt; task, Expression expression) :&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;base(expression,
      BindingRestrictions.Empty, task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_task
      = task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      We will override two of DynamicMetaObject's functions.&amp;nbsp; BindGetMember is called
      when a member is accessed (like a property or field), whereas BindInvokeMember is
      called when a method call is made.&amp;nbsp; There are several other methods that a proper
      binder would need to override in order to make delegate dispatch and such work properly.&amp;nbsp;
      But this suffices to get started:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject BindGetMember(GetMemberBinder binder)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      We have a member access:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.m&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      which must become:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Task.ContinueWith(v
      =&amp;gt; { v.Result.m; })&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicMetaObject(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;MakeContinuationTask(Bind(binder.Name,
      -1), null),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BindingRestrictions.GetInstanceRestriction(Expression,
      Value),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Value&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[]
      args)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      We have a call:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Foo(a1,...,aN)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      which must become:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Task.ContinueWith(v
      =&amp;gt; { v.Result.Foo(a1,...,aN); })&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression[]
      argsEx = new Expression[args.Length];&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
      (int i = 0; i &amp;lt; args.Length; i++) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;argsEx[i]
      = args[i].Expression;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicMetaObject(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MakeContinuationTask(Bind(binder.Name,
      binder.CallInfo.ArgumentCount), argsEx),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BindingRestrictions.GetInstanceRestriction(Expression,
      Value),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Value&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Clearly the workhorses here are Bind and MakeContinuationTask.&amp;nbsp; Bind is responsible
      for performing dynamic lookup for a matching member on T that has the requested Name
      and, if a method call is being made, the proper number of parameters.&amp;nbsp; For brevity,
      I've omitted anything to do with argument type checking, an obvious hole that we'd
      want to fix some day:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      static MemberInfo Bind(string name, int argCount)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      Lookup the target member on the T, rather than the (Dynamic)Task&amp;lt;T&amp;gt;.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(from
      m in typeof(T).GetMembers(BindingFlags.Instance | BindingFlags.Public)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;where
      m.Name.Equals(name) &amp;amp;&amp;amp;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;(argCount
      == -1 ?&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;!(m
      is MethodInfo) :&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;((MethodInfo)m).GetParameters().Length
      == argCount)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;select
      m).&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Single();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Nothing too interesting here either -- just a bit of hacky reflection code done with
      a fancy LINQ query.&amp;nbsp; If anything other than exactly one method was found, the
      call to Single() will throw an exception.&amp;nbsp; If you want to see what a "real" dynamic
      binder looks like, you won't find it here: check out VB's or IronPython's.
   &lt;/p&gt;
   &lt;p&gt;
      Now for the meat.&amp;nbsp; The MakeContinuationTask method takes the target member that
      we've found dynamically via Bind, as well as an optional array of expression trees,
      each representing an argument being passed to the target method (and which will be
      null for property and field access), and manufactures the expression tree that represents
      the execution of the dynamic call itself:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      Expression MakeContinuationTask(MemberInfo target, Expression[] targetArgs)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
      lambdaParam = Expression.Parameter(typeof(Task&amp;lt;T&amp;gt;), "v");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
      lambdaParamResult = Expression.Property(lambdaParam, "Result");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression
      lambdaBody;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Type
      lambdaReturnType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (target is MethodInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Call(lambdaParamResult, (MethodInfo)target, targetArgs);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((MethodInfo)target).ReturnParameter.ParameterType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      if (target is PropertyInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Property(lambdaParamResult, (PropertyInfo)target);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((PropertyInfo)target).PropertyType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      if (target is FieldInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Field(lambdaParamResult, (FieldInfo)target);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((FieldInfo)target).FieldType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("Unsupported dynamic invoke: " + target.GetType().Name);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      Expression.Call(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Property(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Convert(this.Expression,
      typeof(DynamicTask&amp;lt;T&amp;gt;)),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;typeof(DynamicTask&amp;lt;T&amp;gt;).GetProperty("Task")&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GetContinueWith(lambdaReturnType),
      // ContinueWith&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new
      Expression[] {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      v =&amp;gt; { v.Result.M(a0,...,aN) }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Lambda(lambdaBody,
      lambdaParam)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      You should be able to convince yourself that this code generates the desired transformation
      described earlier.&amp;nbsp; It uses a method to find the overload of Task&amp;lt;T&amp;gt;.ContinueWith
      that we want to bind against, and invokes that on the Task&amp;lt;T&amp;gt; contained within
      the DynamicTask&amp;lt;T&amp;gt; against which the dynamic call was made.&amp;nbsp; It is rather
      unfortunate that the CLR does not allow the void type as a generic type argument,
      so we have to be a little bit inconsistent with our treatment of void returns, by
      choosing a different ContinueWith overload.
   &lt;/p&gt;
   &lt;p&gt;
      If the above reflection code was called hacky, the ContinueWith lookup is worse.&amp;nbsp;
      It's very inefficient, not to mention fragile (because it depends on the current layout
      of Task&amp;lt;T&amp;gt;'s overloads, what with instantiating generic methods and the like).&amp;nbsp;
      C'est la vie:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      static MethodInfo GetContinueWith(Type returnType)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      @TODO: caching to avoid expensive lookups each time.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (returnType == typeof(void)) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      typeof(Task&amp;lt;T&amp;gt;).GetMethod(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;"ContinueWith",&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new
      Type[] { typeof(Action&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;) }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;foreach
      (MethodInfo mif in typeof(Task&amp;lt;T&amp;gt;).GetMethods()) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (mif.Name == "ContinueWith" &amp;amp;&amp;amp; mif.IsGenericMethodDefinition) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MethodInfo
      mifOfT = mif.MakeGenericMethod(returnType);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ParameterInfo[]
      mifParams = mifOfT.GetParameters();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (mifParams.Length == 1 &amp;amp;&amp;amp;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;mifParams[0].ParameterType
      == typeof(Func&amp;lt;,&amp;gt;).MakeGenericType(typeof(Task&amp;lt;T&amp;gt;), returnType)) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      mifOfT;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("Fatal error: ContinueWith overload not found");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      And that's it.&amp;nbsp; With that, we can get dynamic invocations on unresolved T's via
      Task&amp;lt;T&amp;gt; objects.&amp;nbsp; Nifty.
   &lt;/p&gt;
   &lt;p&gt;
      I'm not saying any of this is a really good idea.&amp;nbsp; Honestly, I'm not.&amp;nbsp; Of
      course, there's a kernel of a good idea there and the systems we are working on take
      this kernel to its extreme.&amp;nbsp; By providing a programming model that encourages
      deep chains of datafow to be expressed speculatively in a natural and familiar manner,
      greater degrees of latent parallelism can lie resident in an application waiting to
      be unlocked as more processors become available.&amp;nbsp; Doing it for real requires
      impactful changes to the language, supporting infrastructure, and particularly tooling.&amp;nbsp;
      Just imagine what it means to break into a debugger to inspect deep dataflow graphs
      that have been constructed by compiler magic underneath you.&amp;nbsp; And the use of
      ContinueWith is a little lame, because of course the target of our call may be something
      that can be run speculatively too with first class pipleining, rather than completely
      delaying the invocation of it.
   &lt;/p&gt;
   &lt;p&gt;
      So we won't be seeing lifted tasks in .NET anytime soon.&amp;nbsp; Writing up this blog
      post was merely an excuse to toy around with the new C# dynamic features and to have
      a little recreational time.&amp;nbsp;&amp;nbsp; And to generate excitement about what .NET
      4.0 holds in store.&amp;nbsp; I hope you have enjoyed it.&amp;nbsp; Now back to reality.
   &lt;/p&gt;
   &gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=9391c3c8-4935-476d-b2a6-bf7e4374ee47" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=a8cbba20-d3d3-44e5-9b3e-896387803a44</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=a8cbba20-d3d3-44e5-9b3e-896387803a44</wfw:commentRss>
      <title>Tasks and asynchronous control flow</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/11/01/TasksAndAsynchronousControlFlow.aspx</link>
      <pubDate>Sun, 01 Nov 2009 04:06:24 GMT</pubDate>
      <description>&lt;p&gt;
   Well, Visual Studio 2010 Beta 2 is &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;out
   on the street&lt;/a&gt;.&amp;nbsp; It contains plenty of neat new things to keep one busy for
   at least a rainy Saturday.&amp;nbsp; I proved this today.
&lt;/p&gt;
&lt;p&gt;
   Of course, Parallel Extensions is in the box.&amp;nbsp; .NET 4.0's Task and Task&amp;lt;T&amp;gt;
   abstractions are used&amp;nbsp;to implement such things as PLINQ and Parallel.For loops,
   but of course they are&amp;nbsp;great for representing asynchronous work too.&amp;nbsp; The
   FromAsync adapters move you from the dark ages of IAsyncResult to the glitzy new space
   age of tasks.
&lt;/p&gt;
&lt;p&gt;
   Not only are tasks tastier than hamburgers, but they enable complex dataflow graphs
   of asynchronous work to unfold dynamically at runtime, thanks to the ContinueWith
   method.&amp;nbsp; From a Task&amp;lt;T&amp;gt; you can get a Task&amp;lt;U&amp;gt;&amp;nbsp;that was computed
   based on the T; ad infinitum.&amp;nbsp;&amp;nbsp;We like dataflow.&amp;nbsp; It is the key to
   unlocking parallelism, or more accurately,&amp;nbsp;boiling away all else &lt;em&gt;except for&lt;/em&gt; dataflow&amp;nbsp;is
   the key.&amp;nbsp; But what about control flow, you might ask?&amp;nbsp; We like it less.&amp;nbsp;
   But you can do it, so long as you put in some work.&amp;nbsp; F#'s async workflows make
   this sort of thing a tad easier, but the raw libraries in .NET 4.0 don't come with
   any sort of loops or conditional capabilities.&amp;nbsp; Perhaps in the future they will.&amp;nbsp;
   Nevertheless, in this post I shall demonstrate how to build a couple simple ones.
&lt;/p&gt;
&lt;p&gt;
   Not because the lack of them is going to cause unprecidented and unheard of horrors,
   but rather because in doing so we'll see some neat features of tasks.
&lt;/p&gt;
&lt;p&gt;
   The two methods I will illustrate in this post are:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;public
   static class TaskControlFlow&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;{&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;public
   static Task For(int from, int to, Func&amp;lt;int, Task&amp;gt; body, int width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static Task While(Func&amp;lt;int, bool&amp;gt; condition, Func&amp;lt;int, Task&amp;gt; body, int
   width)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Notice that each body is given the iteration index and is expected to launch asynchronous
   work and return a Task.&amp;nbsp; The parameters that these methods take are probably
   obvious.&amp;nbsp; Well, except for the last one.&amp;nbsp; The "width" indicates how many
   outstanding asynchronous bodies should be in flight at once.&amp;nbsp; The Task returned
   by For and While won't be considered done until all iterations are done, and any exceptions
   will be propagated as you might hope.&amp;nbsp; It would be pretty useless otherwise.
&lt;/p&gt;
&lt;p&gt;
   For example, we could write a while loop that does something very silly:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.While(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;i
   =&amp;gt; i &amp;lt; 100,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;i
   =&amp;gt; { return CreateTimerTask(250).ContinueWith(_ =&amp;gt; Console.WriteLine(i)); },&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;4&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This just prints returns a "timer task" that completes after 250ms and prints out
   the iteration to the console. We pass a width of 4, so only four tasks will be outstanding
   at any given time.&amp;nbsp; Notice we call Wait at the end, since both For and While
   return tasks representing the in flight work.&amp;nbsp; This could have instead been written
   using a For loop as follows:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.For(0,
   100,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;i
   =&amp;gt; { return CreateTimerTask(250).ContinueWith(_ =&amp;gt; Console.WriteLine(i)); },&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;4&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   The CreateTimerTask method, by the way, looks like this:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;private
   static Task CreateTimerTask(int ms)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;new
   Timer(x =&amp;gt; ((TaskCompletionSource&amp;lt;bool&amp;gt;)x).SetResult(true), tcs, ms, -1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return
   tcs.Task;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   As something more realistic, imagine we wanted to do something with a large number
   of files, and don't want to block a whole bunch of threads in the process.&amp;nbsp; The
   following "simple" expression will count up all of the bytes for all of the files
   in a particular directory, without once blocking the thread -- well, except for the
   initial call to Directory.GetFiles:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string
   win = "c:\\...\\";&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string[]
   files = Directory.GetFiles(win);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   total = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.For(0,
   files.Length,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   i =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool
   eof = false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   offset = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;byte[]
   buff = new byte[4096];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;FileStream
   fs = File.OpenRead(files[i]);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return
   TaskControlFlow.While(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;j
   =&amp;gt; !eof,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;j
   =&amp;gt; Task&amp;lt;int&amp;gt;.Factory.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FromAsync&amp;lt;byte[],int,int&amp;gt;(&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   fs.BeginRead, fs.EndRead, buff, offset, buff.Length,&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;null,
   TaskCreationOptions.None&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;).&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ContinueWith(v
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (eof = v.Result &amp;lt; buff.Length) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;fs.Close();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;offset
   += v.Result;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/span&gt;Interlocked.Add(ref total, v.Result);&lt;/font&gt;&lt;/span&gt;&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;},&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;8&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Console.WriteLine(total);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Pretty neat.&amp;nbsp; We've somewhat arbitrarily chosen a width of 8 for this loop.&amp;nbsp;
   And notice something very subtle but important here: we've chosen a width of 1 for
   the inner loop that plows through the bytes of a file.&amp;nbsp; This is because we're
   sharing state, and it would not be safe to launch numerous iterations at once.&amp;nbsp;
   The same byte[], eof variable, and so forth, would become corrupt.&amp;nbsp; I will mention
   in passing that it's unfortunate that we've got that interlocked stuck in there to
   add to the total.&amp;nbsp; Refactoring this so that we could just do a LINQ reduce over
   the whole thing would be nice.&amp;nbsp; Indeed, it can be done.
&lt;/p&gt;
&lt;p&gt;
   We can do away with the For implementation very quickly.&amp;nbsp; It is just implemented
   in terms of While:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
   static Task For(int from, int to, Func&amp;lt;int, Task&amp;gt; body, int width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   While(i =&amp;gt; from + i &amp;lt; to, body, width);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   And it turns out that the While implementation is not terribly complicated either.&amp;nbsp;
   Here it is:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
   static Task While(Func&amp;lt;int, bool&amp;gt; condition, Func&amp;lt;int, Task&amp;gt; body, int
   width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   currIx = -1; // Current shared index.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   currCount = width; // The number of outstanding tasks.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   canceled = 0; // 1 if at least one body was cancelled.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ConcurrentBag&amp;lt;Exception&amp;gt;
   exceptions = null; // A collection of exceptions, if any.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//
   Generate a continuation action: this fires for each body that completes.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Action&amp;lt;Task&amp;gt;
   fcont = null;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;fcont
   = tsk =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (tsk.IsFaulted) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Accumulate exceptions.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LazyInitializer.EnsureInitialized(ref
   exceptions);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;foreach
   (Exception inner in tsk.Exception.InnerExceptions) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;exceptions.Add(inner);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (tsk.IsCanceled) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Mark that cancellation has occurred.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;canceled
   =&amp;nbsp;1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (canceled == 0 &amp;amp;&amp;amp; exceptions == null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//
   If no cancellations / exceptions are found, attempt to kick off more work.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   ix = Interlocked.Increment(ref currIx);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (condition(ix)) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Generate a new body task, handling exceptions.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Then
   make sure we&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   tack on the continuation on that new task, so we can keep on going...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   If the condition yielded 'false', we'll simply fall through and try to finish.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task
   btsk;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk
   = body(ix);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch
   (Exception ex) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk
   = AlreadyFaulted(ex);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk.ContinueWith(fcont);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   If this is the last task, signal completion.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (Interlocked.Decrement(ref currCount) == 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (exceptions != null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetException(exceptions);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (canceled == 1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetCanceled();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetResult(true);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Fire off the right number of starting tasks.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; width; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AlreadyDone.ContinueWith(fcont);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   I've commented the code inline to illustrate what is going on.&amp;nbsp; The only other
   part that isn't shown are the AlreadyDone and AlreadyFaulted members, which simply
   give Tasks that are already in a final state.&amp;nbsp; This isn't strictly necessary,
   but come in handy in a number of situations:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;internal
   static Task AlreadyDone;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;static
   TaskControlFlow()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetResult(true);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AlreadyDone
   = tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;private
   static Task AlreadyFaulted(Exception ex)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetException(ex);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   And that's it.&amp;nbsp; I'm done for now.&amp;nbsp; Hope you enjoyed it.&amp;nbsp; I've got a
   few other posts in the works --&amp;nbsp;primarily the result of a day full of hacking
   (I got in the office at 7am this morning, and have been here ever since, 14 hours
   later) --&amp;nbsp;demonstrating how to do speculative asynchronous work for if/else branches.&amp;nbsp;
   Finally, I also have a neat example that illustrates how to do deep dataflow-based
   speculation without having to wait for work to complete.&amp;nbsp; This combines the new
   .NET 4.0 dynamic capabilities with parallelism, so I'm pretty excited to get it working
   and write about it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=a8cbba20-d3d3-44e5-9b3e-896387803a44" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=b18b1acb-e992-49e7-a7a8-c7ce146006f0</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,b18b1acb-e992-49e7-a7a8-c7ce146006f0.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,b18b1acb-e992-49e7-a7a8-c7ce146006f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=b18b1acb-e992-49e7-a7a8-c7ce146006f0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      My team is hiring.  For example:
   </p>
        <p>
          <a href="https://careers.microsoft.com/JobDetails.aspx?jid=7594&amp;lang=en">https://careers.microsoft.com/JobDetails.aspx?jid=7594&amp;lang=en</a>
        </p>
        <p>
      As the job description says, we are focused mainly concurrency &amp; parallelism
      at each layer of the system: kernel-mode, user-mode, frameworks, languages, compilers,
      ...
   </p>
        <p>
      The sky is the limit and nothing is off the table.  I've never had so much fun
      in my life as I am having right now, and couldn't imagine a better <a href="http://blogs.msdn.com/cbrumme/">g</a><a href="http://en.wikipedia.org/wiki/Pavel_Curtis">r</a><a href="http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/t/Tarditi:David.html">o</a><a href="http://srl.cs.jhu.edu/~shap/">u</a><a href="http://steensgaard.org/bjarne/">p</a> of <a href="http://texteditors.org/cgi-bin/wiki.pl?action=browse&amp;diff=1&amp;id=TurboText&amp;diffrevision=5">p</a><a href="http://blog.robjsoftware.org/">e</a><a href="http://www.ps.uni-sb.de/~kornstae/">o</a><a href="http://blogs.msdn.com/danlehen/">p</a><a href="http://blogs.msdn.com/slavao/">l</a><a href="http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/t/Tribble:Eric_Dean.html">e</a> to
      do it with.  I think this is what NT <a href="http://www.amazon.com/Show-Stopper-Breakneck-Generation-Microsoft/dp/0029356717">must
      have felt like</a>.
   </p>
        <p>
      If you're interested, email me at joedu AT microsoft DOT com, or apply via that link.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=b18b1acb-e992-49e7-a7a8-c7ce146006f0" />
      </body>
      <title>Jobs</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,b18b1acb-e992-49e7-a7a8-c7ce146006f0.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/10/31/Jobs.aspx</link>
      <pubDate>Sat, 31 Oct 2009 21:02:15 GMT</pubDate>
      <description>&lt;p&gt;
   My team is hiring.&amp;nbsp; For example:
&lt;/p&gt;
&lt;p&gt;
   &lt;a href="https://careers.microsoft.com/JobDetails.aspx?jid=7594&amp;amp;lang=en"&gt;https://careers.microsoft.com/JobDetails.aspx?jid=7594&amp;amp;lang=en&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
   As the job description says,&amp;nbsp;we are focused&amp;nbsp;mainly concurrency &amp;amp; parallelism
   at each layer of the system: kernel-mode, user-mode, frameworks, languages, compilers,
   ...
&lt;/p&gt;
&lt;p&gt;
   The sky is the limit and nothing is off the table.&amp;nbsp; I've never had so much fun
   in my life as I am having right now, and couldn't imagine a better &lt;a href="http://blogs.msdn.com/cbrumme/"&gt;g&lt;/a&gt;&lt;a href="http://en.wikipedia.org/wiki/Pavel_Curtis"&gt;r&lt;/a&gt;&lt;a href="http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/t/Tarditi:David.html"&gt;o&lt;/a&gt;&lt;a href="http://srl.cs.jhu.edu/~shap/"&gt;u&lt;/a&gt;&lt;a href="http://steensgaard.org/bjarne/"&gt;p&lt;/a&gt; of &lt;a href="http://texteditors.org/cgi-bin/wiki.pl?action=browse&amp;amp;diff=1&amp;amp;id=TurboText&amp;amp;diffrevision=5"&gt;p&lt;/a&gt;&lt;a href="http://blog.robjsoftware.org/"&gt;e&lt;/a&gt;&lt;a href="http://www.ps.uni-sb.de/~kornstae/"&gt;o&lt;/a&gt;&lt;a href="http://blogs.msdn.com/danlehen/"&gt;p&lt;/a&gt;&lt;a href="http://blogs.msdn.com/slavao/"&gt;l&lt;/a&gt;&lt;a href="http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/t/Tribble:Eric_Dean.html"&gt;e&lt;/a&gt; to
   do it with.&amp;nbsp; I think this is what NT &lt;a href="http://www.amazon.com/Show-Stopper-Breakneck-Generation-Microsoft/dp/0029356717"&gt;must
   have felt like&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   If you're interested, email me at joedu AT microsoft DOT com, or apply via that link.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=b18b1acb-e992-49e7-a7a8-c7ce146006f0" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,b18b1acb-e992-49e7-a7a8-c7ce146006f0.aspx</comments>
      <category>Miscellaneous</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=c261af7d-d737-4802-b8af-3809124fb61e</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,c261af7d-d737-4802-b8af-3809124fb61e.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,c261af7d-d737-4802-b8af-3809124fb61e.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=c261af7d-d737-4802-b8af-3809124fb61e</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <title>False sharing is no fun</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,c261af7d-d737-4802-b8af-3809124fb61e.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/10/20/FalseSharingIsNoFun.aspx</link>
      <pubDate>Tue, 20 Oct 2009 00:59:20 GMT</pubDate>
      <description>&lt;p&gt;
   Embarrassingly, I neglected to write about the oldest trick in the book in &lt;a href="http://www.bluebytesoftware.com/blog/2009/10/05/FastSynchronizationBetweenASingleProducerAndSingleConsumer.aspx"&gt;my
   last post&lt;/a&gt;: designing the producer/consumer data structure to reduce false sharing.&amp;nbsp;
   As I've written about several times previously (e.g. &lt;a href="http://www.bluebytesoftware.com/blog/2009/02/21/AMoreScalableReaderwriterLockAndABitLessHarshConsiderationOfTheIdea.aspx"&gt;see
   here&lt;/a&gt;), and more so &lt;a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html"&gt;in
   the book&lt;/a&gt;, false sharing is always deadly and must be avoided.
&lt;/p&gt;
&lt;p&gt;
   As a simple example, consider a program that merely increments a shared counter over
   and over again.&amp;nbsp; If we give P threads their own separate counters, and ask them
   to increment the respective counter an equal number of times.&amp;nbsp;&amp;nbsp;Each thread
   can of course do this without synchronization, because the counters are distinct:
   no locks or even interlocked operations are necessary.&amp;nbsp; Naively, one might expect
   that running P of them in parallel leads to no interference, and hence perfect parallelization.&amp;nbsp;
   However, when I run a little benchmark on my 8-way machine, the numbers for increasing
   values of P tell a very different story:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;1
   = 22425789&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 42023726&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(187%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;4
   = 175828522 &lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/span&gt;(784%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;8
   = 333906288&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;(1489%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   It is clear that the throughput drops dramatically as P increases.&amp;nbsp; The reason?&amp;nbsp;
   Each counter, being only 8 bytes wide, shares a cache line with as many as 7 other
   counters -- or 15 if we're on a machine with 128 byte cache lines.&amp;nbsp; A simple
   change to the counter's layout, so that individual counters do not share the same
   cache line, will remedy the situation.&amp;nbsp; The numbers improve dramatically.&amp;nbsp;
   In fact, they remain constant no matter the value of P:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;1
   = 21914250&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 21900392&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;4
   = 21865781&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;8
   = 21934008&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This perfect scaling isn't always possible due to memory bandwidth, but because we're
   just incrementing a single counter per core this doesn't manifest as a problem.
&lt;/p&gt;
&lt;p&gt;
   For what it's worth, the machine I am running these tests on is an 8-way, dual-socket,
   quad-core.&amp;nbsp; Pairs of cores share an L1 cache, and all cores in a socket share
   an L2 cache.&amp;nbsp; So the pairs {0,1}, {2,3}, {4,5}, and {6,7} are each expected to
   have distinct L1 caches and the groups {0,1,2,3} and {4,5,6,7} are expect to have
   distinct L2 caches.&amp;nbsp; The 2 number above is run with two threads affinitized such
   that they share the same L1 cache.&amp;nbsp; If we force them apart,&amp;nbsp;however,&amp;nbsp;we
   get slightly different results:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 42023726&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;(187%) -- same L1 cache&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 54706505&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(244%) -- same L2 cache&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 75030977&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(335%) -- separate sockets&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   As expected, the more distance in the cache hierarchy, the greater the slowdown due
   to the increased ping pong paths.
&lt;/p&gt;
&lt;p&gt;
   The specific results are of course unique to my machine, but nevertheless the conclusion
   is clear: reducing sharing leads to substantial performance gains, particularly with
   large numbers of threads hammering on the shared lines.&amp;nbsp; Often more so than eliminating
   other sources of wasted cycles, like interlocked operations.&amp;nbsp; Eliminating those
   sources is clearly important too, but it really is amazing how deadly and yet difficult
   to discover false sharing can be: few cases are as obvious as this one.
&lt;/p&gt;
&lt;p&gt;
   One aside is worth mentioning before winding down.&amp;nbsp; When I first ran this experiment,
   I had done it two ways: (1) with fields of a shared object, then using StructLayout(LayoutKind=Explicit)
   to keep fields apart, and (2) with counters crammed into an array, which then contains
   padding elements to eliminate the false sharing.&amp;nbsp; The former is shown above.&amp;nbsp;
   If you try the latter, you may be surprised.&amp;nbsp; The layout of arrays on the CLR
   is such that an array's length resides before the first element.&amp;nbsp; So unless you
   pad the first element of the array, all accesses will perform bounds checking that
   touches the first element's line.&amp;nbsp; Because this line is being mutated by the
   thread incrementing the first counter, terrible false sharing results.&amp;nbsp; To solve
   this, we must pad the first element too.
&lt;/p&gt;
&lt;p&gt;
   For example, here are the array numbers with false sharing:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;1
   = 27366202&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 125264714&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/span&gt;(458%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;4
   = 1383953372&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/span&gt;(7969%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;8
   = 3136996731&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/span&gt;(11463%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Notice the P = 8 case is over 100x slower!&amp;nbsp; Yowzas.&amp;nbsp; After fixing things,
   with the first element padded, we again observe perfect scaling:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;1
   = 27393869&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;2
   = 27465999&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;4
   = 27370901&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;8
   = 27408631&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;(100%)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Clearly false sharing is not merely a theoretical concern.&amp;nbsp; In fact, during our
   Beta1 performance milestone in Parallel Extensions, most of our performance problems
   came down to stamping out false sharing in numerous places: the partitioning logic
   of parallel for loops, polling cancellation token flags, enumerators allocated at
   the beginning of a PLINQ query and constantly mutated during its execution, and even
   in our examples (e.g. see &lt;a href="http://www.ddj.com/go-parallel/article/showArticle.jhtml?articleID=217500206"&gt;Herb's
   matrix multiplication example&lt;/a&gt;), etc.&amp;nbsp; It is terribly simple to make a mistake
   and, in a complicated system, terribly difficult to pinpoint the origin of what can
   be a truly crippling scalability bottleneck.
&lt;/p&gt;
&lt;p&gt;
   In the next post, we will go back and take a look at our single-producer / single-consumer
   buffer, and redesign it to have substantially better cache behavior.
&lt;/p&gt;
&lt;p&gt;
   ~
&lt;/p&gt;
&lt;p&gt;
   For reference, here's the basic program used for a lot of these tests:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//#define
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//#define
   USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#pragma
   warning disable 0169&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;using
   System;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;using
   System.Diagnostics;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;using
   System.Runtime.InteropServices;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;using
   System.Threading;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;class
   Program&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;const
   int P = 1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;class
   Counters&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;long[]
   m_longs;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;internal
   Counters(int n) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_longs
   = new long[(n+1)*16];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#else&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_longs
   = new long[n];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Increment(int i) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_longs[(i+1)*16]++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#else&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_longs[i]++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#else
   // USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[StructLayout(LayoutKind.Explicit)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;struct
   Counters&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(0)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long a;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(128)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long b;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(256)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long c;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(384)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long d;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(512)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long e;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(640)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long f;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(768)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long g;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   CACHE_FRIENDLY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[FieldOffset(896)]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   long h;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static
   Counters s_c = new Counters();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif
   // USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Main(string[] args)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   p = int.Parse(args[0]);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;const
   int iterations = int.MaxValue / 4;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ManualResetEvent
   mre = new ManualResetEvent(false);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Counters
   c = new Counters(p);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread[]
   ts = new Thread[p];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; ts.Length; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   tid = i;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ts[i]
   = new Thread(delegate() {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SetThreadAffinityMask(GetCurrentThread(),
   new UIntPtr(1u &amp;lt;&amp;lt; tid));&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;mre.WaitOne();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int j = 0; j &amp;lt; iterations; j++)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#if
   USE_ARRAY&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;c.Increment(tid);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#else&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;switch
   (tid) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   0: s_c.a++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   1: s_c.b++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   2: s_c.c++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   3: s_c.d++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   4: s_c.e++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   5: s_c.f++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   6: s_c.g++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;case
   7: s_c.h++; break;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;#endif&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;});&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ts[i].Start();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Stopwatch
   sw = Stopwatch.StartNew();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;mre.Set();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;foreach
   (Thread t in ts) t.Join();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Console.WriteLine(sw.ElapsedTicks);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[System.Runtime.InteropServices.DllImport("kernel32.dll")]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static
   extern IntPtr GetCurrentThread();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[System.Runtime.InteropServices.DllImport("kernel32.dll")]&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static
   extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=c261af7d-d737-4802-b8af-3809124fb61e" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,c261af7d-d737-4802-b8af-3809124fb61e.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=3740daff-a459-4298-bc9b-65d3647f5c0d</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,3740daff-a459-4298-bc9b-65d3647f5c0d.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,3740daff-a459-4298-bc9b-65d3647f5c0d.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=3740daff-a459-4298-bc9b-65d3647f5c0d</wfw:commentRss>
      <slash:comments>12</slash:comments>
      <title>Fast synchronization between a single producer and single consumer</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,3740daff-a459-4298-bc9b-65d3647f5c0d.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/10/05/FastSynchronizationBetweenASingleProducerAndSingleConsumer.aspx</link>
      <pubDate>Mon, 05 Oct 2009 01:03:17 GMT</pubDate>
      <description>&lt;p&gt;
   Commonly two threads must communicate with one another, typically to exchange some
   piece of information.&amp;nbsp; This arises in low-level shared memory synchronization
   as in PLINQ’s asynchronous data merging, in the implementation of higher level patterns
   like message passing,&amp;nbsp;inter-process communication,&amp;nbsp;and in countless other
   situations.&amp;nbsp; If only two agents partake in this arrangement, however, it is possible
   to implement a highly efficient exchange protocol.&amp;nbsp; Although the situation is
   rather special, exploiting this opportunity can lead to some interesting performance
   benefits.
&lt;/p&gt;
&lt;p&gt;
   The standard technique for shared-memory situations&amp;nbsp;is to use a ring buffer.&amp;nbsp;
   This buffer is ordinarily an array of fixed length that may become full or empty.&amp;nbsp;
   The two threads in this arrangement assume the role of producer and consumer: the
   producer adds data to the buffer and may make it full, whereas the consumer removes
   data from the buffer and may make it empty.&amp;nbsp; It is possible to generalize this
   to multi-consumers or multi-producers, with some added cost to synchronization.&amp;nbsp;
   What is described below is for the two thread case.
&lt;/p&gt;
&lt;p&gt;
   We will call this a ProducerConsumerRendezvousBuffer&amp;lt;T&amp;gt;, and its basic structure
   looks like this:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;using
   System;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;using
   System.Threading;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   class ProducerConsumerRendezvousPoint&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   T[] m_buffer;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   volatile int m_consumerIndex;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   volatile int m_consumerWaiting;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   AutoResetEvent m_consumerEvent;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   volatile int m_producerIndex;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   volatile int m_producerWaiting;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   AutoResetEvent m_producerEvent;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   ProducerConsumerRendezvousPoint(int capacity)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (capacity &amp;lt; 2) throw new ArgumentOutOfRangeException("capacity");&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_buffer
   = new T[capacity];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_consumerEvent
   = new AutoResetEvent(false);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_producerEvent
   = new AutoResetEvent(false);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   int Capacity&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;get
   { return m_buffer.Length; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   bool IsEmpty&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;get
   { return (m_consumerIndex == m_producerIndex); }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   bool IsFull&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;get
   { return (((m_producerIndex + 1) % Capacity) == m_consumerIndex); }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Enqueue(T value)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   T Dequeue()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   There are some basic invariants to call out:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      Our buffer holds our elements, producer index says at what position the next element
      enqueued will be stored, and the consumer index says from what position the next request
      to dequeue an element will retrieve its value.&lt;/li&gt;
   &lt;li&gt;
      We reserve one element in our buffer to differentiate between fullness and emptiness.&amp;nbsp;
      This is why we demand that capacity be &amp;gt;= 2.&amp;nbsp; We could alternatively know
      how to distinguish between a free slot and a used one, such as checking for null,
      but keep things simple for now.&lt;/li&gt;
   &lt;li&gt;
      Thus, IsEmpty is true when the consumer and producer index are the same.&amp;nbsp; Whereas
      IsFull is true when the consumer is one ahead of the producer, such that producing
      would make the producer index collide with the consumer index (otherwise leading to
      IsEmpty).&lt;/li&gt;
   &lt;li&gt;
      It should be obvious that our intent is to block consumption when IsEmpty == true
      and production when IsFull == true.&amp;nbsp; This is the point of the waiting flags and
      events.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Now let us look at the implementation first of Enqueue and then Dequeue, paying special
   attention to the necessary synchronization operations.&amp;nbsp; They look nearly identical:
&lt;/p&gt;
&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Enqueue(T value)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (IsFull) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WaitUntilNonFull();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_buffer[m_producerIndex]
   = value;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Interlocked.Exchange(ref
   m_producerIndex, (m_producerIndex + 1) % Capacity);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (m_consumerWaiting == 1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_consumerEvent.Set();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Enqueue begins, as expected, by checking whether the queue is full.&amp;nbsp; Notice that
   we have not yet issued any memory fences yet.&amp;nbsp; The only thread that may make
   the buffer full is the current one, which will obviously not occur before proceeding,
   and therefore we needn’t perform any expensive synchronization operation for this
   check.&amp;nbsp; The value seen may of course be stale but we can deal with that possibility
   inside the slow path, WaitUntilNonFull.&amp;nbsp; We’ll look at that momentarily.
&lt;/p&gt;
&lt;p&gt;
   We then proceed to placing the value in the buffer at the current producer’s index.&amp;nbsp;
   Only the current thread will update the producer index and a consumer will not read
   from the current value so long as the producer index refers to it.&amp;nbsp; The value
   may not even be written atomically, e.g. for T’s that are greater than a pointer sized
   word.&amp;nbsp; This is okay: only the act of incrementing the index allows a consumer
   to access the element in question.&amp;nbsp; Writes on the CLR 2.0 memory model are retired
   in order and the reading side will use an acquire load of the index before accessing
   the element’s words.&amp;nbsp; Indeed we could use complicated multipart value types that
   are comprised of lengthy buffers, header words, and so on.
&lt;/p&gt;
&lt;p&gt;
   We then increment the producer index, handling the possibility of wrap-around by modding
   with the capacity.&amp;nbsp; This uses an Interlocked.Exchange for one simple reason:
   we are about to read a consumer waiting flag, and must prevent the load of that flag
   from moving prior to the producer index write.&amp;nbsp; The consumer sets this flag when
   it notices the queue is empty and waits.&amp;nbsp; This enables us to use a “Dekker style”
   check to minimize synchronization.&amp;nbsp; We could have alternatively just unconditionally
   set the event, doing away with the interlocked operation altogether.&amp;nbsp; But that
   call, if it involves kernel transitions, which is quite likely, is going to be much
   more expensive and would occur on every call to Enqueue.&amp;nbsp; And any event of this
   kind that doesn’t require kernel transitions is going to at least require an interlocked
   operation for the same reason we require one here.&amp;nbsp; An alternative technique
   involves setting when we transition the buffer from empty to non-empty or full to
   non-full, but this wastes a possibly expensive signal if the other party isn't even
   currently waiting.&amp;nbsp; If full&amp;nbsp;or empty is a rare situation, then full or empty
   and with a peer actually physically waiting is even rarer.
&lt;/p&gt;
&lt;p&gt;
   Let’s now look at the WaitUntilNonFull method.&amp;nbsp; It’s really the reverse of what
   the consumer does, so based on everything said till this point, I am guessing it’s
   obvious:
&lt;/p&gt;
&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   void WaitUntilNonFull()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Interlocked.Exchange(ref
   m_producerWaiting, 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   (IsFull) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_producerEvent.WaitOne();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;finally
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_producerWaiting
   = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   We begin by issuing a memory fence and setting the producer waiting flag.&amp;nbsp; This
   memory fence is necessary to advertise that we are about to wait, and also to ensure
   the subsequent check of IsFull is synchronized.&amp;nbsp; The consumer does something
   very much like the producer does (above) after taking an element: if the producer
   is waiting, the consumer has made space for it and therefore must signal.&amp;nbsp; But
   it could be the case that the consumer has already made the queue non-full before
   it could notice the producer’s waiting flag.&amp;nbsp; We catch this by ensuring the producer’s
   check of IsFull cannot go before setting the producer waiting; similarly, the consumer
   cannot make IsFull false without subsequently noticing that the producer is waiting;
   this avoids deadlock.
&lt;/p&gt;
&lt;p&gt;
   Everything else is self explanatory.&amp;nbsp; Well, almost.&amp;nbsp; We need a loop here
   to catch one subtle situation.&amp;nbsp; Imagine a producer enters into this method thinking
   the buffer is full.&amp;nbsp; It sets its flag, and then immediately notices that the
   buffer is not full anymore.&amp;nbsp; A consumer has generated a new item of interest.&amp;nbsp;
   But imagine that consumer noticed that the producer was waiting and hence set its
   event.&amp;nbsp; This is an auto-reset event, so the next time the producer must wait,
   the event will have already been set and it’ll likely wake up before IsFull has become
   true.&amp;nbsp; An alternative way of dealing with this is to call Reset on the event
   if we didn’t actually wait on the event, but again we keep things simple.
&lt;/p&gt;
&lt;p&gt;
   At this point, the consumer side is going to look very familiar:
&lt;/p&gt;
&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   T Dequeue()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (IsEmpty) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WaitUntilNonEmpty();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;T
   value = m_buffer[m_consumerIndex];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_buffer[m_consumerIndex]
   = default(T);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Interlocked.Exchange(ref
   m_consumerIndex, (m_consumerIndex + 1) % Capacity);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (m_producerWaiting == 1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_producerEvent.Set();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   value;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
   void WaitUntilNonEmpty() {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Interlocked.Exchange(ref
   m_consumerWaiting, 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   (IsEmpty) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_consumerEvent.WaitOne();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;finally
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_consumerWaiting
   = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   This is near-identical to Enqueue and WaitUntilNonFull, and so needs little explanation.&amp;nbsp;
   The acquire load inside IsEmpty of the producer index ensures that we observe the
   producer index for this particular value being beyond the current consumer’s index
   before loading the value itself, thereby ensuring we see the whole set of written
   words.&amp;nbsp; The one other thing to point out is that we “null out” the element consumed
   which, for large buffers, helps to avoid space leaks that would have otherwise been
   possible.
&lt;/p&gt;
&lt;p&gt;
   There are certainly some opportunities for improving this.
&lt;/p&gt;
&lt;p&gt;
   For example, we might add a little bit of spinning in the wait cases.&amp;nbsp; This would
   be worthwhile for cases that exchange data at very fast rates and have small buffers,
   meaning that the chance of hitting empty and full conditions is quite high.&amp;nbsp;
   Avoiding the context switch thrashing is likely to lead to hotter caches, because
   threads will remain runnable for longer, and the raw costs of switching themselves.
&lt;/p&gt;
&lt;p&gt;
   Additionally, we technically could use a single event if we wanted to spend the effort.&amp;nbsp;
   We’d have to handle a few tricky cases, however: namely, the case where a producer
   or consumer ends up waiting on an event because it “just missed” the event of interest,
   thus satisfying the event.&amp;nbsp; Indeed both threads could actually end up waiting
   on the event simultaneously and we need to somehow ensure the right one eventually
   gets awakened.&amp;nbsp; This leads to some chatter and probably isn’t worth the added
   complexity.
&lt;/p&gt;
&lt;p&gt;
   Here is a peek at some rough numbers from a little benchmark that has two threads
   enqueuing and dequeuing elements as fast as humanly (or computerly) possible.&amp;nbsp;
   This is a particularly unique and unlikely situation, but stresses the implementation
   in a few interesting ways.&amp;nbsp; In particular, it will stress the contentious slow
   paths; although these are expected to be rarer, the fast paths are just so easy to
   get right in this data structure that they are mostly uninteresting to stress performance-wise.&amp;nbsp;
   There are then a few variants, each based on the original version shown above:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      2 element capacity, which means we’ll be transitioning from empty to full and back
      a lot.&lt;/li&gt;
   &lt;li&gt;
      1024 element capacity, which means we won’t.&lt;/li&gt;
   &lt;li&gt;
      With spinning, using .NET 4.0’s new System.Threading.SpinWait type.&lt;/li&gt;
   &lt;li&gt;
      An implementation that overuses interlocked operations as many naïve programmers would
      do.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   The 2 element capacity situation is common in some message passing systems, e.g. Ada
   rendezvous, Comega joins, and the like.&amp;nbsp; Whereas the 1,024 element capacity situation
   is common for more general purpose channels, where some amount of pipelining is anticipated.
&lt;/p&gt;
&lt;p&gt;
   I whipped together a benchmark -- so quickly that we can barely trust it, I might
   add -- to measure these things.&amp;nbsp; Here’s a small table, showing the observed relative
   costs:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;2
   capacity&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;1024 capacity&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;As-is&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;No-spin&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;100.00%&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;1.93%&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spin&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;56.41%&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;1.66%&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;Naïve&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;No-spin&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;101.20%&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;2.09%&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Spin&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;67.73%&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;1.87%&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   As with most microbenchmarks, take the results with a grain of salt.&amp;nbsp; And there
   are certainly more interesting variants to compare this against, including a monitor-based&amp;nbsp;implementation
   that locks around access to the buffer itself.&amp;nbsp; Nevertheless, we can draw a few
   conclusions from this: as expected, the version that uses a single interlocked on
   enqueue and single interlocked on dequeue is faster than the naïve version that uses
   multiple (surprise!); spinning makes a much more interesting difference on the 2 element
   capacity situation, as expected, because it reduces the number of context switches
   dramatically; and, finally, the larger capacity enables a producer to race ahead of
   the consumer, hence avoiding far fewer transitions from full to empty to full and
   so forth.
&lt;/p&gt;
&lt;p&gt;
   This post was more of a case study than anything else.&amp;nbsp; There is nothing conclusive
   or groundbreaking here, and I suppose I should have said that would be the case up
   front.&amp;nbsp; That said, I’ve seen this technique used in over a dozen situations in
   actual product code now, so I figured I’d write a little about it, with a focus on
   how to minimize the synchronization operations.&amp;nbsp; We even contemplated shipping
   such a type in Parallel Extensions to .NET, but it’s just too darn specialized to
   warrant it.&amp;nbsp; So the closest thing&amp;nbsp;we provided is BlockingCollection&amp;lt;T&amp;gt;.&amp;nbsp;
   Enjoy.
&lt;/p&gt;
&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=3740daff-a459-4298-bc9b-65d3647f5c0d" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,3740daff-a459-4298-bc9b-65d3647f5c0d.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=304dd2b1-710a-4b97-aede-b84148124f60</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,304dd2b1-710a-4b97-aede-b84148124f60.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,304dd2b1-710a-4b97-aede-b84148124f60.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=304dd2b1-710a-4b97-aede-b84148124f60</wfw:commentRss>
      <slash:comments>22</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I've officially started down the long road of writing a 2nd edition of <a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html">Concurrent
      Programming on Windows</a>, and would like your help.
   </p>
        <p>
      There are many great new features in Windows 7 and the next versions of <a href="http://msdn.microsoft.com/concurrency/">.NET,
      Visual C++ / CRT, and Visual Studio</a>.  The book will of course cover them
      all.
   </p>
        <p>
      But I am also looking to reshape the 1st edition in many dimensions.  I'd like
      to focus on readability, conciseness, and clearly separating the "must know" topics
      from the more geeky and advanced ones.  This is a common conundrum when writing
      a technical book.  The advanced topics are more likely to appeal to readers of
      my blog, for instance, but may be daunting for newcomers to concurrency.  Tradeoffs
      abound.  Nevertheless the 2nd edition is likely to be slimmed down compared to
      the 1st.
   </p>
        <p>
      Any and all feedback, suggestions, and ideas are welcome.  What
      did you like about the 1st edition, and what did you not like?  If you could
      change a handful of things, what would make the top of your list?  And was it
      missing something crucial that you would like to see covered?  Please send your
      feedback to joe AT@ acm DOT org, or simply leave comments here on the blog. 
      Regardless of whether you've read the 1st edition or not.
   </p>
        <p>
      I sincerely look forward to hearing from you.  Cheers.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=304dd2b1-710a-4b97-aede-b84148124f60" />
      </body>
      <title>2nd edition of Concurrent Programming on Windows</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,304dd2b1-710a-4b97-aede-b84148124f60.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/09/29/2ndEditionOfConcurrentProgrammingOnWindows.aspx</link>
      <pubDate>Tue, 29 Sep 2009 00:47:03 GMT</pubDate>
      <description>&lt;p&gt;
   I've officially started down the long road of writing a 2nd edition of &lt;a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html"&gt;Concurrent
   Programming on Windows&lt;/a&gt;, and would like your help.
&lt;/p&gt;
&lt;p&gt;
   There are many great new features in Windows 7 and the next versions of &lt;a href="http://msdn.microsoft.com/concurrency/"&gt;.NET,
   Visual C++ / CRT, and Visual Studio&lt;/a&gt;.&amp;nbsp; The book will of course cover them
   all.
&lt;/p&gt;
&lt;p&gt;
   But I am also looking to reshape the 1st edition in many dimensions.&amp;nbsp; I'd like
   to focus on readability, conciseness, and clearly separating the "must know" topics
   from the more geeky and advanced ones.&amp;nbsp; This is a common conundrum when writing
   a technical book.&amp;nbsp; The advanced topics are more likely to appeal to readers of
   my blog, for instance, but may be daunting for newcomers to concurrency.&amp;nbsp; Tradeoffs
   abound.&amp;nbsp; Nevertheless the 2nd edition is likely to be slimmed down compared to
   the 1st.
&lt;/p&gt;
&lt;p&gt;
   Any and all feedback, suggestions,&amp;nbsp;and ideas&amp;nbsp;are welcome.&amp;nbsp;&amp;nbsp;What
   did you like about the 1st edition, and what did you not like?&amp;nbsp;&amp;nbsp;If you could
   change a handful of things, what would make the top of your list?&amp;nbsp; And was it
   missing something crucial that you would like to see covered?&amp;nbsp; Please send your
   feedback to joe AT@ acm DOT org, or simply leave comments here on the blog.&amp;nbsp;
   Regardless of whether you've read the 1st edition or not.
&lt;/p&gt;
&lt;p&gt;
   I sincerely look forward to hearing from you.&amp;nbsp; Cheers.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=304dd2b1-710a-4b97-aede-b84148124f60" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,304dd2b1-710a-4b97-aede-b84148124f60.aspx</comments>
      <category>Books;Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=74bd46d2-eedf-49ab-a160-9a4d921fe34c</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,74bd46d2-eedf-49ab-a160-9a4d921fe34c.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,74bd46d2-eedf-49ab-a160-9a4d921fe34c.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=74bd46d2-eedf-49ab-a160-9a4d921fe34c</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I had originally entitled this post "Having your concurrency cake and eating it too",
      but it sounded a little too silly.
   </p>
        <p>
      I have grown convinced over the past few years that taming side effects in our programming
      languages is a necessary prerequisite to attaining ubiquitous parallelism nirvana. 
      Although I am continuously exploring the ways in which we can accomplish this -- ranging
      from shared nothing isolation, to purely functional programming, and anything and
      everything in between -- what I wonder the most about is whether the development ecosystem
      at large is ready and willing for such a change.
   </p>
        <p>
      It is this that I find the most frightening.  I know we can give the world Haskell,
      or Erlang, or simple incremental steps within familiar environments, like Parallel
      Extensions.  (Indeed, the world already has these things.)  But elevating
      effects to a first class concern in day-to-day programming turns out to be a tough
      pill to swallow.  Particularly since the incremental degrees of parallelism that
      this switch will unlock are questionable (see <a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/feedback-directed.pdf">this</a> and <a href="http://www.haskell.org/~simonmar/papers/multicore-ghc.pdf">this</a>);
      and even if they were pervasive and impressive, it's unclear what percentage of developers
      will pay what specific price for a 2x, 4x, or even 16x increase in compute performance. 
      It sounds great on paper, but the cost / benefit equation is a complicated one.
   </p>
        <p>
      "Pay for play" is the standard terminology we use for such things around here, and
      the solution needs to have the right amount of it.
   </p>
        <p>
      Many folks with embarrassingly parallel algorithms will succeed just fine in a shared
      memory + locks + condition variables world, and indeed have already begun to do so. 
      And specialized tools -- like GPGPU programming -- have popped up that, when small
      kernels of computations are written in a highly constrained way, will parallelize,
      sometimes impressively.  Is this enough?  Perhaps for the next 5 years,
      but surely not much longer after that.  It is in my opinion qualitatively very
      important for the future of computer science that we provide programming environments
      that are more conducive to safe and automatic parallelism.  And yet I cannot
      stand up with a straight face and proclaim that each and every developer on the face
      of the planet should practice side effect abstinence.  A healthy balance between
      cognitive familiarity and pragmatic [r]evolution must be found.  Many promising
      approaches are in the works (see <a href="http://dpj.cs.uiuc.edu/">UIUC's DPJ</a>),
      but we are years away.
   </p>
        <p>
      Until then, parallelism on broadly deployed commercial platforms will likely remain
      in the realm of specialists.
   </p>
        <p>
      Of course, Haskell and Erlang both accomplish the no effects feat in a sneaky way. 
      For those interested in foisting parallelism unto the masses, lessons can be learned
      from these communities.  If you buy into purely functional programming, you necessarily
      buy into programming without effects, and the (sparing) use of monads to represent
      them.  (Or, as my colleague Erik calls it, <a href="http://en.oreilly.com/oscon2009/public/schedule/detail/9099">fundamentalist
      functional programming</a>).)  And if you buy into large scale message passing,
      you (typically) necessarily also buy into programming without shared memory, leaving
      behind only strongly isolated effects.  The key here is that developers gain
      many other benefits by switching to these platforms -- and the lack of effects is
      admittedly a consequential byproduct of this switch.  The lack of effects are
      not center stage.  The two approaches have recently begun to converge in what
      I believe to be the appropriate long-term approach: strong isolation with effects
      within, and safe, deterministic data parallelism through careful control over sharing,
      aliasing, and heap separation.
   </p>
        <p>
      That said, though not center stage, the switch to effectless programming is certainly
      not painless.
   </p>
        <p>
      Enabling side effects among otherwise functional code, I think, is a good thing, because
      it allows familiar algorithms to be encoded in an ordinary imperative way.  Familiarity
      is key: it may sound two faced, but I don't think parallelism is sufficiently top
      of mind that developers will want to completely rearrange the way that they write
      software.  Perhaps we will evolve in this direction, but a significant leap will
      fall flat.  Moreover, many algorithms actually depend on stateful updates to
      achieve adequate performance, like write in place graphics buffers.  The Haskell
      state monad strikes a nice balance between embedding imperative-looking effects, when
      coupled with the do notation, within a strictly functional language.
   </p>
        <p>
      Furthermore, I really respect that Haskell discourages cheating.  (Any unsafePerformIO
      is viewed with great suspicion.)  I quite like mostly-functional programming
      languages like ML and Scheme, because they tend to be easier on programmers with C
      backgrounds, but strongly dislike that a mutation can lurk within what appears to
      be an otherwise pure function.  Documenting side effects in the type system is
      healthy and allows better symbolic reasoning about the dependencies and implicit parallelism
      contained within, transitively, while still providing a way to get at effectful programming. 
      Haskell does a great job at this.  The elimination of dependence ought to be
      the focus of programmers, and not the elimination of ad-hoc and unstructured access
      to shared, mutable state.  These are algorithmic and important concerns. 
   </p>
        <p>
      What remains unclear is where the boundaries lie.  Part and parcel of documenting
      effects is thinking about them when designing your software.  You need to consider
      whether IList&lt;T&gt;'s Contains method may mutate the list or not, for example,
      and hold the line on implementations of the interface.  Either it returns an
      'a' or an 'IO a' -- and this decision is one that has far reaching implications. 
      This is a wholly separate kind of interface contract than what most programmers are
      accustomed to having to think about during the code-debug-edit cycle.  And surely
      Python and JavaScript developers will not care one way or the other, particularly
      if it forces more design decisions up front than what is customary today.  This
      bifurcation seems inevitable, and yet there is substantial crossover: C# developers
      will write Python scripts, and Python developers will consume components written in
      C#.
   </p>
        <p>
      And yet, I think we need to venture down this path in order achieve automatically
      scalable software.  Parallel computers have become incredibly cheap, and so the
      historical barriers into high performance technical computing have been whittled away
      to the software skills necessary to write scalable programs; we will likely succeed
      at expanding this market without radical changes, but if we stopped there, vast reams
      of client-side software will be left in the dust.  I've been making inroads into
      solving the problem on my end, with a new language that sits between C# and Haskell. 
      I'm biased, have been hard at work on this problem for many years, and yet still struggle
      to answer these fundamental questions.  I am a big believer that there's got
      to be a happy medium out there.  But I'm still very perplexed, and face some
      very high walls to hurdle.  Who will discover the right balance, and when will
      they do so?
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=74bd46d2-eedf-49ab-a160-9a4d921fe34c" />
      </body>
      <title>On effects and ubiquitous parallelism</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,74bd46d2-eedf-49ab-a160-9a4d921fe34c.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/07/27/OnEffectsAndUbiquitousParallelism.aspx</link>
      <pubDate>Mon, 27 Jul 2009 22:57:18 GMT</pubDate>
      <description>&lt;p&gt;
   I had originally entitled this post "Having your concurrency cake and eating it too",
   but it sounded a little too silly.
&lt;/p&gt;
&lt;p&gt;
   I have grown convinced over the past few years that taming side effects in our programming
   languages is a necessary prerequisite to attaining ubiquitous parallelism nirvana.&amp;nbsp;
   Although I am continuously exploring the ways in which we can accomplish this -- ranging
   from shared nothing isolation, to purely functional programming, and anything and
   everything in between -- what I wonder the most about is whether the development ecosystem
   at large is ready and willing for such a change.
&lt;/p&gt;
&lt;p&gt;
   It is this that I find the most frightening.&amp;nbsp; I know we can give the world Haskell,
   or Erlang, or simple incremental steps within familiar environments, like Parallel
   Extensions.&amp;nbsp; (Indeed, the world already has these things.)&amp;nbsp; But elevating
   effects to a first class concern in day-to-day programming turns out to be a tough
   pill to swallow.&amp;nbsp; Particularly since the incremental degrees of parallelism that
   this switch will unlock are questionable (see &lt;a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/feedback-directed.pdf"&gt;this&lt;/a&gt; and &lt;a href="http://www.haskell.org/~simonmar/papers/multicore-ghc.pdf"&gt;this&lt;/a&gt;);
   and even if they were pervasive and impressive, it's unclear what percentage of developers
   will pay what specific price for a 2x, 4x, or even 16x increase in compute performance.&amp;nbsp;
   It sounds great on paper, but the cost / benefit equation is a complicated one.
&lt;/p&gt;
&lt;p&gt;
   "Pay for play" is the standard terminology we use for such things around here, and
   the solution needs to have the right amount of it.
&lt;/p&gt;
&lt;p&gt;
   Many folks with embarrassingly parallel algorithms will succeed just fine in a shared
   memory + locks + condition variables world, and indeed have already begun to do so.&amp;nbsp;
   And specialized tools -- like GPGPU programming -- have popped up that, when small
   kernels of computations are written in a highly constrained way, will parallelize,
   sometimes impressively.&amp;nbsp; Is this enough?&amp;nbsp; Perhaps for the next 5 years,
   but surely not much longer after that.&amp;nbsp; It is in my opinion qualitatively very
   important for the future of computer science that we provide programming environments
   that are more conducive to safe and automatic parallelism.&amp;nbsp; And yet I cannot
   stand up with a straight face and proclaim that each and every developer on the face
   of the planet should practice side effect abstinence.&amp;nbsp; A healthy balance between
   cognitive familiarity and pragmatic [r]evolution must be found.&amp;nbsp; Many promising
   approaches are in the works (see &lt;a href="http://dpj.cs.uiuc.edu/"&gt;UIUC's DPJ&lt;/a&gt;),
   but we are years away.
&lt;/p&gt;
&lt;p&gt;
   Until then, parallelism on broadly deployed commercial platforms will likely remain
   in the realm of specialists.
&lt;/p&gt;
&lt;p&gt;
   Of course, Haskell and Erlang both accomplish the no effects feat in a sneaky way.&amp;nbsp;
   For those interested in foisting parallelism unto the masses, lessons can be learned
   from these communities.&amp;nbsp; If you buy into purely functional programming, you necessarily
   buy into programming without effects, and the (sparing) use of monads to represent
   them.&amp;nbsp; (Or, as my colleague Erik calls it, &lt;a href="http://en.oreilly.com/oscon2009/public/schedule/detail/9099"&gt;fundamentalist
   functional programming&lt;/a&gt;).)&amp;nbsp; And if you buy into large scale message passing,
   you (typically) necessarily also buy into programming without shared memory, leaving
   behind only strongly isolated effects.&amp;nbsp; The key here is that developers gain
   many other benefits by switching to these platforms -- and the lack of effects is
   admittedly a consequential byproduct of this switch.&amp;nbsp; The lack of effects are
   not center stage.&amp;nbsp; The two approaches have recently begun to converge in what
   I believe to be the appropriate long-term approach: strong isolation with effects
   within, and safe, deterministic data parallelism through careful control over sharing,
   aliasing, and heap separation.
&lt;/p&gt;
&lt;p&gt;
   That said, though not center stage, the switch to effectless programming is certainly
   not painless.
&lt;/p&gt;
&lt;p&gt;
   Enabling side effects among otherwise functional code, I think, is a good thing, because
   it allows familiar algorithms to be encoded in an ordinary imperative way.&amp;nbsp; Familiarity
   is key: it may sound two faced, but I don't think parallelism is sufficiently top
   of mind that developers will want to completely rearrange the way that they write
   software.&amp;nbsp; Perhaps we will evolve in this direction, but a significant leap will
   fall flat.&amp;nbsp; Moreover, many algorithms actually depend on stateful updates to
   achieve adequate performance, like write in place graphics buffers.&amp;nbsp; The Haskell
   state monad strikes a nice balance between embedding imperative-looking effects, when
   coupled with the do notation, within a strictly functional language.
&lt;/p&gt;
&lt;p&gt;
   Furthermore, I really respect that Haskell discourages cheating.&amp;nbsp; (Any unsafePerformIO
   is viewed with great suspicion.)&amp;nbsp; I quite like mostly-functional programming
   languages like ML and Scheme, because they tend to be easier on programmers with C
   backgrounds, but strongly dislike that a mutation can lurk within what appears to
   be an otherwise pure function.&amp;nbsp; Documenting side effects in the type system is
   healthy and allows better symbolic reasoning about the dependencies and implicit parallelism
   contained within, transitively, while still providing a way to get at effectful programming.&amp;nbsp;
   Haskell does a great job at this.&amp;nbsp; The elimination of dependence ought to be
   the focus of programmers, and not the elimination of ad-hoc and unstructured access
   to shared, mutable state.&amp;nbsp; These are algorithmic and important concerns. 
&lt;/p&gt;
&lt;p&gt;
   What remains unclear is where the boundaries lie.&amp;nbsp; Part and parcel of documenting
   effects is thinking about them when designing your software.&amp;nbsp; You need to consider
   whether IList&amp;lt;T&amp;gt;'s Contains method may mutate the list or not, for example,
   and hold the line on implementations of the interface.&amp;nbsp; Either it returns an
   'a' or an 'IO a' -- and this decision is one that has far reaching implications.&amp;nbsp;
   This is a wholly separate kind of interface contract than what most programmers are
   accustomed to having to think about during the code-debug-edit cycle.&amp;nbsp; And surely
   Python and JavaScript developers will not care one way or the other, particularly
   if it forces more design decisions up front than what is customary today.&amp;nbsp; This
   bifurcation seems inevitable, and yet there is substantial crossover: C# developers
   will write Python scripts, and Python developers will consume components written in
   C#.
&lt;/p&gt;
&lt;p&gt;
   And yet, I think we need to venture down this path in order achieve automatically
   scalable software.&amp;nbsp; Parallel computers have become incredibly cheap, and so the
   historical barriers into high performance technical computing have been whittled away
   to the software skills necessary to write scalable programs; we will likely succeed
   at expanding this market without radical changes, but if we stopped there, vast reams
   of client-side software will be left in the dust.&amp;nbsp; I've been making inroads into
   solving the problem on my end, with a new language that sits between C# and Haskell.&amp;nbsp;
   I'm biased, have been hard at work on this problem for many years, and yet still struggle
   to answer these fundamental questions.&amp;nbsp; I am a big believer that there's got
   to be a happy medium out there.&amp;nbsp; But I'm still very perplexed, and face some
   very high walls to hurdle.&amp;nbsp; Who will discover the right balance, and when will
   they do so?
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=74bd46d2-eedf-49ab-a160-9a4d921fe34c" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,74bd46d2-eedf-49ab-a160-9a4d921fe34c.aspx</comments>
      <category>Technology</category>
    </item>
  </channel>
</rss>