<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : Active Object</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Active+Object/default.aspx</link><description>Tags: Active Object</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>ActiveObject</title><link>http://blogs.msdn.com/jaredpar/archive/2008/03/30/activeobject.aspx</link><pubDate>Sun, 30 Mar 2008 19:39:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8344411</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8344411.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8344411</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8344411</wfw:comment><description>&lt;p&gt;I've been busy lately and neglected my series on &lt;a href="http://blogs.msdn.com/jaredpar/archive/2008/01/28/active-objects-and-futures.aspx"&gt;Active Objects&lt;/a&gt;.&amp;#160; It's been a fairly busy time for me both in and out of work.&amp;#160; Enough excuses, back to the fun.&lt;/p&gt;  &lt;p&gt;With the basic &lt;a href="http://blogs.msdn.com/jaredpar/archive/2008/03/02/pipesinglereader.aspx"&gt;PipeSingleReader&lt;/a&gt; class, we now have the last piece necessary to create an ActiveObject.&amp;#160; This article will focus on building the base ActiveObject which will take care of scheduling, construction, destruction and error handling.&amp;#160; The goal is to make implementing an ActiveObject that actually does work easy.&amp;#160; &lt;/p&gt;  &lt;p&gt;Lets break down the implementation of an ActiveObject into the three phases of any object; construction, destruction and running behavior. &lt;/p&gt;  &lt;h3&gt;Construction&lt;/h3&gt;  &lt;p&gt;Active Objects are associated with and have an affinity to a particular thread.&amp;#160; Constructing an ActiveObject mainly consists of creating a thread and initializing the member variables of the object.&amp;#160; &lt;/p&gt;  &lt;p&gt;There are a couple of requirements that need to be met when initializing the object.&amp;#160; The first is getting the thread into a known state before returning out of the constructor.&amp;#160; It's possible for coders to create and then immediately destroy an object.&amp;#160; Part of destructing an object is understanding the state you are destructing.&amp;#160; Returning from an ActiveObject constructor before the thread is up and running means that we can be destructed while in an inconsistent state.&amp;#160; Normally this isn't much an issue with objects because they are single threaded.&amp;#160; We will fix this by doing a simple wait until the thread is finished initializing.&lt;/p&gt;  &lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; ActiveObject() {
            m_thread = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;(() =&amp;gt; InitializeAndRunBackgroundThread());
            m_thread.Start();
            &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (0 == m_backgroundInitialized) { &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(0); }
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Next is providing implementers with a way to initialize member variables on the new thread.&amp;#160; There are many reasons for wanting to initialize members on the ActiveObject thread.&amp;#160; Besides general consistency concerns, there is also the issue that objects can have affinity to a particular thread and including forcing initialization to occur on that thread.&amp;#160; To make this simple part of the thread initialization code will call a virtual method allowing base classes to initialize variables.&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; InitializeAndRunBackgroundThread() {
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_affinity, &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ThreadAffinity&lt;/span&gt;());
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_pipe, &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;PipeSingleReader&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;gt;());
            InitializeMembersInBackground();
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_backgroundInitialized, 1);
            RunBackgroundActions();
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;virtual&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; InitializeMembersInBackground() {
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h3&gt;Running Behavior&lt;/h3&gt;

&lt;p&gt;Active Objects exist for one reason, to run Futures.&amp;#160; The main behavior is to loop over the set of Futures and run them.&amp;#160; The PipeSingleReader class takes care of most of the scheduling and threading work.&amp;#160; This leaves the ActiveObject free to make policy decisions.&amp;#160; &lt;/p&gt;

&lt;p&gt;One question that comes up is how to handle the case where a Future throws an exception?&amp;#160; If we run the Future with no protection it will simple cause an unhandled exception and likely a process crash.&amp;#160; We could catch and try to filter them but based on what criteria?&amp;#160; IMHO there is no way to properly handle an exception in the Active Object base because we don't know what the purpose of that object is.&amp;#160; Only the actual object implementer knows.&amp;#160; Therefore we will make it their problem by passing unhandled exceptions into an abstract method.&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunBackgroundActions() {
            &lt;span style="color: rgb(0,0,255)"&gt;do&lt;/span&gt; {
                RunFuture(m_pipe.GetNextOutput());
            } &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (0 == m_backgroundFinished);
            &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; future;
            &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (m_pipe.TryGetOutput(&lt;span style="color: rgb(0,0,255)"&gt;out&lt;/span&gt; future)) {
                RunFuture(future);
            }
        }
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunFuture(&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; future) {
            &lt;span style="color: rgb(0,0,255)"&gt;try&lt;/span&gt; {
                m_affinity.Check();
                future.Run();
            }
            &lt;span style="color: rgb(0,0,255)"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex) {
                OnBackgroundUnhandledException(ex);
            }
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;If the second loop looks a bit out of place, hopefully the destruction section will explain it's purpose.&lt;/p&gt;

