<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Generalities &amp; Details: Adventures in the High-tech Underbelly</title>
  <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/" />
  <link rel="self" href="http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2008-05-16T23:21:54.5532945-04:00</updated>
  <author>
    <name>Joe Duffy</name>
  </author>
  <subtitle>Joe Duffy's Weblog</subtitle>
  <id>http://www.bluebytesoftware.com/blog/</id>
  <generator uri="http://www.dasblog.net" version="1.8.5223.2">DasBlog</generator>
  <entry>
    <title>Concurrent counting</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/05/17/ConcurrentCounting.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,353c8058-be59-46c1-bd99-da9f0ed2e9ed.aspx</id>
    <published>2008-05-16T23:18:59.2390000-04:00</published>
    <updated>2008-05-16T23:21:54.5532945-04:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="html">&lt;p&gt;
   Counting events and doing something once a certain number have been registered is
   a highly common pattern that comes up in concurrent programming a lot.&amp;nbsp; In the
   olden days, COM ref counting was a clear example of this: multiple threads might share
   a COM object, call Release when done with it, and hence memory management was much
   simpler.&amp;nbsp; GC has alleviated a lot of that, but the problem of deciding when a
   shared IDisposable resource should be finally Disposed of in .NET is strikingly similar.&amp;nbsp;
   And now-a-days, things like CountdownEvent are commonly useful for orchestrating multiple
   workers (see &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163427.aspx"&gt;MSDN
   Magazine&lt;/a&gt;), which (although not evident at first) is based on the same counting
   principle.
&lt;/p&gt;
&lt;p&gt;
   Coding up one-off solutions to all of these is actually pretty simple.&amp;nbsp; But doing
   so seems unfashionably ad-hoc, at least to me.&amp;nbsp; Codifying the pattern can be
   done in a couple dozen lines of code, so that it can be reused for many purposes.&amp;nbsp;
   As an example, here is a reusable Counting&amp;lt;T&amp;gt; class, written in C#, that just
   invokes action delegate once the count reaches zero:
&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;#pragma
   warning disable 0420&lt;o:p&gt;&lt;/o:p&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;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-FAMILY: Consolas"&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-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"&gt;
   &lt;span style="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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public class Counting&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-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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private
   readonly T m_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"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private
   volatile int m_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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private
   readonly Action&amp;lt;T&amp;gt; m_action;&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public
   Counting(T obj, int initialCount, Action&amp;lt;T&amp;gt; action)&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   m_obj = 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"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;m_count = initialCount;&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   m_action = action;&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public
   int AddRef()&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   int 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   if ((c = Interlocked.Increment(ref m_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-FAMILY: Consolas"&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;
   throw new Exception();&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   return 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public
   int Release()&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   int 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   if ((c = Interlocked.Decrement(ref m_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"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&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;
   m_action(m_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"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   return 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&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-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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public
   T Obj { get { return m_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"&gt;
   &lt;span style="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;
   Notice I’ve used the IUnknown vocabulary of AddRef and Release.&amp;nbsp; Old habits die
   hard.
&lt;/p&gt;
&lt;p&gt;
   The CountdownEvent I mentioned earlier is just a simple extension to this basic functionality.&amp;nbsp;
   In fact, we don’t need to write another class; it’s merely an instance of Counting&amp;lt;T&amp;gt;,
   where the T is a ManualResetEvent.&amp;nbsp; Setters directly use the Counting&amp;lt;T&amp;gt;
   object’s Release method to register a signal, while waiters can use the WaitOne method
   on the raw ManualResetEvent itself.&amp;nbsp; The event will be set once all signals have
   arrived:
&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;Counting&amp;lt;ManualResetEvent&amp;gt;
   countingEv = new Counting&amp;lt;ManualResetEvent&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; new ManualResetEvent(false),
   N, e =&amp;gt; e.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-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-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-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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;// Setter:&lt;o:p&gt;&lt;/o:p&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=#000000&gt;countingEv.Release();&lt;o:p&gt;&lt;/o:p&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;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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;// Waiter:&lt;o:p&gt;&lt;/o:p&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=#000000&gt;countingEv.Obj.WaitOne();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   (Exposing a traditional Set/Wait interface would of course be nicer, but even then
   Counting&amp;lt;T&amp;gt; makes the implementation brain-dead simple.)
&lt;/p&gt;
&lt;p&gt;
   Similarly, the “who should dispose” problem is easy to solve with Counting&amp;lt;T&amp;gt;.&amp;nbsp;
   Say that, instead of setting the event, we actually want to Dispose of some IDisposable
   object when all threads are done with it:
&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;Counting&amp;lt;ManualResetEvent&amp;gt;
   ev = new Counting&amp;lt;ManualResetEvent&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; new ManualResetEvent(false),
   N, e =&amp;gt; e.Dispose()&lt;o:p&gt;&lt;/o:p&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=#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;
   Though this does the trick, we might instead wrap it in a more convenient package:
&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public class CountingDispose&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   Counting&amp;lt;T&amp;gt;, IDisposable&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   where T : IDisposable&lt;o:p&gt;&lt;/o:p&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=#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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public
   CountingDispose(T obj, int initialCount) :&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   base(obj, initialCount, d =&amp;gt; d.Dispose()) { }&lt;o:p&gt;&lt;/o:p&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=#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;
   Given this definition, threads can use the CountingDispose&amp;lt;T&amp;gt; object as they
   would any IDisposable thing.&amp;nbsp; This facilitates use in C# using blocks.&amp;nbsp;
   Only when all threads have called Dispose will Dispose be called on the actual underlying
   object:
&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;CountingDispose&amp;lt;ManualResetEvent&amp;gt;
   ev = new CountingDispose&amp;lt;ManualResetEvent&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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; new ManualResetEvent(false),
   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-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-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-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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;// Some threads 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;using (ev) {&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; … ev.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-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-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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;// Some threads 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-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;using (ev) {&lt;o:p&gt;&lt;/o:p&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=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; … ev.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-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;
   I’ve found that the extremely simple Counting&amp;lt;T&amp;gt; idea is a surprisingly powerful
   one.&amp;nbsp; It’s fairly extensible too; for example, you clearly may want to run actions
   at different points in the counting, use clever synchronization to ensure actions
   run at particular points are processed in-order (useful for progress reporting), to
   reset the count afterwards, and so on.&amp;nbsp; It’s way too simple to claim it’s anything
   terribly amazing, but thought I’d share the idea anyway.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=353c8058-be59-46c1-bd99-da9f0ed2e9ed" /&gt;</content>
  </entry>
  <entry>
    <title>Concurrency-oriented code reviews</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/03/28/ConcurrencyorientedCodeReviews.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,6e62a753-75b9-4486-92e8-e32c38a85ad4.aspx</id>
    <published>2008-03-28T13:04:50.6782047-04:00</published>
    <updated>2008-03-28T13:04:50.6782047-04:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      We take code reviews very seriously in our group.  No checkin is ever made without
      a peer developer taking a close look.  (Incubation projects are often treated
      differently than product work, because the loss of agility is intolerable.) 
      A lot of this is done over email, but if there’s anything that is unclear from just
      looking at the code, a face to face review is done.  Feedback ranges from consistency
      (with guidelines and surrounding code), finding errors or overlooked conditions, providing
      suggestions on how to more clearly write something, comments, etc.; this ensures that
      our codebase is always of super high quality.
   </p>
        <p>
      Concurrency adds some complexity to development, and requires special consideration
      during code reviews.  I thought I’d put some thoughts on paper about what I look
      for during concurrency-oriented code reviews, in hopes that it will be useful to anybody
      starting to sink their teeth into concurrency; it may also help you devise your own
      internal review guidelines.  Most of this advice just comes down to knowing a
      laundry list of best practices, but a lot of it is also knowing what to look for and
      where to spend your time during a review.
   </p>
        <p>
      (A couple years ago I wrote a lengthy “Concurrency and its impact on reusable libraries”
      essay which provides a lot of the motivation behind what I look for.  It’s up
      on my blog, <a href="http://www.bluebytesoftware.com/blog/2006/10/26/ConcurrencyAndTheImpactOnReusableLibraries.aspx">http://www.bluebytesoftware.com/blog/2006/10/26/ConcurrencyAndTheImpactOnReusableLibraries.aspx</a>,
      and (though slightly out of date) I’m revising it for an Appendix in <a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html">my
      upcoming book</a>.  If you question why I believe something, chances are that
      this document will explain my rationale.  And it’s far more complete than this
      short essay; I’ve only hit the high points here.)
   </p>
        <p>
          <strong>Getting started</strong>
        </p>
        <p>
      I first review the code in a traditional sequential code review fashion.  When
      doing this, I earmark all state that I see as either “private” (aka isolated) or “shared”. 
      I then go back and closely review all state that is shared (accessible from many threads)
      with a fine-tooth comb.  Sometimes I’ll do this during my first pass through,
      but I usually find it helpful to understand the algorithmic structure of the changes
      first before fully developing an understanding of the concurrency parts.
   </p>
        <p>
      Changes to existing code should be reviewed just as carefully (if not more carefully)
      as new code.  Concurrency behavior is subtle, and it’s very easy to accidentally
      violate some unchecked assumption the code was previously making.  Liberal use
      of asserts is therefore very important.  Sadly many of the conditions code assumes
      are simply unassertable (like “object X isn’t shared”).<br />
      I easily spend about 2x the amount of time reviewing the concurrency aspects of the
      code than usual sequential aspects.  Perhaps more.  This extra time is OK,
      because the concurrency portion is far smaller (in lines of code) than the sequential
      portion in most of the code I review.  There are obvious exceptions to this rule,
      especially since I’m on a team building low-level primitive data types whose sole
      purpose in life is to be used in concurrent apps.
   </p>
        <p>
          <strong>Shared state and synchronization</strong>
        </p>
        <p>
      Some state, although shared, is immutable (read-only) and can be safely shared and
      read from concurrently.  Often this is by construction (e.g. immutable value
      types) but sometimes this is by loose convention (e.g. a data structure is immutable
      for some period of time, simply by virtue of no threads actively writing to it). 
      Both should be clearly documented in the code.<br />
      Once mutable shared state is identified, I look for two major things:
   </p>
        <ol>
          <li>
         When does it become shared, i.e. publicized, and what is the protocol for the transfer
         of ownership?  Is it done cleanly?  And is it well documented? 
      </li>
          <li>
         When does it once again become private again, if ever?  And is this documented
         too?</li>
        </ol>
        <p>
      Ideally all shared state would have clean ownership transitions.  Any state that
      is disposable necessarily must have a point at the end of its life where it has a
      single owner, so it can be safely disposed of (unless ref-counting is used). 
      But for most state the line will be extremely blurry and unenforced.  Comments
      should be used to clarify, in gory detail.  I also tend to prefix names of variables
      that refer to shared objects with the word ‘shared’ itself, so that they jump out.
   </p>
        <p>
      Many, many bugs arise from some code publicizing some state, sometimes by accident,
      and then continuing to treat it as though it is private.  It is also sometimes
      tricky to determine this precisely, since sharing can be modal.  A list data
      structure may be shared in some contexts but not others.  Knowing what its sharing
      policy is requires transitive knowledge of callers.  Building up this level of
      global understanding can involve a fair bit of simply sitting back and reading and
      rereading the code over and over again.
   </p>
        <p>
      Once the policy around sharing a piece of state is known, it is crucial to understand
      the intended synchronization policy for that data.  Is it protected by a fine-grained
      monitor?  Is it manipulated and read in a lock-free way?  And so on. 
      And once the intended policy is known, is the actual policy implemented what was intended?
   </p>
        <p>
      While this part is extremely important, by the way, I have to admit that I feel this
      aspect tends to overshadow other things in conversation.  This is probably because
      it’s the most obvious thing to look for.  Sadly the world of concurrency is far
      more subtle than this.  I’ve honestly found more bugs resulting from failing
      to identify shared state properly than resulting from failing to implement the synchronization
      logic itself properly.  Your mileage will of course vary.
   </p>
        <p>
          <strong>Locks</strong>
        </p>
        <p>
      I treat lock-based code and lock-free as two entirely separate beasts.  I spend
      about 5x the time reviewing lock-free code when compared to lock-based code. 
      There is a tax to having lock-free code in any codebase, so as you are reviewing it,
      also ask yourself: is there a better (or almost-as-good) way that this could have
      been done using locks?  Often the answer is no, due to the benefits lock-freedom
      brings (no thread can indefinitely starve another). 
   </p>
        <p>
      But if the answer is yes, that the code could be written more clearly using locks,
      you could save your team a lot of time by convincing the author to change his or her
      mind.  Not only is lock-free code far more difficult to write and test, it carries
      a large tax during long stress hauls and end-game bug-fixing, an important and time-sensitive
      period in the development lifecycle of any commercial software product.  Maintaining
      lock-free code also carries an extra long-term cost, particularly when ramping up
      new hires on it.  All of this risks interfering with your ability to work on
      cool new features at some point.  Don’t feel bad about pushing back on this one. 
      Hard.
   </p>
        <p>
      Carefully review what happens inside of a lock region.  Look at every single
      line with scrutiny.
   </p>
        <ul>
          <li>
         Lock hold times should be as short as possible.  Hold times should be counted
         in dozens or hundreds of cycles, not thousands (unless absolutely unavoidable). 
      </li>
          <li>
         If lock hold times are in the dozens, you can consider using a spin-lock. 
      </li>
          <li>
         Recursive lock acquisitions are strongly discouraged.  If it can happen, did
         the developer clearly intend it to happen?  Or is this possibility accidental? 
         Point it out to them.  Also, are there any unexpected points at which reentrancy
         can occur?  E.g. any APC or message-pumping waits?  If yes, is there a way
         to avoid that by simple restructuring of the code? 
      </li>
          <li>
         Dynamic method calls via delegates or virtual methods while a lock is held should
         be as rare as possible.  Method calls under a lock to user-supplied code should
         only ever happen if the concurrency behavior is clearly documented and specified for
         the user, and when invariants hold.  All of these cases can lead to reentrancy. 
         Often this requires special code to detect the reentrancy and respond intelligently. 
      </li>
          <li>
         Lock regions should usually not span multiple methods: for example, acquiring the
         lock in one method, returning, and having the caller release it in another method
         is bad form.  It is very easy to screw up the control flow and deadlock your
         library. 
      </li>
          <li>
         CERs can only use spin-locks currently (because Monitor.ReliableEnter is currently
         unavailable), if you care about orphaning locks at least (which most CER-cost does). 
         If you see somebody trying to write a CER using a CLR Monitor, their code is probably
         busted.  Thankfully CERs are pretty rare to encounter in practice.</li>
        </ul>
        <p>
      Races that break code are always must-fix bugs, no matter how obscure.  If they
      happen with low frequency on the quad-cores of today, they will probably break with
      regularity on the 16-cores of tomorrow.  The kinds of code my team writes needs
      to remains correct and scale well into the distant future; presumably if you’re writing
      concurrent code already, yours does too.  If you find such a race, the code should
      not even be checked in until it’s fixed.  “But it only happens once in a while”
      is an inexcusable answer.  Benign races are OK but should be clearly documented.
   </p>
        <p>
          <strong>Events</strong>
        </p>
        <p>
      When I see any event-based code (either Monitor Wait/Pulse/PulseAll condition variables
      or some event type, like AutoResetEvent or ManualResetEvent), the first thing I do
      is build up a global understanding of all the conditions under which events are set,
      reset, and waited on.  This is to understand the coordination and flow of threads
      top-down, rather than bottom-up.  Because I’ve already reviewed the sequential
      parts of the algorithm, I typically already know the important state transitions events
      are guarding before I even get to this point.
   </p>
        <p>
      Next, there are some simple aspects to specific usage of events that I look for:
   </p>
        <ul>
          <li>
         Understanding the relationship between mutual exclusion, the state, and the events
         is important and subtle.  Comments should be used ideally to explain that. 
      </li>
          <li>
         Does the setting of the event happen in a wake-one (Pulse, Auto-Reset) or wake-all
         (PulseAll, Manual-Reset) manner?  If it’s wake-one, are all waiters homogeneous? 
         Is it always strictly true that waking-one is sufficient and won’t lead to missed
         wake-ups?</li>
          <li>
         Waiters that release the lock and then wait should be viewed with suspicion. 
         There’s a race between the release and wait that notoriously causes deadlocks.</li>
          <li>
         Concurrent code should never use timeouts as a way to work around sloppiness in the
         way threads wait and signal.  A missed wake-up is a bug in the code that must
         be fixed.</li>
        </ul>
        <p>
          <strong>Lock-freedom and volatiles</strong>
        </p>
        <p>
      If you’re looking at lock-free code, you need to have a firm grasp on the CLR’s memory
      model.  See <a href="http://www.bluebytesoftware.com/blog/2007/11/10/CLR20MemoryModel.aspx">http://www.bluebytesoftware.com/blog/2007/11/10/CLR20MemoryModel.aspx</a> for
      an overview.  Don’t think about the machine, think about the logical abstraction
      provided by the memory model.  You also need a firm grasp on the invariants of
      the data structures involved.  Specifically you are looking to see if the structure
      could ever move into a state, visible by another thread, where one of these invariants
      doesn’t hold.  I explicitly permute (often on a whiteboard or in notepad) the
      sections of the code that involve shared loads and stores, using knowledge of the
      legal reorderings given our memory model, to see if the code breaks.
   </p>
        <p>
      Any variable marked as volatile should be a red flag to carefully review all use of
      that variable.  For every single read and every single write of that variable,
      you must look at it and convince yourself of why volatile is necessary.  If you
      can’t, ask the person who wrote the code.  Sometimes volatile is used because
      most (but not all) call sites need it; that’s often acceptable.  Leaving the
      variable as non-volatile and selectively using Thread.VolatileRead for the reads that
      need it is typically too costly.  Anyway, comments should always be used to explain
      why each load and store is volatile, even if it doesn’t strictly need the volatile
      semantics.
   </p>
        <p>
      Conversely, any variable that is apparently shared, but not marked volatile, should
      be an even redder flag.  It’s very likely that this is a mistake.  Recall
      that writes happen in-order with the CLR’s memory model, but that reads do not. 
      Anytime there is a relationship between multiple shared variables that are written
      and read together (without the protection of a lock), they typically both need to
      be volatile.
   </p>
        <p>
      Any reads of shared variables used in a tight loop must be marked volatile. 
      Otherwise the compiler may decide to hoist them, causing an infinite loop.  Even
      if they are retrieved via simple method calls like property accessors (due to inlining).
   </p>
        <p>
      Thread.MemoryBarrier should typically only occur to deal with store (release) followed
      by load (acquire) reordering problems.  And it’s usually a better idea to use
      an InterlockedExchange for the store instead, since it implies a full barrier but
      combines the write.  Sometimes a fence can be used to flush write buffers—like
      when releasing a spin-lock to avoid giving the calling thread the unfair ability to
      turn right around and reacquire it—but this is extremely rare, and often an indication
      that somebody has an inaccurate mental model of what the fence is meant to do.
   </p>
        <p>
      Custom spin waiting should be used rarely.  If you see it used, the person may
      not be aware that spin waits <a href="http://www.bluebytesoftware.com/blog/2006/08/23/PriorityinducedStarvationWhySleep1IsBetterThanSleep0AndTheWindowsBalanceSetManager.aspx">need
      special attention</a>: to work well on HT machines, yield properly to all runnable
      threads with appropriate amortized costs, to spin only for a reasonable amount of
      time (in other words, less than the duration of a context switch), and so on. 
      Thread.SpinWait does not do what most people expect, since it only covers the first. 
      Kindly let them know about these things.  If any spin waiting is used in a codebase,
      it’s far better to consolidate all usage into a single primitive that does it all.
   </p>
        <p>
          <strong>Wrapping up</strong>
        </p>
        <p>
      At the end of each review, ask yourself whether all of the concurrency-oriented parts
      of the code were clearly explained in the design doc for the feature.  Did this
      carry over to clearly written comments in the implementation?  These are some
      really hard issues to get your head around, so the time spent reviewing the code should
      not be lost.  Somebody, someday down the road, will need to understand the code
      again (perhaps so that they can maintain it, test it, etc.), and it is your responsibility
      as a member of the team—regardless of whether you wrote the code—to do your part in
      making that feasible.  You should explicitly go back to the design doc and suggest
      areas for clarification.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=6e62a753-75b9-4486-92e8-e32c38a85ad4" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Hooking CLR blocking calls with SynchronizationContext</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/27/HookingCLRBlockingCallsWithSynchronizationContext.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,710e6ba3-60e9-4f5e-a5a7-d878015c7a16.aspx</id>
    <published>2008-02-27T17:47:47.5130000-05:00</published>
    <updated>2008-02-27T17:53:53.1359436-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="html">&lt;p&gt;
   I’ve &lt;a href="http://www.bluebytesoftware.com/blog/2006/11/10/FibersAndTheCLR.aspx"&gt;mentioned
   before&lt;/a&gt; that the CLR has a central wait routine that is used by any synchronization
   waits in managed code.&amp;nbsp; This covers WaitHandles (AutoResetEvent, ManualResetEvent,
   etc.), CLR Monitors (Enter, Wait), Thread.Join, any APIs that use such things, and
   the like.&amp;nbsp; This routine even gets involved for waits that are internal to the
   CLR VM itself.&amp;nbsp; This is primarily done so that the runtime can &lt;a href="http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx"&gt;pump
   appropriately on STAs&lt;/a&gt;, and was later used to experiment with fiber-mode scheduler
   in SQL Server.&amp;nbsp; Two years ago I showed how to use these capabilities to &lt;a href="http://msdn.microsoft.com/msdnmag/issues/06/04/Deadlocks/default.aspx"&gt;build
   a deadlock detection tool via the CLR’s hosting APIs&lt;/a&gt;.&amp;nbsp; Sadly IO-based waits
   (like FileStream.Read) do not route through this.
&lt;/p&gt;
&lt;p&gt;
   The System.Threading.SynchronizationContext class has a very cool (but not widely
   known) feature that enables you to extend this central wait routine.&amp;nbsp; To do so
   requires four steps: subclass SynchronizationContext; call base.SetWaitNotificationRequired;&amp;nbsp;override
   the virtual Wait method to contain some custom wait logic;&amp;nbsp;and then register
   your SynchronizationContext via the static SynchronizationContext.SetSynchronizationContext
   method.&amp;nbsp; After you do this, most waits that occur on that thread will&amp;nbsp;be
   redirected through&amp;nbsp;your custom Wait method.
&lt;/p&gt;
&lt;p&gt;
   Here's a very simple example of this:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;using
   System;&lt;br&gt;
   using System.Threading;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;class
   BlockingNotifySynchronizationContext : SynchronizationContext {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public BlockingNotifySynchronizationContext() {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetWaitNotificationRequired();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public override int Wait(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr[] waitHandles,
   bool waitAll, int millisecondsTimeout) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("Begin wait: {0} handles
   for {1} ms",&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; waitHandles.Length,
   millisecondsTimeout);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int ret = base.Wait(waitHandles, waitAll,
   millisecondsTimeout);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("Finished wait");&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ret;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;class
   Program {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main() {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SynchronizationContext.SetSynchronizationContext(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new BlockingNotifySynchronizationContext());&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ManualResetEvent mre = new ManualResetEvent(false);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mre.WaitOne(1000, false);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   If you run this, you'll see some messages printed to the console to do with beginning
   and finishing waits.
&lt;/p&gt;
&lt;p&gt;
   A few things are worth noting:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      The Wait signature looks a lot like WaitForMultipleObjects.&amp;nbsp; In fact, it's fairly
      trivial to turn around and call it via a P/Invoke.&amp;nbsp; Recovering from APCs is a
      tad tricky however, and you'd have to do all of your own timeout management, message
      pumping, and the like. 
   &lt;li&gt;
      You receive an IntPtr[], making it incredibly difficult to correlate the objects being
      waited on with the actual synchronization objects from which they came (e.g. Monitors,
      EventWaitHandles, etc.). 
   &lt;li&gt;
      The code that runs inside Wait is the wait itself.&amp;nbsp; In other words, when you
      return, whatever code initiated the wait is going to assume that the API is being
      honest and truthful.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Another subtlety is that this code, as written, is subject to stack overflow.&amp;nbsp;
   Why is that?&amp;nbsp; In this particular instance, Console.WriteLine may need to block
   internally because it automatically serializes access to the output stream.&amp;nbsp;
   Well, when that blocks, it just goes through the same central wait routine, which
   calls back out, and so on and so forth.&amp;nbsp; Obviously this extends to any code that
   uses locks, including&amp;nbsp;CLR services like cctors.&amp;nbsp; So the code you write here
   needs to be very carefully written so as not to ever block recursively.
&lt;/p&gt;
&lt;p&gt;
   Notice that some waits do not call out.&amp;nbsp; The reason is that the callout stems
   from a routine deep inside the CLR VM itself.&amp;nbsp; Some waits may occur while a GC
   is in progress, at which point it’s illegal to invoke managed code.&amp;nbsp; The CLR
   just reverts to using its own default wait logic in such cases.
&lt;/p&gt;
&lt;p&gt;
   Lastly this is not a foolproof mechanism.&amp;nbsp; Other components can register their
   own SynchronizationContexts, replacing the context you’ve installed completely.&amp;nbsp;
   This may mean you miss some blocking calls.&amp;nbsp; If you are building a ThreadPool,
   you can always reset it each time the thread is returned, or even use your own ExecutionContexts&amp;nbsp;when
   running them.&amp;nbsp; It is also possible that such a context will exist by the time
   you get around to installing your own.&amp;nbsp; For example, ASP.NET, WinForms, and WPF
   use custom SynchronizationContexts.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
   If such a context exists already when you install this custom one, you can always
   defer to it for things like CreateCopy, Send, Post, and Wait.&amp;nbsp; For example, here’s
   a SynchronizationContext implementation&amp;nbsp;that allows custom before/after wait
   actions, but otherwise relies on the existing SynchronizationContext (if any) for
   things like Send, Post, and Wait:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;using
   System;&lt;br&gt;
   using System.Threading;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;delegate
   object PreWaitNotification(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr[] waitHandles, bool WaitAll, int millisecondsTimeout);&lt;br&gt;
   delegate void PostWaitNotification(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; IntPtr[] waitHandles, bool WaitAll, int millisecondsTimeout,&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; int ret, Exception ex, object state);&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;class
   BlockingNotifySynchronizationContext : SynchronizationContext {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; private SynchronizationContext m_captured;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; private PreWaitNotification m_pre;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; private PostWaitNotification m_post;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public BlockingNotifySynchronizationContext(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PreWaitNotification
   pre, PostWaitNotification post) :&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this(SynchronizationContext.Current, pre,
   post) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public BlockingNotifySynchronizationContext(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SynchronizationContext
   captured, PreWaitNotification pre, PostWaitNotification post) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetWaitNotificationRequired();&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   m_captured = captured;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_pre = pre;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_post = post;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public override SynchronizationContext CreateCopy() {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new BlockingNotifySynchronizationContext(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_captured == null
   ? null : m_captured.CreateCopy(), m_pre, m_post);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public override void Post(SendOrPostCallback cb, object s) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (m_captured != null)&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_captured.Post(cb,
   s);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Post(cb, s);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public override void Send(SendOrPostCallback cb, object s) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (m_captured != null)&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_captured.Send(cb,
   s);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.Send(cb, s);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   public override int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object s = m_pre(waitHandles, waitAll,
   millisecondsTimeout);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int ret = 0;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exception ex = null; &lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   try {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (m_captured
   != null)&lt;br&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;
   ret = m_captured.Wait(waitHandles, waitAll, millisecondsTimeout);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br&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;
   ret = base.Wait(waitHandles, waitAll, millisecondsTimeout);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } catch (Exception e) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ex = e;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } finally {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_post(waitHandles,
   waitAll, millisecondsTimeout, ret, ex, s);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ret;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Courier New'"&gt;class
   Program {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main() {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SynchronizationContext.SetSynchronizationContext(&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new BlockingNotifySynchronizationContext(&lt;br&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;
   delegate { Console.WriteLine("PRE"); return null; },&lt;br&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;
   delegate { Console.WriteLine("POST"); }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ManualResetEvent mre = new ManualResetEvent(false);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mre.WaitOne(1000, false);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   }&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
   That’s a fair bit of code, but it's mostly boilerplate.&amp;nbsp; It allows you to easily
   specify a pre/post action to be invoked upon each blocking call, and will work on
   ASP.NET, GUI threads, and the like.&amp;nbsp; The pre action can return&amp;nbsp;an object
   for the post action to&amp;nbsp;inspect.&amp;nbsp; And the post action is given the return
   value and exception&amp;nbsp;(if any).&amp;nbsp; If no SynchronizationContext was present
   when installed, it just defers to the base SynchronizationContext implementation of
   Send, Post, and Wait.
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Now what you actually do inside those callbacks, I suppose, is entirely your business
   …
&lt;/p&gt;
&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=710e6ba3-60e9-4f5e-a5a7-d878015c7a16" /&gt;</content>
  </entry>
  <entry>
    <title>Channel9 on TPL</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/19/Channel9OnTPL.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,42529ff5-127c-4a60-8773-91588674d88d.aspx</id>
    <published>2008-02-19T14:27:36.6366935-05:00</published>
    <updated>2008-02-19T14:27:36.6366935-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      A few of us got in a room two weeks ago with Charles to discuss the <a href="http://msdn.microsoft.com/msdnmag/issues/07/10/Futures/default.aspx">Task
      Parallel Library</a> component of Parallel Extensions.
   </p>
        <p>
      It's now available on Channel9: <a href="http://channel9.msdn.com/Showpost.aspx?postid=384229">http://channel9.msdn.com/Showpost.aspx?postid=384229</a>.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=42529ff5-127c-4a60-8773-91588674d88d" />
      </div>
    </content>
  </entry>
  <entry>
    <title>IDisposable, finalization, and concurrency</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/18/IDisposableFinalizationAndConcurrency.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,5e9010c3-7899-42ce-bcfd-ff3c689a7dab.aspx</id>
    <published>2008-02-17T23:29:07.7740000-05:00</published>
    <updated>2008-02-17T23:33:41.3973340-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      A long time ago, I wrote that you’d <a href="http://www.bluebytesoftware.com/blog/2005/12/27/NeverWriteAFinalizerAgainWellAlmostNever.aspx">never
      need to write another finalizer again</a>.  I’m sorry to say, my friends, that
      I may have (unintentionally) lied.  In my defense, the blog title where
      I proclaimed this fact <em>did</em> end with “well, almost never.”
   </p>
        <p>
      Finalizers have historically been used to ensure reclamation of resources that are
      finite or outside of the purview of the CLR’s GC.  Native memory and Windows
      kernel HANDLEs immediately come to mind.  Without a finalizer, resources would
      leak; server apps would die, client apps would page like crazy, and life would be
      a mess.  For such resources, properly authored frameworks also provide IDisposable
      implementations to eagerly and deterministically reclaim the resources when they are
      definitely done.  Three years ago, I wrote <a href="http://www.bluebytesoftware.com/blog/2005/04/08/DGUpdateDisposeFinalizationAndResourceManagement.aspx">a
      lengthy treatise</a> on the subject.
   </p>
        <p>
      The finalizer is there as a backstop.  It is often meant to clean up after bugs
      , such as when a developer forgets to call Dispose in the first place, tried to but
      failed due to some runtime execution path skipping it (often exceptions-related),
      or a framework or library author hasn’t respect the transitive IDisposable rule, meaning
      that eager reclamation isn’t even possible.  It also avoids tricky ref-counting
      situations as are prevalent in native code:  since the GC handles tracking references,
      you, the programmer, can avoid needing to worry about such low-level mechanics. 
      In all honesty, the finalizer’s main purpose is probably that we wanted to facilitate
      a RAD and VB-like development experience on .NET, where programmers don’t need to
      think about resource management <em>at all</em>, unlike C++ where it’s in your face. 
      While one could reasonable argue that IDisposable is all you need (the C++ argument),
      that would have gone against this goal.
   </p>
        <p>
      Concurrency changes things a little bit.  A thread is just another resource outside
      of the purview of the CLR’s GC, and is actually backed by a kernel object and associated
      resources like non-pageable memory for the kernel stack, some data for the TEB and
      TLS, and 1MB of user-mode stack, to name a few.  They also add pressure to the
      thread scheduler.  Threads are fairly expensive to keep around, and “user” code
      is responsible for creating and destroying them.
   </p>
        <p>
      Now, it’s true that we are moving towards a world where threads and logical tasks
      are not one and the same.  This is a ThreadPool model.  But it’s also true
      that a task that is running on a thread is effectively keeping that thread alive,
      and perhaps more concerning, preventing other tasks from running on it.  <em>Use </em>of
      a resource is a kind of <em>actual </em>resource itself, although more difficult to
      quantify.
   </p>
        <p>
      So, what does all of this have to do with finalization?
   </p>
        <p>
      If some object kicks off a bunch of asynchronous work and then becomes garbage—i.e.
      the consumer of that object no longer needs to access it’s information—then it’s possible
      (or even likely) that any outstanding asynchronous operations ought to be killed as
      soon as possible.  Otherwise they will continue to use up system resources (like
      threads, the CPU, IO, system locks, virtual memory, and so on), all in the name of
      producing results that will never be needed.  The only reason this task stays
      alive is because the scheduler itself has a reference to it.
   </p>
        <p>
      Just as with everything discussed above pertaining to non-GC resources, we’d like
      it to be the case that such a component would offer two methods of cleanup:
   </p>
        <ol>
          <li>
         Dispose: to get rid of associated asynchronous computations immediately when the caller
         knows they no longer need the object. 
      </li>
          <li>
         Finalization: to get rid of associated resources that are still outstanding when the
         GC collects the root object that is responsible for managing those asynchronous computations.</li>
        </ol>
        <p>
      You’ll notice that we support cancelation in a first class and deeply-ingrained way
      in the Task Parallel Library.  While not exposed in PLINQ (yet), there is actually
      cancelation support built-in (though not as fundamental as we’d like (yet)). 
      This is a useful hook to allow us to build support for both resource reclamation models. 
      In this sense, cancelation as a pattern of stopping expensive things from happening
      is quite similar to resource cleanup.  Clearly they aren't identical, but we
      will need to figure out the specific deltas.
   </p>
        <p>
      I should also point out that we will prefer and push structured parallelism for many
      reasons.  Parallel.For is an example, where the API looks synchronous but is
      internally highly parallel.  One reason we like this model is that the point
      at which concurrency begins and ends is very specific.  The call won’t return
      until all work is accounted for and completed.  It’s only when you bleed computations
      into the background after a call returns that everything stated above becomes an issue. 
      This is obviously nice for failures (e.g. you are forced to deal with them right away),
      but also because it alleviates this problem nicely.
   </p>
        <p>
      I don’t think we’re at a point where we can recommend definite tried-and-true best
      practices for cancelation of asynchronous work and how it pertains to resource management. 
      I do think we need to get there by the time we ship Parallel Extensions V1.0. 
      And I think we will.  Here’s a snapshot summary of my current thinking, however,
      and I would love to get feedback on it:
   </p>
        <ol>
          <li>
         We should tell people to implement IDisposable and to Cancel tasks inside Dispose,
         when their classes own unstructured asynchronous computations. 
      </li>
          <li>
         We may or may not want people to implement a finalizer to do the same.  I currently
         believe we will. 
      </li>
          <li>
         I am undecided about whether these cancelations should be synchronous.  In some
         sense, they should be since you’d like to know that all resources have definitely
         been reclaimed.  But this would mean blocking (possibly indefinitely) on the
         finalizer thread.  That’s a definite no-no.  Blocking in Dispose would mean
         blocking (possibly indefinitely) inside a finally block.  That’s also a no-no,
         although it’s less severe of one than the finalizer.  It just means hosts can’t
         take over threads as easily when they need to abort them.  Thankfully we offer
         the Task.Cancel method which is non-blocking.  Possibly we should suggest synchronous
         cancelation inside of Dispose, and asynchronous inside of the finalizer. 
      </li>
          <li>
         If we did do synchronous anywhere, presumably with Task.CancelAndWait, we’d need to
         recommend a practice for communicating failures.  Throwing from Dispose is discouraged,
         but so is swallowing failures.  The kind of code usually run inside of Dispose
         is much less likely to generate exceptions than running arbitrary tasks full of user
         code.  Catch-22. 
      </li>
          <li>
         There are some cases we can do the cancelation thing ourselves.  Whether we do
         or not is subject to debate, but I believe we should.  If we ensured the scheduler’s
         references are weak, then once all other code in the process drops the reference,
         we would not schedule it.  This implies that tasks are seldom executed “for effect”,
         which is certainly a judgment call.  It might be worth exposing an option that
         allows “for effect” tasks to be created not subject to this rule. 
      </li>
          <li>
         The trickiest case is when a task is already running.  For short-running tasks,
         this may not be a huge concern, but a lot of such tasks do recursively queue up additional
         ones.  It would be nice if the fact that its results are no longer needed somehow
         flowed automatically to the task, perhaps through cancelation.   This also
         means waking tasks from blocking calls.</li>
        </ol>
        <p>
      It’s interesting to point out that 5 and 6 were part of the original motivation for
      the inventors of the future abstraction.  They noted that representing computations
      as futures, and allowing the GC to collect them before they run once they’ve become
      unreachable, effectively makes computations garbage-collectable.  This, I think,
      is a neat idea, particularly if your program uses futures pervasively.
   </p>
        <p>
      In any case, I wanted to point these subtleties out, and hear any feedback folks out
      there might have.  What I find particularly interesting about concurrency,
      as we move forward on things like Parallel Extensions, is that there are a lot of
      subtle implications to the way programs are written.  This includes fundamental
      things like exceptions and resource management.  There are other subtle impacts,
      like whether the ordering of results coming out of a computation matters.  PLINQ
      surfaced this early on, and I didn’t expect the pervasive nature of the issue.  
      Debugging and profiling are also extraordinarily different.  I suspect we’ll
      continue running into many such things throughout the evolution to highly parallel
      software.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=5e9010c3-7899-42ce-bcfd-ff3c689a7dab" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Burton on why parallel computing matters</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/13/BurtonOnWhyParallelComputingMatters.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,8479f01d-885b-4939-9e60-e651e5b26f8b.aspx</id>
    <published>2008-02-13T13:26:22.0123343-05:00</published>
    <updated>2008-02-13T13:26:22.0123343-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      A couple weeks back, we started filming a bunch of Channel9 videos about various
      aspects of the <a href="http://msdn.microsoft.com/concurrency/">Parallel Computing</a> team. 
      This is the larger team responsible for <a href="http://blogs.msdn.com/pfxteam/">Parallel
      Extensions to .NET</a>, among other things.  We'll of course spend some time
      on Parallel Extensions in upcoming videos.
   </p>
        <p>
      But who better to kick it off than <a href="http://www.microsoft.com/presspass/exec/techfellow/Smith/default.mspx">Burton
      Smith</a>, a legend in the parallel computing arena?
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <strong>On General Purpose Supercomputing and the History and Future of Parallelism<br /></strong>
            <a href="http://channel9.msdn.com/Showpost.aspx?postid=382639">http://channel9.msdn.com/Showpost.aspx?postid=382639</a>
          </p>
        </blockquote>
        <p dir="ltr">
      Burton's the kind of guy that you run into when meandering the hallways, have a 5
      minute conversation, and walk away with at least 20 relevant and fascinating papers
      on parallel computing to go off and read.  But now instead of reading
      20 papers, you can just go watch the video.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=8479f01d-885b-4939-9e60-e651e5b26f8b" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Violating type safety with torn reads</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/10/ViolatingTypeSafetyWithTornReads.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,0c7c63c2-09dd-4981-84b1-c00717bb4013.aspx</id>
    <published>2008-02-10T02:32:23.0110000-05:00</published>
    <updated>2008-02-10T02:36:53.7023906-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.bluebytesoftware.com/blog/2006/02/08/ThreadsafetyTornReadsAndTheLike.aspx">Torn
      reads</a> are possible whenever you read a shared value without synchronization that
      is either misaligned and/or which spans an addressible pointer-sized region of memory.  This
      can lead to crashes and data corruption due to bogus values being seen.  If not
      careful, torn reads can also violate type safety.  If you have a static variable
      that points to an object of type T, and your program only ever writes references to
      objects of type T into it, you may still end up accessing a memory location that isn't
      actually a T.  How could this be?
   </p>
        <p>
      You guessed it.  Torn reads apply to pointer values just as much as they do to
      ordinary values.  So a thread reading a pointer in-flux could see bits of its
      value in separate pieces, blending the state before and after the update.  Dereferencing
      this mutant pointer would lead you off into an unknown place in the address space,
      and most certainly not to an instance of T, breaking type safety.  Since VC++
      aligns pointer fields automatically, you'd have to go out of your way with __declspec(align(N))
      or an unaligned allocator to create this situation.  Similarly with .NET's StructLayoutAttribute. 
      Moreover, it turns out that .NET guards against this problem in its type loader, by
      rejecting any types containing improperly aligned object references.  This is
      good news, because otherwise a plethora of security vulnerabilities would be possible. 
      But VC++ doesn't offer any such guarantees.
   </p>
        <p>
      This is another example where trying to program in a lock-free manner can lead to
      difficulties that aren't present when you stick to ordinary locking.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=0c7c63c2-09dd-4981-84b1-c00717bb4013" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Update on: Concurrent Programming on Windows</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/02/03/UpdateOnConcurrentProgrammingOnWindows.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,b22cda2c-606f-4259-a48d-fa04e692db92.aspx</id>
    <published>2008-02-03T01:09:05.2719337-05:00</published>
    <updated>2008-02-03T01:09:05.2719337-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I'm still plugging away at my new concurrency book.
   </p>
        <p>
      The fact that we have cover art and that it's <a href="http://www.amazon.com/Concurrent-Programming-Windows-Vista-Architecture/dp/032143482X">available
      for pre-order on Amazon</a> are both great signs that it's getting close to being
      done:
   </p>
        <p>
          <img src="http://ecx.images-amazon.com/images/I/51Ua%2Bb-wjNL._SS500_.jpg" />
        </p>
        <p>
      The full TOC (as of right now) is <a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html">available
      here</a>.  A few chapters are still in the works, and there's a bit of editing
      ahead.  I've also decided to write Appendices on PFX and CCR.  In the meantime,
      definitely feel free to pick up a PDF of some early content via <a href="http://safari.oreilly.com/9780321434821">RoughCuts</a>. 
      Believe it or not, this is a way to provide real-time feedback that can impact the
      book before it hits the presses.
   </p>
        <p>
      I have to say this is the most complicated piece of writing I've ever undertaken. 
      Not only does the material require going very deep in a lot of hard areas, but I've
      noticed that as a community we're learning to speak speak better and to use consistent
      terminology about various aspects of concurrency.  To stay relevant, I'm
      finding a fair bit of content has had to be reworked several times.
   </p>
        <p>
      At the same time, and from a selfish standpoint, this book has been a wonderful forcing
      function to learn everything there is to know about concurrency.  Not that this
      is a realistic goal, mind you, but gosh darnit <em>I'm trying</em>.  I'm convinced
      at this point that the best way to become an expert on something is to try to teach
      it to other people.  And what better way than writing a book?  If you can't
      explain it <em>clearly </em>to a broad, on-the-average-less-expert-than-you audience, you
      probably have a faulty mental model to begin with.  The process of merely trying
      usually reveals this.  I encourage everybody out there to try it.  At least
      once.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=b22cda2c-606f-4259-a48d-fa04e692db92" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Blocked threads and work schedulers</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/01/17/BlockedThreadsAndWorkSchedulers.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,894f77dd-94b8-45c9-9de7-03d81144b740.aspx</id>
    <published>2008-01-17T18:29:24.1320000-05:00</published>
    <updated>2008-01-17T18:33:02.9275803-05:00</updated>
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Most schedulers try to keep the number of runnable threads (within a certain context)
      equal to the number of processors available.  Aside from fairness desires, there’s
      usually little reason to have more: and in fact, having more can lead to more memory
      pressure due to the cost of stacks and working set held on the stacks, non-pageable
      kernel memory, per-thread data structures, etc., and also has execution time costs
      due to increased pressure on the kernel scheduler, more frequent context switches,
      and poor locality due to threads being swapped in and out of processors.  In
      extreme cases, blocked threads can build up only for all of them to be awoken and
      released to wreak havoc on the system at once, hurting scalability.
   </p>
        <p>
      A naïve approach of one-thread-per-processor works great until a thread on one of
      these processors blocks, either “silently” as a result of a pagefault or “explicitly”
      due to IO or a synchronization wait.  (I should mention that due to the plethora
      of hidden blocking calls in the kernel, Win32 user-mode code, and the .NET Framework,
      a lot of IO and synchronization waiting is “silent” too.)  In this case, a processor
      becomes idle (0% utilization) for some period of time.  If there is other work
      that could be happening instead, this is clearly bad.
   </p>
        <p>
      Many programs spend most of their time blocked.
   </p>
        <p>
      Four particular solutions to this problem are commonplace on Windows:
   </p>
        <ol>
          <li>
         Create more threads than processors and hope for the best.  This trades some
         amount of runtime efficiency for the insurance that processor time won’t go to waste. 
      </li>
          <li>
         Periodically poll for blocked threads using some kind of daemon and respond to the
         presence of one by creating a new thread to execute work.  Eventually this thread
         would go away, for instance when the blocked thread awakens.  This is the approach
         used by the CLR ThreadPool, although it <a href="http://www.bluebytesoftware.com/blog/2007/03/05/WhyTheCLR20SP1sThreadpoolDefaultMaxThreadCountWasIncreasedTo250CPU.aspx">caps
         the total</a>.  (The TPL uses this appraoch today also, but we're changing/augmenting
         it.)  For obvious reasons, this approach is quite flawed: you easily end up with
         more running threads than processors, have to trade-off more frequent polling--which
         implies more runtime overhead--with less frequent polling--which adds time to the
         latency in the scheduler’s response to a blocked thread. 
      </li>
          <li>
         Block on an IO Completion Port at periodic intervals--e.g. when dispatching a new
         work item in a ThreadPool-like thing--which has the effect of throttling running threads. 
         This still requires creating more threads than processors, but helps to ensure few
         of them run at the same time.  Unfortunately, it still does lead to more of them
         actually running than you’d like since the port can only prevent a thread from running
         when it goes back and blocks on the port in the kernel.  But this is only done
         periodically. 
      </li>
          <li>
         Specialized systems like SQL have used Fibers in the past to avoid needing full-fledged
         threads to replace the blocking ones.  To do this, they ensure all blocking goes
         through a cooperative layer, which notifies a user-mode scheduler (UMS).  The
         user-mode scheduler maintains a list of blocked Fibers, but can multiplex runnable
         Fibers onto threads, keeping the number of threads equal to the number of processors. 
         A thread effectively never blocks, Fibers do, but this requires all blocking to notify
         the UMS.  Aside from extraordinarily closed world systems, this approach doesn’t
         usually work.  That’s because Fibers are not threads and multiplexing entirely
         different contexts of work onto a shared pool of threads (at blocking points) can
         easily lead to thread affinity nightmares.</li>
        </ol>
        <p>
      The CLR facilities #4 by funneling all synchronization waits in managed code through
      one point in the VM codebase.  This was done initially to ensure consistent message
      pumping on STA threads, via CoWaitForMultipleHandles.  But it was then exploited
      in 2.0 to expand the CLR Hosting APIs to enable custom hosts to hook all synchronization
      calls.  This is convenient for building interesting debugging tools, like <a href="http://msdn.microsoft.com/msdnmag/issues/06/04/Deadlocks/default.aspx">deadlock
      detecting hosts</a>.
   </p>
        <p>
      A fifth approach is often viable and even preferable, and that is to avoid blocking
      altogether.  Often referred to as continuation passing style (CPS), the idea
      is that, where you’d normally have blocked, the callstack is transformed into a resumable
      continuation.  For an example of this, look at <a href="http://msdn.microsoft.com/msdnmag/issues/06/11/ConcurrentAffairs/">Jeff
      Richter’s ReaderWriterLockGate class</a>: it’s a reader/writer lock with no blocking. 
      Asynchronous IO is supported by files and sockets on Windows, and enables a similar
      style of programming.  The continuation is ordinarily just a closure object that
      has enough state to restart itself when the sought-after condition arises.  When
      it does arise, the continuation is scheduled on something like the CLR ThreadPool. 
      This avoids burning any threads while the wait occurs.
   </p>
        <p>
      For obvious reasons, CPS is usually hard to achieve in .NET: there is no language
      support for first class continuations in .NET, all synchronization primitives are
      wait-based, and keeping a whole stack around in memory would be a terrible idea
      anyway.  You’d also need to worry about resources held on the stack, including
      locks.  Instead, you should save only that state which is needed during the continuation. 
      In a message passing system this is much simpler, since most of the program is full
      of continuations in the form of message handlers.  For an example of such a system,
      check out the <a href="http://msdn.microsoft.com/msdnmag/issues/06/09/ConcurrentAffairs">Concurrent
      and Coordination Runtime (CCR)</a> and/or <a href="http://www.erlang.org/">Erlang</a>.
   </p>
        <p>
      Even in message passing systems, it’s impossible to escape the fundamental blocking
      issue, since it is platform-wide.  And in ordinary imperative programs, the CPS
      transformation is near-impossible at the leaves of callstacks: unless you have whole
      program knowledge, who knows what your caller expects?  Most APIs are synchronous. 
      Futures and Promises potentially make this style of programming easier, though in
      the extreme all APIs would need to return a Promise rather than a true value.
   </p>
        <p>
      Nothing conclusive, just some random thoughts ...
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=894f77dd-94b8-45c9-9de7-03d81144b740" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Happy New Year &amp; .NET Rocks!</title>
    <link rel="alternate" type="text/html" href="http://www.bluebytesoftware.com/blog/2008/01/02/HappyNewYearNETRocks.aspx" />
    <id>http://www.bluebytesoftware.com/blog/PermaLink,guid,a24857cc-3c28-48be-aeac-5d092783acda.aspx</id>
    <published>2008-01-02T13:54:04.8117574-05:00</published>
    <updated>2008-01-02T13:54:04.8117574-05:00</updated>
    <category term="Miscellaneous" label="Miscellaneous" scheme="dasBlog" />
    <category term="Technology" label="Technology" scheme="dasBlog" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I've been very remiss with blogging lately.  This was mostly due to travel
      (5 weeks out of the past 2 months), but also because I'm focused intensely
      on <a href="http://www.bluebytesoftware.com/books/winconc/winconc_book_resources.html">the
      book</a>, and have been super busy working on <a href="http://msdn.microsoft.com/concurrency/">Parallel
      Extensions</a>: <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e848dc1d-5be3-4941-8705-024bc7f180ba&amp;displaylang=en">the
      December CTP</a>, <a href="http://www.bluebytesoftware.com/blog/2007/07/21/WantANewJob.aspx">hiring
      for our team</a>, planning what we do in the next year, designing stuff, implementing
      stuff, ... it's been a lot of fun.  I've also been trying to learn classical
      guitar after 15 years of playing electric and some acoustic (metal, rock, blues). 
      It's more challenging than I anticipated... I guess I should have <a href="http://en.wikipedia.org/wiki/Andr%C3%A9s_Segovia">started
      when I was six</a>.
   </p>
        <p>
      Somewhere in there, I recorded an episode of .NET Rocks with Carl and Richard about
      Parallel Extensions: <a href="http://www.dotnetrocks.com/default.aspx?showNum=301">check
      it out</a>.  I haven't listened to it yet, but I distinctly remember having a
      lot of fun.  When the hour was up, I couldn't believe how quickly the time had
      passed.
   </p>
        <p>
      Happy New Year, everybody.  I promise to blog more in the coming months. 
      Famous last words, eh?  ;)
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=a24857cc-3c28-48be-aeac-5d092783acda" />
      </div>
    </content>
  </entry>
</feed>