&lt;p&gt;All that is left is to provide helper methods to let base classes queue up Futures to run.&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; RunInBackground(&lt;span style="color: rgb(43,145,175)"&gt;Action&lt;/span&gt; action) {
            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; f = &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(action);
            m_pipe.AddInput(f);
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; f;
        }
        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;lt;T&amp;gt; RunInBackground&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; func) {
            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; f = &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(func);
            m_pipe.AddInput(f);
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; f;
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h3&gt;Destruction&lt;/h3&gt;

&lt;p&gt;Destruction of an ActiveObject can be tricky with respect to handling pending actions.&amp;#160; Should they be executed, aborted or just completely ignored?&amp;#160; What happens if more input is added once we start the dispose process?&amp;#160; If we don't allow more input where should we error?&amp;#160; &lt;/p&gt;

&lt;p&gt;IMHO, the simplest user and programming model is the following.&amp;#160; &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Dispose is synchronous.&amp;#160; It will block until the background thread is destroyed.&amp;#160; Dispose is the equivalent of destruction so it follows that all resources including the thread will be destroyed when destruction completes.&lt;/li&gt;

  &lt;li&gt;Once dispose starts input will be stopped.&amp;#160; This prevents live-lock scenarios where one thread is disposing the ActiveObject and another thread is constantly adding data.&amp;#160; &lt;/li&gt;

  &lt;li&gt;If another thread tries to add an operation during the middle of disposing they will be given an exception at that time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In future posts, we'll explore how to create ActiveObjects with differing dispose semantics.&amp;#160; &lt;/p&gt;

&lt;p&gt;Now how can we signal the background thread that we are done processing?&amp;#160; Just add a future to the queue to be running.&amp;#160; Because this will run on the only thread reading the int there is no need for an Interlocked operation.&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Dispose(&lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; disposing) {
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (disposing) {
                m_pipe.AddInput(&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(() =&amp;gt; { m_backgroundFinished = 1; }));
                m_pipe.CloseInput();
                m_thread.Join();
            }
        }&lt;/pre&gt;

&lt;p&gt;Now that we've gone over the dispose code, hopefully the reason for the second loop in RunBackgroundActions is a little more apparent.&amp;#160; Between the two calls to m_pipe in Dispose another thread can post a Future.&amp;#160; Without the second loop the user will get no exception and the future will never run.&amp;#160; Likely they would hopelessly deadlock.&amp;#160;&amp;#160; The second loop will run all Futures which get caught it this gap.&amp;#160; &lt;/p&gt;

&lt;h3&gt;The Code&lt;/h3&gt;

&lt;p&gt;Here is the full version of the code. &lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;abstract&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ActiveObject&lt;/span&gt; :  &lt;span style="color: rgb(43,145,175)"&gt;IDisposable&lt;/span&gt; {
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;PipeSingleReader&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;gt; m_pipe;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ThreadAffinity&lt;/span&gt; m_affinity;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt; m_thread;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; m_backgroundInitialized;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; m_backgroundFinished;

        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; ActiveObject() {
            m_thread = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;(() =&amp;gt; InitializeAndRunBackgroundThread());
            m_thread.Start();
            &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (0 == m_backgroundInitialized) { &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(0); }
        }
&lt;span style="color: rgb(0,0,255)"&gt;        #region&lt;/span&gt; Dispose
        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Dispose() {
            Dispose(&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;);
            &lt;span style="color: rgb(43,145,175)"&gt;GC&lt;/span&gt;.SuppressFinalize(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;);
        }
        ~ActiveObject() {
            Dispose(&lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;);
        }
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Dispose(&lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; disposing) {
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (disposing) {
                m_pipe.AddInput(&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(() =&amp;gt; { m_backgroundFinished = 1; }));
                m_pipe.CloseInput();
                m_thread.Join();
            }
        }
&lt;span style="color: rgb(0,0,255)"&gt;        #endregion
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; InitializeAndRunBackgroundThread() {
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_affinity, &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ThreadAffinity&lt;/span&gt;());
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_pipe, &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;PipeSingleReader&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;gt;());
            InitializeMembersInBackground();
            &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_backgroundInitialized, 1);
            RunBackgroundActions();
        }
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunBackgroundActions() {
            &lt;span style="color: rgb(0,0,255)"&gt;do&lt;/span&gt; {
                RunFuture(m_pipe.GetNextOutput());
            } &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (0 == m_backgroundFinished);
            &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; future;
            &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (m_pipe.TryGetOutput(&lt;span style="color: rgb(0,0,255)"&gt;out&lt;/span&gt; future)) {
                RunFuture(future);
            }
        }
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunFuture(&lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; future) {
            &lt;span style="color: rgb(0,0,255)"&gt;try&lt;/span&gt; {
                m_affinity.Check();
                future.Run();
            }
            &lt;span style="color: rgb(0,0,255)"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex) {
                OnBackgroundUnhandledException(ex);
            }
        }
        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt; RunInBackground(&lt;span style="color: rgb(43,145,175)"&gt;Action&lt;/span&gt; action) {
            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; f = &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(action);
            m_pipe.AddInput(f);
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; f;
        }
        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;lt;T&amp;gt; RunInBackground&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; func) {
            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; f = &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.CreateNoRun(func);
            m_pipe.AddInput(f);
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; f;
        }
        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;virtual&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; InitializeMembersInBackground() {
        }
        &lt;span style="color: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;abstract&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OnBackgroundUnhandledException(&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex);
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8344411" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Active+Object/default.aspx">Active Object</category></item><item><title>PipeSingleReader</title><link>http://blogs.msdn.com/jaredpar/archive/2008/03/02/pipesinglereader.aspx</link><pubDate>Sun, 02 Mar 2008 20:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7991808</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/7991808.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=7991808</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=7991808</wfw:comment><description>&lt;P&gt;Before we can get to building an Active Object implementation, there are some more primitive structures we need to define.&amp;nbsp; Active Objects live on a separate thread where every call is executed in a serialized fashion on that thread.&amp;nbsp; The next primitive will allow us to easily pass messages in the form of delegates from the caller to the background thread.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The structure must support adding messages from N callers but only needs to support 1 reader (the active object).&amp;nbsp; Successive writes from the same thread should add the items in their respective order.&amp;nbsp; The input end of the pipe can be closed while leaving the output end active.&amp;nbsp; This allows for the consumer to deterministically reach an end state while not ignoring any any input.&lt;/P&gt;
&lt;P&gt;The name of the structure is PipeSingleReader.&lt;/P&gt;
&lt;P&gt;Designing a thread safe mutable structure is significantly different from a non-thread safe or immutable data structure.&amp;nbsp; There is a significant temptation to apply existing patterns.&amp;nbsp; We should question every pattern we apply to these collections.&amp;nbsp; Many of these patterns lead us to design API's which encourage bad programming practices. &lt;/P&gt;
&lt;P&gt;Our existing collection patterns are designed around structures which behave deterministically on any given thread because they are 1) Immutable or 2) designed for single thread use only.&amp;nbsp; Many of these concepts do not apply to mutable thread safe collections because they are constantly being accessed and mutated by multiple threads.&amp;nbsp; This gives them the &lt;EM&gt;appearance &lt;/EM&gt;of behaving non-deterministically with respect to a given thread.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Take the member Count for instance.&amp;nbsp; This is found on virtually every collection class in the BCL.&amp;nbsp; Yet having it on a mutable thread safe collection class only leads to programming errors.&amp;nbsp; The only reason to have a member such is Count is to use it to make a decision.&amp;nbsp;&amp;nbsp; However in a mutable thread-safe collection making a decision off of this value is wrong.&amp;nbsp; The value can and will change between any two instructions in code.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Take the example below.&amp;nbsp; Just because the Count is &amp;gt;0 in the if block has no dependable relevance to what the value will be inside the if block.&amp;nbsp; &lt;/P&gt;&lt;PRE class=code&gt;            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;var&lt;/SPAN&gt; ThreadSafeList&amp;lt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&amp;gt; col = GetList();
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;( col.Count &amp;gt; 0 )
            {
                &lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//...
&lt;/SPAN&gt;            }&lt;/PRE&gt;
&lt;P&gt;To guard against this and have users of the collections avoid the pit of failure members such as Count should not appear on mutable thread-safe collections.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Instead we'll design API's around the functionality of this structure.&amp;nbsp; The input end of the structure is straight forward.&amp;nbsp; All writers want is to add data.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The output end is more interesting.&amp;nbsp; The end goal is to read output from the pipe but how to deal with cases where there is no data.&amp;nbsp;&amp;nbsp; Some programs will want to check for data, others block until data is available.&amp;nbsp;&amp;nbsp; We can define three methods to satisfy most scenarios&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;WaitForOutput - void method which blocks until input is available&lt;/LI&gt;
&lt;LI&gt;GetNextOutput - Blocks until input is available and returns it&lt;/LI&gt;
&lt;LI&gt;TryGetOutput - Returns immediately.&amp;nbsp; If output is available it will be returned.&lt;/LI&gt;&lt;/UL&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;PipeSingleReader&lt;/SPAN&gt;&amp;lt;T&amp;gt; : &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;IDisposable&lt;/SPAN&gt; {
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;ThreadAffinity&lt;/SPAN&gt; m_affinity = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;ThreadAffinity&lt;/SPAN&gt;();
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Queue&lt;/SPAN&gt;&amp;lt;T&amp;gt; m_queue = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Queue&lt;/SPAN&gt;&amp;lt;T&amp;gt;();
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;AutoResetEvent&lt;/SPAN&gt; m_event = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;AutoResetEvent&lt;/SPAN&gt;(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;false&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt; m_lock = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;();
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt; m_inputClosed;

        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; PipeSingleReader() {
        }

&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;        #region&lt;/SPAN&gt; Dispose
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; Dispose() {
            Dispose(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;);
            &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;GC&lt;/SPAN&gt;.SuppressFinalize(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;);
        }
        ~PipeSingleReader() {
            Dispose(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;false&lt;/SPAN&gt;);
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; Dispose(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt; disposing) {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (disposing) {
                m_event.Close();
            }
        }
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;        #endregion
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; WaitForOutput() {
            m_affinity.Check();
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;do&lt;/SPAN&gt; {
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (m_lock) {
                    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (m_queue.Count &amp;gt; 0) {
                        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;;
                    }
                }
                m_event.WaitOne();
            } &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;while&lt;/SPAN&gt; (&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;);
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; T GetNextOutput() {
            m_affinity.Check();
            T data;
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;while&lt;/SPAN&gt; (!TryGetOutput(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;out&lt;/SPAN&gt; data))
            {
                m_event.WaitOne();
            } 
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; data;
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt; TryGetOutput(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;out&lt;/SPAN&gt; T value) {
            m_affinity.Check();
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (m_lock) {
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (m_queue.Count == 0) {
                    value = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;default&lt;/SPAN&gt;(T);
                    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;false&lt;/SPAN&gt;;
                }

                value = m_queue.Dequeue();
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;;
            }
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; AddInput(T value) {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (m_lock) {
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (m_inputClosed) {
                    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Input end of pipe is closed"&lt;/SPAN&gt;);
                }
                m_queue.Enqueue(value);
            }
            m_event.Set();
        }
        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; CloseInput() {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (m_lock) {
                m_inputClosed = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;;
            }
        }
    }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;At a quick glance it may seem odd in GetNextOutput that I loop around m_event being set and TryGetOutput.&amp;nbsp; Why loop?&amp;nbsp; Shouldn't a single check for the settness of m_event be enough?&amp;nbsp; In this case no.&amp;nbsp; The reason why is TryGetOutput will remove output from the queue without resetting the settness of m_event.&amp;nbsp; Thus m_event can be set without actually having any data in m_queue.&amp;nbsp; In general the implementation must treat m_event being set as the possibility of data rather than a guarantee.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;This implementation uses locks to synchronize access to the data.&amp;nbsp; In general I'm a fan of avoiding locks when possible since it's very easy to miss trivial cases.&amp;nbsp; Next time we'll look at an implementation of PipeSingleReader which avoids the use of locks.&amp;nbsp; &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7991808" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Active+Object/default.aspx">Active Object</category></item><item><title>Active Objects and Futures</title><link>http://blogs.msdn.com/jaredpar/archive/2008/01/28/active-objects-and-futures.aspx</link><pubDate>Tue, 29 Jan 2008 07:57:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7298431</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/7298431.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=7298431</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=7298431</wfw:comment><description>&lt;p&gt;Herb Sutter gave one of my favorite and inspiring presentations.&amp;#160; It is called &amp;quot;The Free Lunch is Over&amp;quot;.&amp;#160; The original article can be found &lt;a href="http://www.gotw.ca/publications/concurrency-ddj.htm"&gt;here&lt;/a&gt;.&amp;#160; My first encounter though came from his &lt;a href="http://www.pluralsight.com/blogs/hsutter/archive/2005/10/25/15903.aspx"&gt;PDC presentation&lt;/a&gt; and highly recommend viewing that as well.&lt;/p&gt;  &lt;p&gt;The part that interested me the most about the talk was two new threading abstractions I hadn't encountered before.&amp;#160; Future's and ActiveObjects.&amp;#160; One of the basic premise is that concurrency should be grep`able and somewhat declarative.&amp;#160; The act of calling a method on a background thread and later waiting for it to complete should be simple, not complicated.&amp;#160; &lt;/p&gt;  &lt;p&gt;Asynchronous programming is one of my favorite aspects of computing.&amp;#160; What interests me the most is how asynchronous programming can be useful for UI.&amp;#160; My biggest pet peeve is when UI hangs because an operation call, or network operation takes too long.&amp;#160; Why not make multi-threading easy and give users a way to cancel out of these operations???&amp;#160; Or start loading on the background thread instead of waiting for the user to perform a specific.&amp;#160; Hopefully over the next month or so I'll lay out some utilities and classes building on Futures and Active Objects that will do precisely this.&lt;/p&gt;  &lt;p&gt;Future's are actions where work can be done now, but the result is not needed until a future time.&amp;#160; Work occurs on a separate thread and the results can be easily joined once work is complete.&lt;/p&gt;  &lt;pre class="code"&gt;            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; f = &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;.Create(() =&amp;gt; LongCalculation());
            &lt;span style="color: rgb(0,128,0)"&gt;// ...
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; result = f.Wait();&lt;/pre&gt;

&lt;p&gt;Future's are now exposed via the &lt;a href="http://blogs.msdn.com/pfxteam/archive/2007/11/29/6558413.aspx"&gt;Parallel Extension&lt;/a&gt; team's work.&amp;#160; You can download the CTP off of their web site and get to work. &lt;/p&gt;

&lt;p&gt;ActiveObjects are objects which only expose Asynchronous functions where the return value is exposed as a Future.&amp;#160; So instead of &lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; GetName()&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;You would have&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(43,145,175)"&gt;Future&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;&amp;gt; GetName()&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;An ActiveObject essentially lives on or owns a thread.&amp;#160; All operations are queued up and processed one at a time.&amp;#160; Since only one action at a time can be executing the object internals don't have to use locks or consider many types of race conditions.&amp;#160; In fact if your return types are immutable a great many threading concerns go out the window.&amp;#160; Yet all of the calls are inherently asynchronous so callers can get the result only when they are needed.&amp;#160; The best of both worlds.&amp;#160; &lt;/p&gt;

&lt;p&gt;Both of these provide significant advantages over the &amp;quot;lock before use&amp;quot; patterns.&amp;#160; In my experience I find these to be hard to maintain and lead to difficult to track down bugs.&amp;#160; I can't tell you how many times I've gone through someone else's code, or even my own, and wondered ...&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Did they forget to lock here or is this an optimization?&lt;/li&gt;

  &lt;li&gt;Is a join needed here or can these terminate at separate times?&lt;/li&gt;

  &lt;li&gt;OK I need to touch that variable, can it be accessed in multiple threads or is it safe? &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Futures/Active Objects on the other hand are a bit more declarative and straight forward to understand than plain old locks.&amp;#160; They allow you to do away with many uses of plain old locking.&amp;#160; Don't confuse this with me saying they are a cure all for threading.&amp;#160; They're not.&amp;#160; But in my experiences I've found them to be a significant upgrade.&amp;#160; &lt;/p&gt;

&lt;p&gt;Over the next month or so I'll be laying out the design for a basic ActiveObject implementation.&amp;#160; We will likely have to deviate off of the Parrallel Extension work to get certain behaviors but the concepts map well.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7298431" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Active+Object/default.aspx">Active Object</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Futures/default.aspx">Futures</category></item></channel></rss>