<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>mutex &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/mutex/</link>
	<description>Feed of posts on WordPress.com tagged "mutex"</description>
	<pubDate>Sat, 05 Dec 2009 12:40:49 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Intel memory ordering, fence instructions, and atomic operations.]]></title>
<link>http://peeterjoot.wordpress.com/2009/12/04/intel-memory-ordering-fence-instructions-and-atomic-operations/</link>
<pubDate>Sat, 05 Dec 2009 02:32:25 +0000</pubDate>
<dc:creator>peeterjoot</dc:creator>
<guid>http://peeterjoot.wordpress.com/2009/12/04/intel-memory-ordering-fence-instructions-and-atomic-operations/</guid>
<description><![CDATA[[Click here for a PDF of this post with nicer formatting] Motivation. In my recent post about memory]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://sites.google.com/site/peeterjoot/math2009/dekker.pdf?revision=2">[Click here for a PDF of this post with nicer formatting]</a></p>
<h1>Motivation.</h1>
<p>In my <a href="http://peeterjoot.wordpress.com/2009/11/29/an-attempt-to-illustrate-differences-between-memory-ordering-and-atomic-access/">recent post about memory barriers and atomics</a>(or <a href="http://sites.google.com/site/peeterjoot/math2009/atomic.pdf?revision=1">the PDF version here</a>), I stated a number of times that x86 (or x86-64) was among the platforms that provided strongly ordered memory access.</p>
<p>I have an old friend, we&#8217;ll call him Joe here, who uses a Dekker&#8217;s algorithm program to demonstrate to his computer architecture class that this is in fact not the case.  This flies in the face of all my experience with DB2, so it becomes immediately interesting from both a theoretical point of view and a correctness issue for the mutual exclusion code that I maintain for our product, much of which I have written or rewritten myself over the years.</p>
<p>When talking to Joe, I had stated that I thought the MFENCE, LFENCE, and SFENCE instructions were only required for SSE.  I said the Intel memory model already provided an ordering guarantee, and that no fence instructions were required.  My mistaken recollection was that these were effectively no-ops for non-SSE code and special memory configurations used in privileged mode.</p>
<p>The reasons for my misunderstanding is explored here, including why DB2 mutex implementation does not require these fences.  I knew this once back in the days when the P6 was new, but I had forgotten all this processor-ordering business, demonstrated by an implementation of the Dekker algorithm on a recent intel multiprocessor machine.</p>
<p>The fact that DB2 mutex and atomic code does not use memory fence instructions can likely be extrapolated to any other intel targeting mutex implementation regardless of the products or library that providing the mutex.  We all are restricted to the same tricks and toolbox, the Intel instruction set specification itself, and we will see that the easiest and fastest way to implement a mutex does not require these fence instructions and why.</p>
<p>Dekker&#8217;s algorithm uses a pair of flags and a turn variable to control access to a critical section.  While this algorithm is of historical interest, and the subject of classroom discussions, I doubt that anybody uses it for anything real.  There are a number of flaws, the most obvious of which is that it allows only the synchronization of a pair of threads.  A less obvious limitation is that it assumes strongly ordered memory access.  These strongly ordered memory access assumptions can be subdivided into two cases.  The first of these ordering assumptions is that we require acquire and release fences to ensure the data guarded by the critical section is not accessed outside of the critical section.  The second of the ordering assumptions is the trickier part, since there are memory ordering access assumptions in the algorithm itself.  I would expect that these have been studied in depth by others, but even without such study, running the code with and without a few types of barrier instructions demonstrates conclusively that the algorithm is flawed as is, at least on intel hardware.</p>
<p>Trolling the internet I found references stating that Dekker&#8217;s algorithm can be faster than mutex methods build using the more modern hardware specific atomic instructions.  Experiment shows that this is not necessarily true.  To demonstrate this requires some low level knowledge.  One cannot compare Dekker&#8217;s to pre-canned blackbox library methods like pthread_mutex_lock, where the implementation details are unknown.  With Dekker&#8217;s algorithm doing brute force spinning, a good comparison requires that the mutex implementation also do so.  Results comparing the Dekker&#8217;s method and some alternatives will be supplied below.  These are of general interest since the question of how expensive is an atomic compared to a mutex occurs frequently (at least in DB2 development).</p>
<h1>What the intel documentation says about ordering.</h1>
<p>The intel architecture documentation and instruction set reference do detail memory fence instructions.  There are actually three such instructions in current CPUs, very much like powerpc, which is intrinsically weakly ordered, and (except for the Power6 blip) always has been.  Intel provides a bidirectional fence instruction MFENCE, an acquire fence LFENCE, and a release fence SFENCE.  The MFENCE instruction dates way back to the P6 where intels first weakened their memory ordering (defining their unique processor ordering).</p>
<p>Here is what the instruction set reference says about these fence instructions</p>
<ul>
<li> <a href="http://www.intel.com/Assets/PDF/manual/253667.pdf">SFENCE</a> [1]</li>
</ul>
<p>Performs a serializing operation on all store-to-memory instructions that were issued prior the SFENCE instruction. This serializing operation guarantees that every store instruction that precedes in program order the SFENCE instruction is globally visible before any store instruction that follows the SFENCE instruction is globally visible. The SFENCE instruction is ordered with respect store instructions, other SFENCE instructions, any MFENCE instructions, and any serializing instructions (such as the CPUID instruction). It is not ordered with respect to load instructions or the LFENCE instruction.</p>
<ul>
<li><a href="http://www.intel.com/Assets/PDF/manual/253666.pdf">LFENCE</a></li>
</ul>
<p>Performs a serializing operation on all load-from-memory instructions that were issued prior the LFENCE instruction. This serializing operation guarantees that every load instruction that precedes in program order the LFENCE instruction is globally visible before any load instruction that follows the LFENCE instruction is globally visible. The LFENCE instruction is ordered with respect to load instructions, other LFENCE instructions, any MFENCE instructions, and any serializing instructions (such as the CPUID instruction). It is not ordered with respect to store instructions or the SFENCE instruction.</p>
<ul>
<li><a href="http://www.intel.com/Assets/PDF/manual/253666.pdf">MFENCE</a></li>
</ul>
<p>Performs a serializing operation on all load-from-memory and store-to-memory instructions that were issued prior the MFENCE instruction. This serializing operation guarantees that every load and store instruction that precedes in program order the MFENCE instruction is globally visible before any load or store instruction that follows the MFENCE instruction is globally visible. The MFENCE instruction is ordered with respect to all load and store instructions, other MFENCE instructions, any SFENCE and LFENCE instructions, and any serializing instructions (such as the CPUID instruction).</p>
<p>A condensed and somewhat more human friendly version of these statements can be found in the <a href="http://www.intel.com/Assets/PDF/manual/253668.pdf">system programming guide</a>.</p>
<p>The functions of these instructions are as follows:</p>
<ul>
<li> SFENCE</li>
</ul>
<p>Serializes all store (write) operations that occurred prior to the SFENCE instruction in the program instruction stream, but does not affect load operations.</p>
<ul>
<li> LFENCE</li>
</ul>
<p>Serializes all load (read) operations that occurred prior to the LFENCE instruction in the program instruction stream, but does not affect store operations.</p>
<ul>
<li> MFENCE</li>
</ul>
<p>Serializes all store and load operations that occurred prior to the MFENCE instruction in the program instruction stream.</p>
<p>Now, we do not use MFENCE anywhere in DB2 code.  It was once tried with the hope of trying to fix some code that had a mysterious bug, but it did not help.  We replaced the problematic code with simpler more trustworthy stuff.  Reading the intel docs where there is no immediate sign that one would not have to use fence instructions for mutual exclusion code, one may think we avoid MFENCE since we prereq later hardware that supplies the LFENCE and SFENCE instructions and we use those instead.  While the prereq statement is now probably true, but that is not why we do not use MFENCE.  I will return to this later.  For now lets instead explore some code.</p>
<h1>The Dekker algorithm, implementation, and a non-correctness test case.</h1>
<p>The <a href="http://en.wikipedia.org/w/index.php?title=Dekker%27s_algorithm&#38;oldid=329540788">Wikipedia Dekker&#8217;s algorithm page</a> describes the algorithm.  An implementation of the code described in that article, pretty much line for line except for some comment addition, is the following</p>
<pre class="brush: cpp;">struct FLAG_STATE
{
   volatile int flag ;

   char dummy[120] ; // cacheline separator.
} ;

FLAG_STATE g[2] ;
volatile int turn = 0 ;

void my_lock( int tid )
{
   int other = 1 - tid  ;

   g[tid].flag = true ; // I want in the CS.

   FENCE ;

   while ( true == g[other].flag )
   {
      //
      // The other guy is in the CS or wants in.
      //
      if ( turn != tid )
      {
         // he said he wants it, but hasn't given me my turn yet.
         //
         // let him have a chance.
         //
         g[tid].flag = false ;

         // wait till he gives me my turn
         while ( turn != tid )
         {
         }

         // he's about to give up his turn or has, so I can say I want it again.
         //
         // I'll now spin on his flag changing (though I could miss it if
         // unlucky and have to start all over if he tries again fast).
         //
         g[tid].flag = true ;
      }
   }
}

void my_unlock( int tid )
{
   int other = 1 - tid  ;

   turn = other ;

   g[tid].flag = false ;
}</pre>
<p>This is called with tid values of 0 or 1 depending on which of the two contenders are trying to get into the mutual exclusion battle.  Only two such contenders are allowed.</p>
<p>This algorithm is costly.  Compared to a serially executing the increment instructions without locking or contention this took 76 times more time on the system I tried it on.  This isn&#8217;t exactly a fair comparision since executing the dekker code without contention would also be faster.</p>
<p>Observe that there is no release barrier and the beginning of my_unlock and no acquire barrier at the end of my_lock, so one would expect this to fail on a weakly ordered platform like PowerPC, even if the algorithm itself guarantees mutual exclusion, and it is implemented properly.  Failure here means allowing access of the data protected by the mutex outside of the mutual exclusion region.</p>
<p>If one presumes that the compiler has dumbly emitted the code in program order, and if one also presumes that this algorithm and the Wikipedia description of it is sound, and that it was implemented correctly, then one would expect this to work on a platform with strong memory ordering.  On a platform with weak ordering acquire and release fences should be required for correctness, and possibly more.</p>
<p>Does this code work?  This code was compiled without optimization, hoping that the compiler will not reorder anything, but I did not check the assembly listings to be sure.  Unoptimized code is usually spectularily bad, dumb and straightforward, so it is not entirely unreasonable to presume that the instructions are all generated in program order.  Of a set of three runs with FENCE=MFENCE,SFENCE,LFENCE,NOFENCE where two threads looped incrementing an integer variable ten million times each, this code failed the consistency checking with LFENCE and NOFENCE.  With SFENCE this code ran correctly twice, but in one of the iterations produced a count 7 less than the required target value for a mutex protected counter incremented a well known number of times.  This does not demonstrate that this algorithm is correct with FENCE=MFENCE, but it is decisively busted otherwise.</p>
<p>Now, Joe had a different implementation of Dekker&#8217;s that did not do the turn hand off.  His looked like so (after tweaking it a bit to make it look like mine for consistency)</p>
<pre class="brush: cpp;">void my_lock( int tid )
{
   g[tid].flag = 1 ;
   turn = 1-tid ;

   FENCE ;

   while( g[1-tid].flag )
   {
      if ( turn == tid )
      {
         break ;
      }
   }
}

void my_unlock( int tid )
{
   g[tid].flag = 0 ;
}</pre>
<p>This code was also run with FENCE=MFENCE,SFENCE,LFENCE,NOFENCE.  It passes with MFENCE and SFENCE, and fails with LFENCE and NOFENCE like the implementation of the Wikipedia listing.</p>
<p>Does this mean that his code is correct with SFENCE or even correct with MFENCE or the implementation of the wikipedia listing is correct with MFENCE?  That is harder to say.  The correctness of either set of code even with FENCE=MFENCE is not prove that the code is correct by a few successful test runs.  What is interesting about all this is that Joe&#8217;s assertion that memory ordering is weakened enough to require fences for correctness now seems very plausible.</p>
<p>Note that this code is also not cheap.  It is better than the listing originating with the vanilla wikipedia article, but was also clocked at 47 to 67 times slower than uncontended serial access.  Again not entirely a fair comparison since this code would also do better serially.</p>
<p>We still have to understand why this is true here and not for atomic instruction based mutual exclusion.  Additionally in many years of running DB2 on all varieties of intel and amd64 hardware we haven&#8217;t seen signs of requiring fence instructions for our non-mutex atomic code.  That doesn&#8217;t mean we do not have such problems, since it could be very hard to identify this sort of error.  Keeping suspense high, the reasons for this a bit more, and instead show what is required to implement a mutex a more natural way on intel.</p>
<h1>Using an lock xchg mutex</h1>
<p>Here is a dumb busy spinlock implementation on intel using the GCC compiler</p>
<pre class="brush: cpp;">volatile char lock_word = 0 ;

void my_lock( int tid_ignored )
{
   char old_value ;
   char new_value = 1 ;

   do {
      __asm__ __volatile__( &#34;lock; xchgb  %0,%1\n\t&#34; :
                            &#34;=r&#34; (old_value), &#34;+m&#34; (lock_word) :
                            &#34;0&#34; (new_value) :
                            &#34;cc&#34; ) ;

   } while ( 1 == old_value ) ;
}

void my_unlock( int tid_ignored )
{
   lock_word = 0 ;
}</pre>
<p>Compared to unlocked serial uncontended increment, using this as a replacement for the Dekker mutex (ie. variations of that with enough fencing inserted to make the code appear functional), this runs about 16 times slower.  This is already much better than the Dekker code (either the wikipedia based version or Joe&#8217;s), but it would again be interesting to check how much worse that gets without contention.  How much of this bus locked atomic exchange cost is aggrivated by the contention itself.</p>
<h1>Using an lock xadd for increment</h1>
<p>For this simple task of doing thread safe increment, we can do much better easily.  Here&#8217;s some code to do the increment without using a mutex</p>
<pre class="brush: cpp;">volatile unsigned foo = 0 ;

inline void my_increment()
{
   unsigned old_value ;
   unsigned add_value = 1 ;

   __asm__ __volatile__( &#34;lock ; xaddl %0,%1\n\t&#34; :
                         &#34;=r&#34;(old_value), &#34;+m&#34; (foo) :
                         &#34;0&#34; (add_value) :
                         &#34;cc&#34; ) ;
}

void my_lock( int tid_ignored )
{
}

void my_unlock( int tid_ignored )
{
}</pre>
<p>Executed serially a loop of such increments was measured at about 3 times slower than the plain old increment.  That baseline timing actually used this code but with the bus locking signal flag removed.  It costs us to do the bus locking even if we do not need it.  Adding contention into the mix, the loop of these increments now clocks in at about 7-9 times slower than the unlocked uncontended code.  This provides some rough idea of how much the correct code minimally costs us.  All of these times will be summarized below.</p>
<h1>Performance comparision.</h1>
<p>Here&#8217;s a summary of the costs of the various increment methods in order of added cost.  This is with a plain old xadd as a baseline (no lock prefix).</p>
<ul>
<li> xadd</li>
</ul>
<p>This was the baseline time.</p>
<ul>
<li> lock xadd (ie. atomic add.  No fences.  No contention).</li>
</ul>
<p>3x baseline time.</p>
<ul>
<li> lock xadd (ie. atomic add.  w/ contention)</li>
</ul>
<p>7-9x baseline time.</p>
<ul>
<li> lock xchg (spinlock mutex w/contention)</li>
</ul>
<p>16x baseline time.</p>
<ul>
<li> Joe&#8217;s implementation of dekker w/ [sm]fence (mutex w/ contention)</li>
</ul>
<p>47-67x baseline time.</p>
<ul>
<li> my implementation of wikipedia&#8217;s dekker algorithm w/ mfence (mutex w/ contention)</li>
</ul>
<p>76x baseline time.</p>
<h1>Why no fences are required for the lock prefixed atomic or spinlock mutex code.</h1>
<p>I have held off talking about what makes the lock xchg based code work without requiring fences.  The reason for this can be found in volume 3, and is somewhat anticlimatic</p>
<p>8.2.2 &#8220;Reads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions.&#8221;</p>
<p>The DB2 mutex code (and likely many other similar implementations) is fine as is.  Our &#8220;atomic&#8221; library which also uses lock flagged atomic instructions of various sorts (xadd, cmpxchg, &#8230;) also requires no additional or specific barriers as it does on ia64 or ppc.  This atomic code MUST also use the lock prefix or else it is demonstratably busted when there is contention, so it imposes a minimum costs of approximately 3x in order to be safe when there is contention.</p>
<h1>Conclusions.</h1>
<p>I think that it is fair to say that the processor ordering model of modern intel cpus is demonstratably unordered requiring barriers in some cases.  It would be interesting to identify exactly where in the Dekker algorithm there are ordering dependencies.  I suspect such a study is already in the literature, although it is of little practical value since bus locked intel instructions are superior.</p>
<p>While some timing differences were presented, these may just provide a ballpark performance comparision.  Doing a performance comparision without optimization is of dubious value.  It may possible that since only relative times were compared there may be some validity to the comparision otherwise, but that really requires doing it to be sure.  It would be interesting to redo at least the xchg vs xadd vs baseline cases with optimization and see the differences.</p>
<p>The weaker aspects of intel memory ordering are hidden from code that exploits thier bus locked instructions for atomic and mutex library implementations.  Requiring that any dependence on ordering is explicitly guarded by critical sections bounded by bus locked instructions is the key to correctness.  As I write this it occurs to me that since not all of the DB2 mutex types use an atomic store for the lock release we may actually have an issue.  That implicitly depends on ordering, so there may actually be a correctness issue to be investigated and this will have to be explored.  Yet another reason not to write this sort of code oneself if it can be avoided.  It is so easy to get it wrong and finding a demonstration that this one time in 20 million has blown up can be very hard.</p>
<h1>References</h1>
<p>[1] Intel. Intel architectures software developer&#8217;s manuals.htm [online]. 2009. [Online; accessed 4-December-2009].  <a href="http://www.intel.com/products/processor/index.htm">http://www.intel.com/products/processor/index.htm</a>.</p>
<p>[2] Wikipedia. Dekker&#8217;s algorithm &#8212; wikipedia, the free encyclopedia [online]. 2009. [Online; accessed 4-December-2009].  <a href="http://en.wikipedia.org/w/index.php?title=Dekker%27s_algorithm&#38;oldid=32%9540788">http://en.wikipedia.org/w/index.php?title=Dekker%27s_algorithm&#38;oldid=32%9540788</a>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[An attempt to illustrate differences between memory ordering and atomic access.]]></title>
<link>http://peeterjoot.wordpress.com/2009/11/29/an-attempt-to-illustrate-differences-between-memory-ordering-and-atomic-access/</link>
<pubDate>Mon, 30 Nov 2009 03:07:56 +0000</pubDate>
<dc:creator>peeterjoot</dc:creator>
<guid>http://peeterjoot.wordpress.com/2009/11/29/an-attempt-to-illustrate-differences-between-memory-ordering-and-atomic-access/</guid>
<description><![CDATA[[Click here for a PDF alternative to this post] Abstract. Discussion of problems and solutions assoc]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><a href="http://sites.google.com/site/peeterjoot/math2009/atomic.pdf">[Click here for a PDF alternative to this post]</a></p>
<h1>Abstract.</h1>
<p>Discussion of problems and solutions associated with correct atomic and memory barrier usage has been a recurrent theme of many cubicle chats within DB2 development, and I will attempt to describe here part of what I&#8217;ve learned in some of those chats.  As a software developer, not a hardware implementer, I cannot pretend to understand more than a fragment of this topic, but it is enough to be dangerous and perhaps help others be dangerous too.</p>
<h1>Introduction</h1>
<p>Use of atomic instructions for manipulating &#8220;word&#8221; (or smaller) size quantities, avoiding the use of mutual exclusion library functions is becoming increasingly easy for developers.  This hasn&#8217;t made the using atomic instructions or library methods correctly any less difficult or error prone.</p>
<p>Information on how to use these written for an average Joe developer is hard to come by.  What can be easily found is good detailed low level information targeted at or written by operating system kernel extension writers or compiler developers [1], or language implementers and designers [2] [3] [4] [5] [6] [7] [8].  </p>
<p>The driving reason for most atomic usage is lock avoidance, and a desire for performance drives most use of atomic methods.  When atomic operations are used to replace lock protected updates, this can change the semantics of the program in subtle ways that are difficult to understand or get right.  In particular, additional use of memory barrier instructions or library methods may be required to correctly replace lock protected updates.</p>
<p>It is assumed here that most use of atomics is done for performance based lock (mutex) avoidance.  An attempt to cheat, writing atomic based code that avoids a lock, unfortunately requires some understanding of what that lock provided in the first place.  Here the meaning of, requirements for, and implementation of, a lock are discussed.  The implementation of an atomic operation is then discussed, and we then move on to an enumeration of the types of memory barrier instructions and their effects.  Finally, unless the assembly samples provided have not scared aways the reader, we cover a simple non-mutex example of barrier usage.  This example uses an atomic to signal completion of a previous update and requires memory barriers for correctness on weakly ordered systems.</p>
<h1>Why do I need locking or atomics?</h1>
<p>Why would you want to use an atomic?  If you are reading because you want to know a bit about memory barrier usage, you probably already know.  However, for illustrative purposes, consider the very simplest example of non-thread-safe updates of an integer in shared memory.</p>
<pre class="brush: cpp;">
   pShared-&#62;x++ ;
</pre>
<p>Any such operation requires three steps.  Read the value, change the value, update the value in memory.  On some platforms this is explicit and obvious since the value will have to be read into a register before making the update.  Here&#8217;s a PowerPC example that illustrates this</p>
<pre class="brush: cpp;">
    lwz      r4=(sharedMem)(r3,0)    ; r3 == pShared ; (r3,0) == *pShared
    addi     r0=r4,1
    stw      (sharedMem)(r3,0)=r0
</pre>
<p>The value in the memory address contained in the register r3 (offset by zero bytes) is loaded into a register (r4).  One is added to this (into r0), and the value is stored back into the original memory location.  Running on a CISC system this may all appear as one instruction, but it still requires the same number of steps internally.</p>
<p>Suppose you had a threaded application where this counter variable is updated routinely by many threads as they complete some routine and commonplace action.  What can now happen, interleaved (with time downwards) across threads may now look like this</p>
<pre class="brush: cpp;">
T0: pShared-&#62;x = 0

T1: R1 = pShared-&#62;x (R1 = 0)
    R1 = R1 + 1
T1: 

T2: R2 = pShared-&#62;x
    R2 = R2 + 1
    pShared-&#62;x = R2

T3: R3 = pShared-&#62;x (assume T3 &#34;sees&#34; T2's update, a value of 1)
    R3 = R3 + 1
    pShared-&#62;x = R3

T4: R4 = pShared-&#62;x (assume T4 &#34;sees&#34; T3's update, a value of 2)
    R4 = R4 + 1
    pShared-&#62;x = R4

T1:
    pShared-&#62;x = R1 (T2 and T3's updates are now clobbered)
</pre>
<p>We had four updates to the counter after the initialization, but the counter value only goes up by one.  Uncontrolled updates like this can oscillate in peculiar fashions, and something that may be expected to be monotonically increase perhaps only trends upwards on average.  If the counter is not heavily contended you may even fluke out, running for a long time before getting unlucky with an inconsistent update of some sort.</p>
<p>This example leads us nicely to requirements for locking or atomic methods to protect such updates.</p>
<h2>Use of locking to make the read, change update cycle safe.</h2>
<p>Mutual exclusion mechanisms go by many names, locks, mutexes, critical sections, and latches to name a few.  System libraries usually provide implementations, a common example of which are the Unix pthread_mutex methods.  The unsafe increment above can be made thread safe, protecting it with such a guard.  With error handling omitted for clarity, a modified example could be as follows</p>
<pre class="brush: cpp;">
   pthread_mutex_lock( &#38;m ) ;

   pShared-&#62;x++ ;

   pthread_mutex_unlock( &#38;m ) ;
</pre>
<p>If all threads only ever updates this value we can use a similar bit of code to read and do something with the read value</p>
<pre class="brush: cpp;">
   pthread_mutex_lock( &#38;m ) ;

   v = pShared-&#62;x ;

   pthread_mutex_unlock( &#38;m ) ;

   doSomethingWithIt( v ) ;
</pre>
<p>No two sequential samplings of the shared variable x would see the value go down was possible in the uncontrolled update case.  </p>
<p>Now, you may ask why a mutex is required for the read.  If that is just a single operation, why would it matter?  That&#8217;s a good question, but not easy to answer portably.  If your variable is appropriately aligned and of an appropriate size (practically speaking this means less than or equal to the native register size) and you aren&#8217;t compiling with funky compiler options that turn your accesses into byte accesses (such options do exist), then you may be able to do just that.  However, if that variable, for example, is a 64-bit integer and you are running on a 32-bit platform, then this read may take two instructions and you have a risk of reading the two halves at different points in time.  Similarly, suppose you were doing a 32-bit integer read, but that 32-bit integer was aligned improperly on a 16-bit boundary (on a platform that allows unaligned access), then your apparent single read may internally require multiple memory accesses (perhaps on different cache lines).  This could cause the same sort of split read scenario.</p>
<p>You probably also need to declare your variable volatile and also have to be prepared to deal with a few other subtleties if lock avoidance on read is going to be attempted.  By the end of these notes some of those subtleties will have been touched on.</p>
<p>To make the story short, it should be assumed that if you want portable correct results you need to take and release the mutex for both read and write.</p>
<h2>What does correctness cost me?</h2>
<p>Now, having corrected the increment code, what does that cost us.  Timing the following very simple single threaded program with an without the mutex code</p>
<pre class="brush: cpp;">
#include 

int x = 0 ;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER ;

int inc() ;

int main()
{
   for ( int i = 0 ; i &#60; 1000000000 ; i++ )
   {
      inc() ;
   }
}

int inc()
{
#if defined USE_THE_MUTEX
   pthread_mutex_lock( &#38;m ) ;
#endif

   x++ ;

   int v = x ;

#if defined USE_THE_MUTEX
   pthread_mutex_unlock( &#38;m ) ;
#endif

   return x ;
}
</pre>
<p>I find that this billion sets of call a function, increment a variable and return it without the mutex takes about 2.5 seconds.  With the mutex calls enabled, the total elapsed time required goes up to 8.5 seconds.  It costs over three times the time to do it with a mutex, and that&#8217;s without any contention on the mutex whatsoever!  With threads all fighting over the mutex and blocking on kernel resources when they cannot get it, this is as good as it gets.  Correctness doesn&#8217;t come cheaply.</p>
<h2>How does the lock work?</h2>
<p>Any mutex implementation requires at least one platform specific method of performing a read, update, change cycle as if it is done without interruption.  Your hardware will likely provide a <a href="http://en.wikipedia.org/wiki/Compare-and-swap">Compare and swap</a> or <a href="http://en.wikipedia.org/wiki/Load-Link/Store-Conditional">Load Link/Store Conditional</a> (which can be used to construct higher level atomics like compare and swap).  Implementing a mutex is intrinsically unportable territory.  Older <a href="http://download.intel.com/design/intarch/manuals/24319101.pdf">intel</a> cpus did not provide a compare and swap, but one did have a bus locked exchange available (LOCK XCHG).</p>
<p><a href="http://ftp.parisc-linux.org/docs/arch/pa11_acd.pdf">HP&#8217;s PARSIC</a>, also easily argued to not be a modern architecture, only provides an atomic load and clear word instruction, also requiring 16 BYTE a aligned word.  What can be assumed is that any multiple cpu machine provides some sort of low level atomic instruction suitable for building a mutual exclusion interface.  If one were to imagine what a pthread_mutex_lock protected increment expands to, it would likely have the following form</p>
<pre class="brush: cpp;">
   T valueRead ;

   while ( ATOMIC_OP_WAS_SUCCESSFUL != SOME_INSTRUCTIONS_FOR_LOCK_ACQUISION( &#38;m ) )
   {
      // instructions (like PAUSE()) for hyper threaded cpus.

      // code for wasting a bit of time hoping to get the lock
      //   (when not running on a uniprocessor).

      // sleep or block using an OS primitive
   }

   valueRead = pShared-&#62;myLockProtectedData ;
   pShared-&#62;myLockProtectedData = valueRead + 1 ;

   SOME_INSTRUCTIONS_FOR_LOCK_RELEASE( &#38;m ) ;
</pre>
<p>Even before talking about the hardware, one has to assume that there are mechanisms available to the library writer that prevent the generated code from being reordered.  Any instructions containing accesses of myLockProtectedData must NOT occur BEFORE the ACQUISION instructions, and must NOT occur AFTER the RELEASE instructions.  If the compiler were to generate code that had the following effect</p>
<pre class="brush: cpp;">
   T valueRead = pShared-&#62;myLockProtectedData ;
   pShared-&#62;myLockProtectedData++ ;

   pthread_mutex_lock( &#38;m ) ;
   pthread_mutex_unlock( &#38;m ) ;
</pre>
<p>Things would obviously be busted.  In the case of the pthread functions we have non-inline method with unknown side effects requiring a call to an external shared library.  This appears to be enough that the lock protected data does not have to be declared volatile and can be used while the mutex is held as if in a serial programming context.  Understanding the murky language rules that give us the desired thread safe behavior is difficult (for an average Joe programmer like me).  I came to the conclusion this is not a completely well defined area, motivating a lot of the recent C++ standards work into memory consistency and threaded behavior.</p>
<p>Presuming one assumes that the compiler is laying down the instructions for this code in program order (or something sufficiently close), or that there are mechanism available to ensure this, is this enough to guarantee that we have correct program behavior?</p>
<p>It may come as a rude surprise that, depending on the instructions used to acquire and release the mutex, having the all the instructions scheduled in program order is NOT actually sufficient.  Not all hardware executes instructions in the order the compiler specifies.  We also need mechanisms to prevent the hardware from starting a memory access too early or letting a memory access complete too late.</p>
<p>This requirement for memory ordering instructions is really the whole point of this discussion.  We must have additional mechanism on top of the raw atomic instructions that only ensure read,change,update accesses on an isolated piece of memory (the lock word) can be made as if uninterpretable.</p>
<p>For illustrative purposes, suppose that one was using the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html">gcc Atomic Builtins</a> to attempt to implement a mutex.  One (generally) wrong way to use these would be</p>
<pre class="brush: cpp;">
   // the mutex data
   volatile int m = 0 ;
   #define HELD_BIT 1
   #define WAITER_BIT 2

   // Get the mutex.
   while ( true )
   {
      int prev = __sync_fetch_and_or( &#38;m, HELD_BIT ) ;

      if ( 0 == ( prev &#38; HELD_BIT ) )
      {
         break ;
      }
      // sleep and other stuff, possibly blocking and setting WAITER_BIT.
   }

   // Got the mutex, access the protected data.
   pShared-&#62;myLockProtectedData++ ;

   // Release the mutex.
   int preLockState = __sync_fetch_and_and( &#38;m, ~HELD_BIT ) ;
   assert( preLockState &#38; HELD_BIT ) ;
   if ( preLockState &#38; WAITER_BIT )
   {
      WakeUpAnyWaiters() ;
   }
</pre>
<p>Reading the GCC docs one sees that this has desired compiler behaviour for instruction scheduling.  Namely, &#8220;these builtins are considered a full barrier. That is, no memory operand will be moved across the operation, either forward or backward&#8221;.  The compiler will not move instructions that access memory and move them into or out of the critical section bounded by the pair of atomic instructions.</p>
<p>Will the hardware retain that ordering?  NO, not necessarily.</p>
<p>These compiler builtins are partially patterned after ones provided by intel, and intel designed them to match functionality provided by their <a href="http://www.intel.com/design/itanium/manuals/245319.htm">ia64 architecture</a>, a weakly ordered instruction set.  While ia64 was effectively killed by AMD64 and Windows (now living on only in the guise of HP&#8217;s IPF systems), there are other weakly ordered instruction sets predating ia64.  Some PowerPC chips that you will find on big AIX systems, or in older macs, or in your childrens ps3 will be weakly ordered.  The <a href="http://developers.sun.com/solaris/articles/sparcv9.pdf">Sparc architecture</a> allows for weak ordering, but the chips you find in sun machines implement their TSO (total store order) model which is mostly ordered.</p>
<p>This allowance for unordered memory is why, way down at the end of the GCC page, one finds a couple special _lock methods.  The point of these is to ensure that the hardware does not reorder the relative order of memory accesses of the lock word and the lock data, even when the compiler lays down the code in &#8220;program order&#8221;.</p>
<p>We can do a mutex implementation less wrongly using a memory barrier for release as in the following fragment.</p>
<pre class="brush: cpp;">
   // the mutex data
   volatile int m = 0 ;
   #define HELD_BIT 1

   // Get the mutex.
   while ( true )
   {
      int prev = __sync_fetch_and_or( &#38;m, HELD_BIT ) ;

      if ( 0 == ( prev &#38; HELD_BIT ) )
      {
         break ;
      }
      // sleep and other stuff if you didn't get it
   }

   // Got the mutex, access the protected data.
   pShared-&#62;myLockProtectedData++ ;

   __sync_syncronize() ; 

   m = 0 ;
</pre>
<p>Observe that the use of the word barrier is overloaded on the GCC page, with some usage associated with compiler instruction scheduling, and other usage referring to memory ordering.  Their syncronize function appears to be both.  Looking at the generated assembly for a weakly ordered system would verify this interpretation, and one would likely find an lwsync or sync instruction on PowerPC for example.  This modification of the locking code ensures the compiler lays down the code with the clearing of the lock word after the protected data access, and the hardware memory barrier instruction should ensure that reads or writes to the protected data are complete before the store to the lock word is allowed to occur.  Depending on the type of instruction that is emitted for the synchronize builtin, this may not actually prevent loads and stores that occur after the lock release from being started before the lock is released.  Generally if that must be prevented a full barrier is required, and it would not surprise me if most implementations of library methods like pthread_mutex_unlock do not prevent memory accesses that occur after the lock release from being initiated before the lock release occurs.  This sort of subtlety is not likely to be found in documentation for the mutex library functions.</p>
<p>With the sample code above we are still left with the possibility that the hardware will execute the memory access instructions for the lock protected data before the lock is acquired, making the lock useless.  We can fix this with the _lock test and set and release builtins, and also remove the likely overzealous full memory barrier that synchronize provides (allowing post lock release memory access into the critical section).  Sample code for this would may look like</p>
<pre class="brush: cpp;">
   // the mutex data
   volatile int m = 0 ;
   #define HELD_VALUE 1

   // Get the mutex.
   while ( true )
   {
      int wasHeld = __sync_lock_test_and_set( &#38;m, HELD_VALUE ) ;

      if ( !wasHeld )
      {
         break ;
      }
      // sleep and other stuff if you didn't get it
   }

   // Got the mutex, access the protected data.
   pShared-&#62;myLockProtectedData++ ;

   __sync_lock_release( &#38;m ) ;
</pre>
<p>This now implements functional mutual exclusion code, even on weakly ordered systems.  The compiler has provided methods that ensure it does not move the lock protected data accesses out of the critical section, we are using atomic instructions that guarantee other threads cannot also be modifying the data (presuming they all use the same mutex), and also have instructions that ensure the hardware does not inappropriately reorder the memory accesses.  Inappropriate reordering means that accesses to the lock protected data remain after the lock acquisition and before the lock release.  It may not mean that memory accesses from before the lock acquisition are done by the time the lock is acquired nor that memory accesses that occur after the lock is released occur after the lock is released (the compiler says it does that, but the hardware may be a sneaky bastard behind your back).</p>
<p>So, that&#8217;s a mutex.  We can use something like this to avoid some of the excessive cost observed when using the pthread library mutex functions, and would expect that the uncontended cost of the back to back increment code could be lessened.</p>
<p>Presuming you do not mind the unportability of such code, and manage to get it right, and want to live with maintaining your own mutex implementation, if you do a poor job dealing with contention, you may very well get worse performance than the system provided methods in the contended codepath.</p>
<p>You really really have to want the performance to go down the path of implementing your own mutual exclusion code.  DB2 is such a product.  DB2 is also a product where first born sacrifices in the name of performance are not considered abhorrent.  In our defense our mutex implementation predates the availability of widely available library methods.  We had SMP exploitation before pthreads existed using mutiple processes and shared memory.  Our own implementation also has the advantage of providing framework to build problem determination functionality that cannot be found in generic library implementations.  Performance driven coding is not the only reason we do it ourself even in this more modern age where library methods do exist.</p>
<h1>What is an atomic, and how does an atomic method work?</h1>
<p>At the core of every atomic library method or compiler intrinsic is one or more special purpose instructions.  In many cases these are the same types of instructions that can be used to implement mutual exclusion code (when properly augmented by memory barrier instructions).</p>
<p>If you don&#8217;t have an instruction for exactly what you want, it can often be built from some other lower level operation.  Suppose for example you want to safely do the contended increment without using a lock, but only have a compare and swap.  Something like the following can be used</p>
<pre class="brush: cpp;">
int fetch_and_add( volatile int *ptr, int value )
{
   int v = *ptr ;
   while ( true )
   {
      int prev = __sync_val_compare_and_swap( ptr, v, v + value ) ;
      if ( prev == v )
      {
         break ;
      }

      v = prev ;
   }

   return prev ;
}
</pre>
<p>Generally, one can assume at least a compare and swap operation, but this may also be simulated, built up out of more direct and primitive operations.  On PowerPC for example, this is done with the very flexible load-linked/store-condition instructions (load with reservation and store conditional in PowerPC lingo).  A fetch and add can be built with a small loop</p>
<pre class="brush: cpp;">
fadd32_spin:
   lwarx     r0,0,r3       ; load word and acquire a registration
   add       r0,r0,r4      ; add in the desired amount to the possibly current value
   stwcx.    r0,0,r3       ; try to store it
   bne-      fadd32_spin   ; if loaded value old or somebody beat us repeat.
   sub       r3,r0,r4      ; fake a return using normal function return register r3
</pre>
<p>Building a portable dependence on a load-linked/store-condition primitive can be tricky, but possible.  In DB2&#8217;s reader-writer mutex implementation, a data structure employing some nifty lock free algorithms ([9] [10] [11] [12] [13] [14]) we use this load-linked/store-conditional code to implement a slick compare-against-bitmask-and-increment-or-set-bit atomic operation.  We can do the same thing with compare and swap, but the generated assembly code is particular pretty on AIX, where PowerPC gives us this more powerful primitive. </p>
<h2>A historical reflection aside.</h2>
<p>Even only a couple of years ago, it was much harder to exploit atomic instructions.  Part of the reason is that we have tossed or stopped caring about older systems.  Nobody cares anymore about older ia32 systems that did not have cmpxchg.  Pre-PowerPC AIX implementations that did not have lwarx and stwcx. instructions are now dead and buried in landfill somewhere.  PARISC systems that did not provide any sort of reasonable atomic instructions are now being replaced by HP IPF systems.  We can now generally assume that some basic atomic building blocks are available and no longer have to tiptoe around avoiding breaking the old guys.</p>
<p>Another reason that makes atomic usage easier these days is the wide availability of compiler builtins and library infrastructure, rendering this previous area of horrendous unportability accessible.  Use of the GCC builtins was illustrated above, showing some incorrect and correct lock implementations.  On the same page can be found instructions for compare and swap, atomic read and increment, and others.  Similar builtins are available from many other compilers.  The boost library implementation of atomic types are a good example.  The next revision of the C++ standard will likely include boost-like methods.  It appears that C# implements Interchange methods very much like the msdev intrinsics, so this is even becoming a cross language portability area in a limited fashion.</p>
<p>It is fun to illustrate just how murky the portability waters were before the prevalance of compiler intrinsics.  Atomic exploitation generally required writing assembly code, and that is never particularly fun.  Here is a sample code showing how one would code an &#8220;inline assembly&#8221; language implementation of fetch and add using the IBM compiler targeting PowerPC</p>
<pre class="brush: cpp;">
int PowerPcFetchAndAdd32Internal ( volatile int * pAtomic, int value ) ;

#pragma mc_func PowerPcFetchAndAdd32Internal { &#34;7c001828&#34; &#34;7c002214&#34; \
   &#34;7c00192d&#34; &#34;40a2fff4&#34; &#34;7c640050&#34; }

#pragma reg_killed_by PowerPcFetchAndAdd32Internal cr0, gr0, gr3
</pre>
<p>What we have here is literally a mechanism to instruct the compiler to shove the raw instructions, specified by hexadecimal opcodes, directly into the instruction stream, with the writer of the inline assembly routine providing information about all the registers clobbered and side effects of the code.  This inline assembly code is actually generated from the listing in PowerPC assembly fragment above, which was carefully constructed to have the same structure as a real function call on AIX (input params in r3,r4, and output in r3)</p>
<p>Before the GCC intrinsics were available, one could use the powerful GCC inline assembly syntax to instruct the compiler what to shove into the code.  Here&#8217;s an example for fetch and add</p>
<pre class="brush: cpp;">
#define IA32_XADD32(pAtomic, addValue, oldValue)                    \
__asm__ __volatile__( &#34;lock; xaddl
 /* outputs */     : &#34;=r&#34;(oldValue), /*
                     &#34;+m&#34; (*pAtomic) /*
 /* inputs */      : &#34;0&#34; (addValue)  /*
 /* clobbers */    : &#34;cc&#34;            /* condition registers (ZF) */ \
                       )
</pre>
<p>Looks kind of like ASCII barf, but does the job nicely enough if you can get it right.  </p>
<p>On platforms that provided no good inline assembly methods was not available (which includes those platforms and compilers for which inline assembly makes the code worse than using out of line assembly) one had the option of coding standalone assembler files to create the function call methods desired.  That can be just as difficult since one is then forced to learn the assembler syntax in more detail and understand the idiosyncrasies of all the platform calling conventions.</p>
<h1>Types of barriers.</h1>
<p>There are in general a few different types of barriers (or fences).  Not all architectures that allow for unordered instructions necessarily implement all of these.  Some of these may also only be available as variations of specific instructions, so when standalone use is required a more expensive instruction may be required.</p>
<p>To describe the barrier types I will borrow ia64 nomenclature and assembly fragments, which is nicely descriptive despite death of this platform.  The ia64 architecture provides acquire, release and full barriers.  Some of these are built into the load and store operations on these platforms.</p>
<h2>Acquire barrier</h2>
<p>If one sees assembly code containing</p>
<pre class="brush: cpp;">
ld4      r3   = [r30]   ; &#34;BEFORE&#34;
st1      [r4] = r40     ; &#34;BEFORE&#34;

ld4.acq  r5   = [r42]   ; LOAD with ACQUIRE barrier

ld4      r6   = [r43]   ; AFTER
st1      [r7] = r44     ; AFTER
</pre>
<p>One has no guarantee about the relative ordering of the BEFORE-labeled memory accesses.  Similarly one has no guarantee about the ordering of the pair of AFTER-labeled memory accesses, but because of the acquire barrier the load and acquire barrier must be complete before the AFTER accesses.</p>
<p>Note that the acquire barrier is one directional.  It does not prevent the pair of BEFORE memory accesses from occurring after the barrier.  Illustrating by example, the instructions could be executed as if the compiler had generated the following sequence of instructions</p>
<pre class="brush: cpp;">
ld4.acq  r5   = [r42]

st1      [r7] = r44
ld4      r3   = [r30]
ld4      r6   = [r43]
st1      [r4] = r40
</pre>
<p>There may be additional architecture rules about dependent loads and stores effecting the types of code motion that is allowed by the compiler, or the way that the CPU implements the architecture, but I will ignore those here.</p>
<p>This sort of effective instruction reordering is often described saying that one instruction has passed another.</p>
<p>This acquire barrier, perhaps unsurprising, is exactly the type of barrier required when implementing code for mutex acquisition.  In addition to ia64, the PowerPC platform requires such barriers, and provides the isync instruction to the programmer.  An example in a higher level language that may not have the desired effect without an such an acquire barrier is</p>
<pre class="brush: cpp;">
if ( pSharedMem-&#62;atomic.bitTest() ) // use a bit to flag that somethingElse is &#34;ready&#34;
{
   foo( pSharedMem-&#62;somethingElse ) ;
}
</pre>
<p>If correct function of your program depends on somethingElse only being read after the atomic update flagging a condition is complete, then this code is not correct without an isync or other similar platform specific acquire barrier.</p>
<p>Your code should really look something like this</p>
<pre class="brush: cpp;">
if ( pSharedMem-&#62;atomic.bitTestAcquire() )
{
   foo( pSharedMem-&#62;somethingElse ) ;
}
</pre>
<p>Here it is assumed the atomic library provides acquire and release variations of all atomic operations.  For PowerPC this could be as simple as adding an isync to the unordered atomic operation, but on a platform like ia64 one would probably use a different instruction completely (ie. cmpxchg.acq for example).  With such an acquire barrier the somethingElse access or another other memory operation that follows the load associated with the bitTest necessarily follows the load itself.  For example, this can mean that a non-dependent load that has been executed ahead of the acquire barrier will have to be thrown out and reexecuted.</p>
<h2>Release barrier</h2>
<p>A release barrier is perhaps a bit easier to understand.  Illustrating again by example, suppose that we have the following set of load and store instructions, separated by a release barrier.</p>
<pre class="brush: cpp;">
ld4      r3    = [r30]   ; BEFORE
st1      [r4]  = r40     ; BEFORE

st4.rel  [r42] = r5      ; STORE with release barrier.

ld4      r6    = [r43]   ; &#34;AFTER&#34;
st1      [r7]  = r44     ; &#34;AFTER&#34;
</pre>
<p>The release barrier ensures that the pair of BEFORE-labeled reads and write memory accesses are complete before the barrier instruction.  There is nothing that prevents the BEFORE instructions from executing in an alternate order, but all of the effects of these instructions visible by any other CPU must be complete before the effects of the store barrier instruction is observable by that other CPU.  Like the acquire barrier, the release barrier is a one way fence.  Neither of the instructions labeled &#8220;AFTER&#8221; necessarily have to be executed after the release barrier.</p>
<p>On PowerPC this release barrier is implemented with a light weight sync (different than the isync that implements the acquire barrier).  As a general rule of thumb, code that requires a release barrier will require an acquire barrier and vice-versa.  These instructions are almost always required to be used in complementary pairs.</p>
<p>Similar but opposite to the acquire barrier, a release barrier has the semantics required for implementing the release portion of a mutex.</p>
<h2>Full barrier.</h2>
<p>We have seen how use of release and acquire barriers can ensure that that memory access before or after the barrier can be forced to maintain that order relative to the barrier.  There are however, some types of memory ordering that neither of these can ensure.</p>
<p>Using ia64 code to illustrate, suppose one has memory accesses before a release barrier, and memory accesses after an acquire barrier.</p>
<pre class="brush: cpp;">
ld4      r3    = [r30]   ; BEFORE
st1      [r4]  = r40     ; BEFORE

st4.rel  [r42] = r5      ; STORE with release barrier.
ld4.acq  r8    = [r45]   ; LOAD with acquire barrier.

ld4      r6    = [r43]   ; AFTER
st1      [r7]  = r44     ; AFTER
</pre>
<p>The barriers here ensure that the BEFORE-labeled instructions all complete before the release barrier effects become visible to other cpus, and also ensure that none of the AFTER-labeled instructions may start before the acquire barrier instruction is complete.  This barrier cannot prevent the acquire barrier LOAD instruction from completing before the STORE barrier.  Illustrating by example, the CPU could execute things like so</p>
<pre class="brush: cpp;">
ld4.acq  r8    = [r45]   ; LOAD with acquire barrier.

ld4      r3    = [r30]   ; BEFORE
st1      [r4]  = r40     ; BEFORE

ld4      r6    = [r43]   ; AFTER
st1      [r7]  = r44     ; AFTER

st4.rel  [r42] = r5      ; STORE with release barrier.
</pre>
<p>This alternate ordering also meets the requirements of the original code.  All of the BEFORE-labeled instructions are still before the release barrier and all the AFTER-labeled instructions occur after the acquire barrier.  In order to enforce this type of ordering we require a special standalone ordering construct.  On ia64 this is the mf (memory fence) instruction, and on that platform it acts as both a standalone acquire and release barrier, but also prevents store load reordering.  Of the weakly ordered platforms that are widely available (PowerPC, Sparc, ia64), all of these architectures require a special standalone instruction to enforce this special ordering.</p>
<p>The Sparc architecture has a number of possible ordering models, but the one implemented in all the CPUs that you would find in a Solaris Sparc based system is designated total store order (TSO).  For all practical purposes this means that all instruction pairs are ordered except a store load sequence, and in order to enforce such an ordering one must interject a &#8216;membar #storeload&#8217; instruction.</p>
<p>On PowerPC, in order to guarantee store load instructions maintain their order, one must interject a heavy-weight sync instruction between the two (sync).</p>
<p>One gets the impression that whatever hardware black magic is required to implement store load ordering, it has the appearance of being harder to enforce this particular type of instruction ordering than any other.</p>
<p>One of the interesting things about these store load barrier instructions, is not that they do enforce this ordering, but that the release and acquire barrier instructions do not.  In particular a mutex implemented with acquire and release barriers and not with a full barrier instruction can let exterior memory accesses into the critical section.</p>
<p>Suppose for example we have the following code</p>
<pre class="brush: cpp;">
pSharedMem-&#62;x = 3 ;  // BEFORE
v = pSharedMem-&#62;y ;  // BEFORE

mutex.acquire() ; // assumed to contain an acquire barrier.

pSharedMem-&#62;z = 4 ;
foo( pSharedMem-&#62;w ) ;

mutex.release() ; // assumed to contain a release barrier.

pSharedMem-&#62;a = 2 ;  // AFTER
q = pSharedMem-&#62;b ;  // AFTER
</pre>
<p>Unless the mutex uses very heavy handed memory barrier instructions one has the possibility that either the AFTER or BEFORE labeled memory accesses may sneak into the critical section that the mutex provides.</p>
<p>This subtlety may not be documented by the mutex implementation, but one should not assume that a mutex does any more than keep stuff within the critical section from getting out.</p>
<h1>The flag pattern illustrated.</h1>
<p>The mutex is by far the most important example of barrier use that is required for correctness in a weakly ordered system.  It is however, perhaps not the easiest to understand.  A slightly simpler pattern is the use of an atomic to flag that another memory update is complete.</p>
<p>As mentioned it is common for correct memory barrier usage to require paired conugate acquire and release barriers.  This is very much like what is in the implementation of the critical section that you are probably trying to avoid by using an atomic and the associated barriers.  It will however, be split, with the release and acquire barrier usage divided into portions for the producer and consumer.  As an example your critical section implementation would typically be of the form:</p>
<pre class="brush: cpp;">
while (!pShared-&#62;lock.testAndSet_Acquire()) ;
// (this loop should include all the normal critical section stuff like
// spin, waste,
// pause() instructions, and last-resort-give-up-and-blocking on a resource
// until the lock is made available.)

// Access to shared memory.

pShared-&#62;foo = 1
v = pShared-&#62; goo

pShared-&#62;lock.clear_Release()
</pre>
<p>Acquire memory barrier above makes sure that any loads (pShared-&#62;goo) that may have been started before the successful lock modification are tossed, to be restarted if necessary.</p>
<p>The release memory barrier ensures that the load from goo into the (local say) variable v is complete before the lock word protecting the shared memory is cleared.</p>
<p>You have a similar pattern in the typical producer and consumer atomic flag scenario (it is difficult to tell by your sample if that is what you are doing but should illustrate the idea).</p>
<p>Suppose your producer used an atomic variable to indicate that some other state is ready to use. You&#8217;ll want something like this:</p>
<pre class="brush: cpp;">
pShared-&#62;goo = 14

pShared-&#62;atomic.setBit_Release()
</pre>
<p>Without a release (&#8220;write&#8221;) barrier here in the producer you have no guarantee that the hardware isn&#8217;t going to get to the atomic store before the goo store has made it through the CPU store queues, and up through the memory hierarchy where it is visible (even if you have a mechanism that ensures the compiler orders things the way you want).</p>
<p>In the consumer</p>
<pre class="brush: cpp;">
if ( pShared-&#62;atomic.compareAndSwap_Acquire(1,1) )
{
   v = pShared-&#62;goo
}
</pre>
<p>Without a &#8220;read&#8221; barrier here you won&#8217;t know that the hardware hasn&#8217;t gone and fetched goo for you before the atomic access is complete. The atomic (ie: memory manipulated with the Interlocked functions doing stuff like lock cmpxchg), is only &#8220;atomic&#8221; with respect to itself, not other memory.</p>
<p>That&#8217;s really all there is to this example, so it is perhaps anticlimatic, as well as including a redundant discussion of a mutex implementation.  However, it is a good first step and these notes are already too long.  A more and better example will perhaps follow on a different day.</p>
<h1>Conclusion.</h1>
<p>Some of the issues associated with using atomics and barriers to replace the simpler lock mechanism have been discussed.  It is unfortunate to have to provide examples that utilize assembly code to illustrate these ideas, but it is harder to discuss these topics without doing so.  Hopefully the inclusion of some of the low level details and assembly listings has not rendered this on paper discussion too incomprehensible.  </p>
<p>The fact that some low level systems details must be understood for correct use of atomics to avoid locking should perhaps be a warning to the programmer.  It can be very hard to implement lock avoidance algorithms correctly, harder still to test them.  There is also a significant maintenance impact to going down this road, so be good and sure that you really need the performance provided before taking this path.  Premature optimization in this area can be particularly dangerous and costly.</p>
<p>Do you really have a good reason for going down the atomic path instead of just using a lock?  Doing all this correctly can be very difficult. Be prepared for a lot of self doubt and insecurity in code reviews and make sure you have a lot of high concurrency testing with all sorts of random timing scenarios.  Use a critical section unless you have a very very good reason to avoid it, and don&#8217;t write that critical section yourself.</p>
<p>The correctness aspects of memory barrier utilization makes your code intrinsically unportable.  Because many correct uses of atomics also require barrier instructions, this can in turn imply that the &#8220;innocent and simple&#8221; atomic class library implementation you have access to can be correspondingly unportable.  This may be surprising to the developer, but then again, many aspects of weakly ordered system behavior can be surprising.</p>
<p>On a weakly ordered system, your compiler probably provides _acquire and _release variations for most of the atomic manipulation methods.  On a strongly ordered platform like ia32 or amd64, _acquire() or _release() suffixed atomic intrinsics will not be any different than the vanilla operations.  With ia64 effectively dead (except on HP where its still twitching slightly) PowerPC is probably the most pervasive surviving weakly ordered architecture around, and even it has toyed with non-weak ordering (power5 was weakly ordered, power6 was not, and power7 is again weakly ordered).  It will be interesting to what path the hardware folks take, and whether these details are of any importance in 10 or 15 years.</p>
<h1>References</h1>
<p>[1] M. Lyons, E. Silha, and B. Hay. PowerPC storage model and AIX programming, 2002.  <a href="http://www.ibm.com/developerworks/eserver/articles/powerpc.html">http://www.ibm.com/developerworks/eserver/articles/powerpc.html</a>.</p>
<p>[2] D. Lea. The JSR-133 cookbook for compiler writers, 2005.</p>
<p>[3] L.C.H.J. Boehm. Threads and memory model for C++ [online].  <a href="http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/">http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/</a>.</p>
<p>[4] J. Manson and B. Goetz. Jsr 133 (java memory model) faq [online]. 2004.  <a href="http://www.cs.umd.edu/ pugh/java/memoryModel/jsr-133-faq.html">http://www.cs.umd.edu/ pugh/java/memoryModel/jsr-133-faq.html</a>.</p>
<p>[5] L.C.H.J. Boehm. C++ atomic types and operations. mailings N2427. <em>C++ standards committee</em>, 2007.  <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html</a>.</p>
<p>[6] H. Sutter. {Prism-A Principle-Based Sequential Memory Model for Microsoft Native  Code Platforms Draft Version 0.9. 1}, 2006.  <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2197.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2197.pdf</a>.</p>
<p>[7] R. Silvera, M. Wong, P. McKenney, and B. Blainey. {A simple and efficient memory model for weakly-ordered  architectures}, 2007.  <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2153.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2153.pdf</a>.</p>
<p>[8] S.V. Adve and K. Gharachorloo. Shared memory consistency models: A tutorial. <em>Computer</em>, 29(12):66&#8211;76, 1996.  <a href="http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-95-7.pdf">http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-95-7.pdf</a>.</p>
<p>[9] J.D. Valois. Lock-free linked lists using compare-and-swap.</p>
<p>[10] T.L. Harris, K. Fraser, and I.A. Pratt. A practical multi-word compare-and-swap operation. <em>Lecture Notes in Computer Science</em>, 2508:265&#8211;279, 2002.  <a href="http://www.cl.cam.ac.uk/research/srg/netos/papers/2002-casn.pdf">http://www.cl.cam.ac.uk/research/srg/netos/papers/2002-casn.pdf</a>.</p>
<p>[11] T.L. Harris. A pragmatic implementation of non-blocking linked-lists. <em>Lecture Notes in Computer Science</em>, 2180:300&#8211;314, 2001.  <a href="http://www.cl.cam.ac.uk/research/srg/netos/papers/2001-caslists.pdf">http://www.cl.cam.ac.uk/research/srg/netos/papers/2001-caslists.pdf</a>.</p>
<p>[12] T.L. Harris. Harris Papers [online].  <a href="http://www.cl.cam.ac.uk/research/srg/netos/lock-free/">http://www.cl.cam.ac.uk/research/srg/netos/lock-free/</a>.</p>
<p>[13] R. Bencina. lock free papers and notes. [online]. 2006.  <a href="http://www.audiomulch.com/ rossb/code/lockfree/">http://www.audiomulch.com/ rossb/code/lockfree/</a>.</p>
<p>[14] M.M. Michael. {Safe memory reclamation for dynamic lock-free objects using atomic  reads and writes}. In {<em>Proceedings of the twenty-first annual symposium on  Principles of distributed computing}, pages 21&#8211;30. ACM New York, NY, USA,  2002.</em>  <a href="http://www.research.ibm.com/people/m/michael/podc-2002.pdf">http://www.research.ibm.com/people/m/michael/podc-2002.pdf</a>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[A pthread wrapper class - part 2]]></title>
<link>http://slworkthings.wordpress.com/2009/11/11/a-pthread-wrapper-class-part-2/</link>
<pubDate>Wed, 11 Nov 2009 00:12:59 +0000</pubDate>
<dc:creator>slworkthings</dc:creator>
<guid>http://slworkthings.wordpress.com/2009/11/11/a-pthread-wrapper-class-part-2/</guid>
<description><![CDATA[Continue from part 1. This is a wrapper class for Mutex. Header File: #ifndef _MUTEX_H_ #define _MUT]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Continue from part 1. This is a wrapper class for Mutex.</p>
<p>Header File:<br />
#ifndef _MUTEX_H_<br />
#define _MUTEX_H_<br />
#include &#60;pthread.h&#62;<br />
class Mutex<br />
{<br />
public:<br />
    friend class Condition;</p>
<p>    Mutex();<br />
    virtual ~Mutex();<br />
    Mutex(const Mutex&#38; copy);</p>
<p>    bool lock();<br />
    bool unlock();<br />
    bool tryLock();<br />
private:<br />
    pthread_mutex_t      *m_pmutex;<br />
    bool                  m_isOwner;</p>
<p>};</p>
<p>#endif // _MUTEX_H_<br />
 Source File:</p>
<p>#include &#60;cerrno&#62;<br />
#include &#8220;mutex.h&#8221;</p>
<p>Mutex::Mutex()<br />
      :m_pmutex(0),<br />
       m_isOwner(true)<br />
{<br />
    m_pmutex = new pthread_mutex_t;<br />
    if (pthread_mutex_init(m_pmutex, 0) == 0)<br />
    {<br />
        //ok<br />
    }<br />
}</p>
<p>Mutex::~Mutex()<br />
{<br />
    if (m_isOwner)<br />
    {<br />
        if (pthread_mutex_destroy(m_pmutex) == EBUSY)<br />
        {<br />
            unlock();<br />
            pthread_mutex_destroy(m_pmutex);<br />
        }<br />
        delete m_pmutex;<br />
    }<br />
}</p>
<p>Mutex::Mutex(const Mutex&#38; copy)<br />
{<br />
    m_pmutex = copy.m_pmutex;<br />
    m_isOwner = false;<br />
}</p>
<p>bool Mutex::lock()<br />
{<br />
    if (pthread_mutex_lock(m_pmutex) == 0)<br />
        return true;<br />
    return false;<br />
}</p>
<p>bool Mutex::unlock()<br />
{<br />
    if (pthread_mutex_unlock(m_pmutex) == 0)<br />
        return true;<br />
    return false;<br />
}</p>
<p>bool Mutex::tryLock()<br />
{<br />
    if (pthread_mutex_trylock(m_pmutex) == 0)<br />
        return true;<br />
    return false;<br />
}</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Exercise 10: Concurrency and Threading Demonstration in Python]]></title>
<link>http://jrankmore.wordpress.com/2009/10/28/exercise-10-concurrency-and-threading-demonstration-in-python/</link>
<pubDate>Wed, 28 Oct 2009 04:55:14 +0000</pubDate>
<dc:creator>Jason Rankmore</dc:creator>
<guid>http://jrankmore.wordpress.com/2009/10/28/exercise-10-concurrency-and-threading-demonstration-in-python/</guid>
<description><![CDATA[This exercise poses the questions in a relatively simple manner so i will answer in the same way. Fi]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This exercise poses the questions in a relatively simple manner so i will answer in the same way.</p>
<p><strong>Finding Definitions for Eight Terms and Concepts</strong> <strong>used in Threaded Programming</strong>.</p>
<p>The term or concept will be followed by the definition.</p>
<p><span style="text-decoration:underline;">Thread Synchronization<br />
</span></p>
<p>Thread synchronization refers to the simultaneous execution of multiple threads (defined further down) and/or processes, this allows a program to provide a wider range of services than using singular processes.</p>
<p><span style="text-decoration:underline;">Locks</span></p>
<p>Basically a lock is a synchronization mechanism put in place to control access to resources between threads and processes. These locks are put in place to ensure the integrity of the data resources and that no 2 threads can access one data source at the same time.</p>
<p><span style="text-decoration:underline;">Dead Locks</span></p>
<p>Wikipedia defines a deadlock as:</p>
<blockquote><p><em>In computer science, <strong>deadlock</strong> refers to a specific condition when two or more processes are each waiting for each other to release a resource, or more than two processes are waiting for resources in a circular chain. Deadlock is a common problem in multiprocessing where many processes share a specific type of mutually exclusive resource known as a <em>software lock</em> or <em>soft lock</em>. Computers intended for the <em>time-sharing</em> and/or <em>real-time</em> markets are often equipped with a <em>hardware lock</em> (or <em>hard lock</em>) which guarantees <em>exclusive access</em> to processes, forcing serialized access. Deadlocks are particularly troubling because there is no <em>general</em> solution to avoid (soft) deadlocks.</em></p></blockquote>
<p><span style="text-decoration:underline;">Semaphores</span></p>
<p>Wikipedia defines a semaphore as:</p>
<blockquote><p><em>In computer science, a <strong>semaphore</strong> is a protected variable or abstract data type which constitutes the classic method for restricting access to shared resources such as shared memory in a parallel programming environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by Edsger Dijkstra<sup>[1]</sup>. Semaphores are the classic solution to preventing race conditions in the dining philosophers problem, although they do not prevent resource deadlocks.</em></p></blockquote>
<p><span style="text-decoration:underline;">Mutual Exclusion (Mutex)</span></p>
<p>Mutual Exclusion is the concept of locking resources and restricting access.</p>
<p>Wikipedia defines the concept of mutual exclusion as:</p>
<blockquote><p><em><strong>Mutual exclusion</strong> (often abbreviated to <strong>mutex</strong>) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections. A critical section is a piece of code where a process or thread accesses a common resource. The critical section by itself is not a mechanism or algorithm for mutual exclusion. A program, process, or thread can have critical section in it without any mechanism or algorithm, which implements mutual exclusion. Examples of such resources are fine-grained flags, counters or queues, used to communicate between code that runs concurrently, such as an application and its interrupt handlers. The synchronization of access to those resources is an acute problem because a thread can be stopped or started at any time. To illustrate: suppose a section of code is altering a piece of data over several program steps, when another thread, perhaps triggered by some unpredictable event, starts executing. If this second thread reads from the same piece of data, the data, which is in the process of being overwritten, is in an inconsistent and unpredictable state. If the second thread tries overwriting that data, the ensuing state will probably be unrecoverable. These shared data being accessed by critical sections of code, must therefore be protected, so that other processes which read from or write to the chunk of data are excluded from running.</em></p></blockquote>
<p><span style="text-decoration:underline;">Thread</span></p>
<p>Basically threads are a lightweight processes. Darrel Ince defines a thread as <em>The execution of a chunk of code which can be carried out in parallel with the execution of other chunks of code</em>.</p>
<p><span style="text-decoration:underline;">Event</span></p>
<p>Wikipedia defines an event as:</p>
<blockquote><p><em>In computing an <strong>event</strong> is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program. Typically events are handled synchronous with the program flow, that is, the program has one or more dedicated places where events are handled. Typical sources of events include the user (who presses a key on the keyboard, in other words, through a keystroke). Another source is a hardware devices such as a timer. A computer program that changes its behavior in response to events is said to be event-driven, often with the goal of being interactive.</em></p></blockquote>
<p><span style="text-decoration:underline;">Waitable Timer</span></p>
<p>The Microsoft Developer Network defines a waitable timer object as:<strong><br />
</strong></p>
<blockquote><p><em>A <em>waitable timer object</em> is a synchronization object whose state is set to signaled when the specified due time arrives. There are two types of waitable timers that can be created: manual-reset and synchronization. A timer of either type can also be a periodic timer.</em></p></blockquote>
<p style="text-align:center;"><em><img class="aligncenter" title="Thread" src="http://farm1.static.flickr.com/122/296633705_7b1b9aec4d.jpg" alt="Thread" width="210" height="210" /><br />
</em></p>
<p><strong>Demonstration of Threading in Python</strong></p>
<p>This program creates 10 tasks and sets a bounded semaphore so no more than 3 of the 10 processes can run at the same time. A mutex lock is also put in place. Each thread acquires the semaphore resource and lock and then releases it and runs until all taks have been executed successfully.</p>
<p><em><br />
</em>References</p>
<p>http://en.wikipedia.org/wiki/Deadlock</p>
<p>http://en.wikipedia.org/wiki/Semaphore_%28programming%29</p>
<p>http://en.wikipedia.org/wiki/Mutual_exclusion</p>
<p>http://en.wikipedia.org/wiki/Thread_%28computer_science%29</p>
<p>http://en.wikipedia.org/wiki/Event_%28computing%29</p>
<p>http://msdn.microsoft.com/en-us/library/ms687012%28VS.85%29.aspx</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Prefer Simple Mutex Over Recursive Mutex]]></title>
<link>http://askldjd.wordpress.com/2009/10/26/prefer-simple-mutex-over-recursive-mutex/</link>
<pubDate>Mon, 26 Oct 2009 01:41:02 +0000</pubDate>
<dc:creator>Alan Ning</dc:creator>
<guid>http://askldjd.wordpress.com/2009/10/26/prefer-simple-mutex-over-recursive-mutex/</guid>
<description><![CDATA[For those of us who work in the multi-threaded environment, the concept of mutex (&#8220;mutual excl]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>For those of us who work in the multi-threaded environment, the concept of mutex (&#8220;mutual exclusion&#8221;) should be no stranger. There are two common categories of mutex &#8211; simple and recursive.</p>
<p>Simple mutex can only be locked once, and if the same thread tries to lock the same mutex again, it will deadlock.</p>
<p>Recursive mutex can be locked by the same thread numerous times, and the same thread must unlock the same number of times before the critical region can be accessed by another thread.</p>
<p>Recursive mutex is common. In fact, mutexes are <a href="http://www.ibm.com/developerworks/linux/library/l-ipc2lin3.html">recursive by default</a> in Windows.</p>
<p>With all that said, I believe recursive mutex should be avoid as much as possible.</p>
<h3>Obvious Overhead</h3>
<p>Since recursive mutex tracks more information than a simple mutex, it should be no surprise that it is more expensive.</p>
<p>Here is a sample test case with multiple threads accessing a critical region. The mutex implementation is from Boost.Thread library.</p>
<div id="attachment_173" class="wp-caption aligncenter" style="width: 410px"><a href="http://askldjd.wordpress.com/files/2009/10/recursive_mutex_benchmark_big.png"><img class="size-full wp-image-173 " style="border:1px solid black;" title="recursive_mutex_benchmark_small" src="http://askldjd.wordpress.com/files/2009/10/recursive_mutex_benchmark_small.png" alt="recursive_mutex_benchmark_small" width="400" height="225" /></a><p class="wp-caption-text">Red dotted line indicates the cost of a single simple mutex. Blue line indicates the cost of multiple recursive mutexes.</p></div>
<p style="text-align:center;">
<p>A single recursive mutex is quite a bit more expensive than a simple mutex. If recursive mutex is used, it is probably going to be locked more than once by the same thread (otherwise, why would you use it?). The graph also demonstrates the cost of recursively locking the mutex again, up to five times. As you can see, it is linear, and it is definitely not free.</p>
<p>The term &#8220;recursive mutex&#8221; might sound fancy, but they are nothing but <em>redundant</em> locks. After the critical region is locked, each additional recursive mutex adds nothing but overhead.</p>
<p>Some might argue that this cost is negligible. This might be true depending on the application, but this is just the surface of the problem.</p>
<h3>The Real Problem</h3>
<p>The truth is that locks and mutexes are <a href="http://www.ddj.com/go-parallel/article/showArticle.jhtml?articleID=212501082">difficult to use correctly</a>. Until we have <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">STM</a>, there is no silver bullet.</p>
<p>Recursive mutex makes locking easy by taking away the programmer&#8217;s responsibility of tracking mutexes. Programmers can add recursive mutex to existing functions and member functions easily without refactoring code. This would not have been possible with simple mutex. However, this advantage is merely an illusion.</p>
<p>Don&#8217;t believe it? Here&#8217;s the <a href="http://groups.google.com/group/comp.programming.threads/msg/d835f2f6ef8aed99?hl=en">thought of David Butenhof</a>, the person who introduced recursive mutex in POSIX thread.</p>
<p>Here&#8217;s some of his powerful arguments:</p>
<p>1. Recursive mutex allows you to lose track of your locking scheme and scope. As your call stack becomes deeper, you will have less sense on how long you&#8217;ve been locking the critical region.</p>
<p>2. Recursive mutex are reference counted, and releasing it does not imply that the critical region is freed. So how would you know how to unlock?</p>
<p>I&#8217;ve learned a lot from his post. His argument addresses the fundamental design issue in software that use recursive mutex.</p>
<p>To avoid using recursive mutex, we have to consider refactoring.</p>
<h3>Refactoring</h3>
<p>In functional programming, a typical refactoring technique to break up recursive locking is by <a href="http://www.fieryrobot.com/blog/2008/10/14/recursive-locks-will-kill-you/">splitting a function into two components</a>- outer and inner. The outer function holds the lock, and invoke the inner functions to perform its task. The inner functions is an internal function that is not exposed as an API.</p>
<p>In Object Oriented programming, this technique can also be applied in form of <a href="http://c2.com/cgi/wiki?PimplIdiom">Pimpl idiom</a> or private functions.</p>
<p>Consider this example:</p>
<p>Class A has two public functions &#8211; One and Two. One calls Two. Both methods are<a href="http://en.wikipedia.org/wiki/Thread_safety"> thread-safe</a>, they both lock the same recursive mutex, which is a private member of Class A. Below demonstrates that the functional programming refactoring technique can be applied in OO programming.</p>
<div id="attachment_194" class="wp-caption aligncenter" style="width: 419px"><a href="http://askldjd.wordpress.com/files/2009/10/recursive_class_bad3.png"><img class="size-full wp-image-194" title="recursive_class_bad" src="http://askldjd.wordpress.com/files/2009/10/recursive_class_bad3.png" alt="recursive_class_bad" width="409" height="289" /></a><p class="wp-caption-text">A::One calls A::Two, they both lock on the same mutex member variable.</p></div>
<p style="text-align:center;">
<div id="attachment_186" class="wp-caption aligncenter" style="width: 419px"><a href="http://askldjd.wordpress.com/files/2009/10/recursive_class_good.png"><img class="size-full wp-image-186" title="recursive_class_good" src="http://askldjd.wordpress.com/files/2009/10/recursive_class_good.png" alt="recursive_class_good" width="409" height="331" /></a><p class="wp-caption-text">Refactor the implemention of A::Two into A::Two_Impl. A::One and A::Two nows calls A::Two_Impl. Recursive mutex becomes unnecessary, and can be refactored into a simple mutex.</p></div>
<h3>Refactoring &#8211; A More Complicated Scenario</h3>
<p>When more than one classes are involved, things gets more complicated. The technique above will not hold.</p>
<p>Here&#8217;s a more complicated (yet realistic) example:</p>
<p>Class A has two public functions &#8211; One and Three. Both functions are thread-safe, and lock the same recursive mutex, which is a private member variable of A. Class B has one public function &#8211; Two.</p>
<p>A::One calls B::Two, and B::Two calls A::Three.</p>
<div id="attachment_209" class="wp-caption aligncenter" style="width: 419px"><a href="http://askldjd.wordpress.com/files/2009/10/recursive_class_complicated.png"><img class="size-full wp-image-209" title="recursive_class_complicated" src="http://askldjd.wordpress.com/files/2009/10/recursive_class_complicated.png" alt="recursive_class_complicated" width="409" height="423" /></a><p class="wp-caption-text">A more complicated scenario that involves more than one class. </p></div>
<p style="text-align:center;">
<p>Now we have a mess. The possible call paths grows exponentially as the call stack gets deeper.</p>
<p>In the example, there are three possible paths.</p>
<p>1. A::One, B::Two, A::Three</p>
<p>2. B::Two, A::Three</p>
<p>3. A::Three</p>
<p>If we apply the previous technique, we would refactor A::Three into A::Three and A::Three_Impl. That won&#8217;t work here because class B expects A::Three_Impl to be a public interface.</p>
<p>There is no fix-all solution here, but here&#8217;s some suggestion.</p>
<p>1. Refactor A and B such that they do not circular reference each other. In other word, A can reference B, or B can reference A, but not at the same time.</p>
<p>2. Merge A and B in one class, or move B::Two into class A.</p>
<h3>Use Recursive Mutex If It Is Absolutely Positively Necessary</h3>
<p>Unfortunately, in the real world, things get even more complicated. The complicated example above only involves two classes. But what if more classes are involved, the number of call paths grows exponentially, and becomes unrealistic to refactor?</p>
<p>As a discipline of computer science, I would argue that for every solution with recursive mutex, there must be a solution for simple mutex.</p>
<p>As a software engineer who is under the gun with a deadline&#8230; I say just hack it and use recursive mutex. We are shipping tomorrow! <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>But as David Butenhof said, recursive mutex is a hack. Instead of relying on recursive mutex up front, we should avoid it until it is absolutely necessary.</p>
<h3>Conclusion</h3>
<p>Recursive mutex is dangerous because you lose sense of locking scope. It costs more than a simple mutex.</p>
<p>Consider to refactor the code to avoid the use of recursive mutex.</p>
<p>Use recursive mutex if it can&#8217;t be refactored economically.</p>
<h3>Source</h3>
<p>The source and the spreadsheet can be downloaded <a href="http://cid-4f23a64a0ca2474e.skydrive.live.com/self.aspx/shared/recursive^_mutex^_benchmark.zip">here</a>.</p>
<p>Tools: Visual Studio 2008, Boost 1.39, Window XP SP3 32bit</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[How to limit single instance of an application]]></title>
<link>http://vcpptips.wordpress.com/2009/10/13/how-to-limit-single-instance-of-an-application/</link>
<pubDate>Tue, 13 Oct 2009 03:26:30 +0000</pubDate>
<dc:creator>Sanoop S P</dc:creator>
<guid>http://vcpptips.wordpress.com/2009/10/13/how-to-limit-single-instance-of-an-application/</guid>
<description><![CDATA[    Some applications are built to allow users to run single instance of an application at a time. T]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><img class="alignleft size-full wp-image-727" title="Description" src="http://vcpptips.wordpress.com/files/2009/09/description.jpg" alt="Description" width="150" height="40" /></p>
<p> </p>
<p> </p>
<p>Some applications are built to allow users to <span style="color:#0000ff;">run single instance of an application at a time</span>. That means we need to get an information from first instance of an application to the second instance and thus we need to <span style="color:#0000ff;">prevent the second instance from running</span>.</p>
<p><img class="alignleft size-full wp-image-706" title="HowCanIDoIt1" src="http://vcpptips.wordpress.com/files/2009/07/howcanidoit13.jpg" alt="HowCanIDoIt1" width="200" height="40" /></p>
<p> </p>
<p> </p>
<p>We can use one of the <span style="color:#0000ff;">synchronization </span>objects <span style="color:#0000ff;">mutex </span>to ensure two or more threads do not attempt to access a shared memory simultaneously. We need to check it within the <span style="color:#0000ff;">startup(WinMain) of the application</span>.</p>
<p><img class="alignleft size-full wp-image-689" title="MyCodeSnippet" src="http://vcpptips.wordpress.com/files/2009/07/mycodesnippet1.jpg" alt="MyCodeSnippet" width="150" height="40" /></p>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">BOOL WinMain()</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">{</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>if(!IsInstanceRunningAlready)</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>{</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>return FALSE;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>}</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>//Do further operations</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>return FALSE;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">}</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">BOOL IsInstanceRunningAlready()</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">{</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>// For handling Single Instance of Application</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>HANDLE hSingleInstance = NULL;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>hSingleInstance<span style="white-space:pre;"> </span>= CreateMutex( NULL, FALSE,_T(&#8220;ApplicationName&#8221;));</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>DWORD dwLastError<span style="white-space:pre;"> </span>= GetLastError();</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span></div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>if(dwLastError == ERROR_ALREADY_EXISTS)</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>{</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>AfxMessageBox(_T(&#8220;Application already running&#8221;));</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>CloseHandle(hSingleInstance);</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>return 0;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>}</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;"><span style="white-space:pre;"> </span>return 1;</div>
<p> </p>
<p> </p>
<pre>
BOOL WinMain()
{
   if(!IsInstanceRunningAlready)
   {
      return FALSE;
   }

   //Do further operations here

   return FALSE;
}

BOOL IsInstanceRunningAlready()
{
   // For handling Single Instance of Application
   HANDLE hSingleInstance = NULL;
   hSingleInstance		= CreateMutex( NULL, FALSE,_T("ApplicationName"));
   DWORD dwLastError	= GetLastError();

   if(dwLastError == ERROR_ALREADY_EXISTS)
   {
      AfxMessageBox(_T("Application already running"));
      CloseHandle(hSingleInstance);
      return 0;
   }

   return 1;
}
</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Multithreaded Applications]]></title>
<link>http://enggtech.wordpress.com/2009/10/12/multithreaded-applications/</link>
<pubDate>Mon, 12 Oct 2009 07:54:02 +0000</pubDate>
<dc:creator>Visitor Blogs</dc:creator>
<guid>http://enggtech.wordpress.com/2009/10/12/multithreaded-applications/</guid>
<description><![CDATA[Managed Threading Threading Objects and Features A process is a collection of virtual memory space, ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><div class="MTPS_CollapsibleRegion">
<div class="CollapseRegionLink">
<div><a id="ctl00_MTCS_main_ctl37_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/3e8s7xdd.aspx">Managed Threading</a></div>
<div><a id="ctl00_MTCS_main_ctl37_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/9xyf641a.aspx">Threading Objects and Features</a></div>
<p>A <em>process</em> is a collection of virtual memory space, code, data, and system resources. A <em>thread</em> is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread.</p>
<p>Processes communicate with one another through messages, using Microsoft&#8217;s Remote Procedure Call (RPC) technology to pass information to one another. There is no difference to the caller between a call coming from a process on a remote machine and a call coming from another process on the same machine.</p>
<p>When a thread begins to execute, it continues until it is killed or until it is interrupted by a thread with higher priority (by a user action or the kernel&#8217;s thread scheduler). Each thread can run separate sections of code, or multiple threads can execute the same section of code. Threads executing the same block of code maintain separate stacks. Each thread in a process shares that process&#8217;s global variables and resources.</p>
</div>
<div class="CollapseRegionLink">Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains</div>
<div class="CollapseRegionLink">
<ul>
<li>exception handlers,</li>
<li>a scheduling priority, and</li>
<li>a set of structures the system uses to save the thread context until it is scheduled.</li>
</ul>
</div>
<div class="CollapseRegionLink"><strong>The thread context </strong>includes all the information the thread needs to seamlessly resume execution, including</div>
<div class="CollapseRegionLink">
<ul>
<li>the thread&#8217;s set of CPU registers and</li>
<li>stack, in the address space of the thread&#8217;s host process.</li>
</ul>
</div>
<div class="CollapseRegionLink">
<p>The .NET Framework further subdivides an operating system process into lightweight managed subprocesses, called <strong>application domains</strong>, represented by <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx">System..::.AppDomain</a>. One or more managed threads (represented by <a id="ctl00_MTCS_main_ctl03" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx">System.Threading..::.Thread</a>) can run in one or any number of application domains within the same managed process. Although each application domain is started with a single thread, code in that application domain can create additional application domains and additional threads. The result is that a managed thread can move freely between application domains inside the <strong>same managed process; </strong>you might have only one thread moving among several application domains.</p>
</div>
<div class="CollapseRegionLink">If your application uses only one thread of execution, you can combine <a id="ctl00_MTCS_main_ctl23_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx">asynchronous programming</a> with <a id="ctl00_MTCS_main_ctl23_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/kwdt6w2k.aspx">.NET Framework remoting</a> or <a id="ctl00_MTCS_main_ctl23_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/7bkzywba.aspx">XML Web services</a> created using ASP.NET to use the processing time of other computers in addition to that of your own to increase responsiveness to the user and decrease the data processing time of your application.</div>
<div class="CollapseRegionLink"></div>
<div class="CollapseRegionLink">
<h3>Disadvantages of Multiple Threads</h3>
<div>
<p>It is recommended that you use as few threads as possible, thereby minimizing the use of operating-system resources and improving performance. Threading also has resource requirements and potential conflicts to be considered when designing your application. The resource requirements are as follows:</p>
<ul>
<li>The system consumes memory for the context information required by processes, <strong>AppDomain </strong>objects, and threads. Therefore, the number of processes, <strong>AppDomain </strong>objects, and threads that can be created is limited by available memory.</li>
<li>Keeping track of a large number of threads consumes significant processor time. If there are too many threads, most of them will not make significant progress. <span style="color:#ff0000;">If most of the current threads are in one process, threads in other processes are scheduled less frequently</span>.</li>
<li>Controlling code execution with many threads is complex, and can be a source of many bugs.</li>
<li>Destroying threads requires knowing what could happen and handling those issues.</li>
</ul>
</div>
</div>
<div class="CollapseRegionLink">
<p>Resources that require <span style="color:#ff0000;"><strong>synchronization </strong></span>include:</p>
<ul>
<li>System resources (such as communications ports).</li>
<li>Resources shared by multiple processes (such as file handles).</li>
<li>The resources of a single application domain (such as <strong><span style="color:#0000ff;">global, static, and instance fields</span></strong>) accessed by multiple threads.</li>
</ul>
</div>
<div class="CollapseRegionLink">
<h2>Synchronizing Data for Multithreading</h2>
<p><!--Content type: Devdiv1. Transform: orcas2mtps.xslt.--></p>
<div>
<p>When multiple threads can make calls to the properties and methods of a single object, it is critical that those calls be synchronized. Otherwise one thread might interrupt what another thread is doing, and the object could be left in an invalid state. A class whose members are protected from such interruptions is called thread-safe.</p>
<p>The Common Language Infrastructure provides several strategies to synchronize access to instance and static members:</p>
<ul>
<li>Synchronized code regions. You can use the <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx">Monitor</a> class or compiler support for this class to synchronize only the code block that needs it, improving performance.</li>
<li>Manual synchronization.You can use the synchronization objects provided by the .NET Framework class library. See <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/ms228964.aspx">Overview of Synchronization Primitives</a>, which includes a discussion of the <a id="ctl00_MTCS_main_ctl03" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx">Monitor</a> class.</li>
<li>Synchronized contexts. You can use the <a id="ctl00_MTCS_main_ctl04" href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.contexts.synchronizationattribute.aspx">SynchronizationAttribute</a> to enable simple, automatic synchronization for <a id="ctl00_MTCS_main_ctl05" href="http://msdn.microsoft.com/en-us/library/system.contextboundobject.aspx">ContextBoundObject</a> objects.</li>
<li><a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/system.collections.hashtable.synchronized.aspx">Synchronized</a> property. A few classes, such as <a id="ctl00_MTCS_main_ctl07" href="http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx">Hashtable</a> and <a id="ctl00_MTCS_main_ctl08" href="http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx">Queue</a>, provide a <a id="ctl00_MTCS_main_ctl09" href="http://msdn.microsoft.com/en-us/library/system.collections.queue.synchronized.aspx">Synchronized</a> property that returns a thread-safe wrapper for an instance of the class. See <a id="ctl00_MTCS_main_ctl10" href="http://msdn.microsoft.com/en-us/library/573ths2x.aspx">Collections and Synchronization (Thread Safety)</a>.</li>
</ul>
<p>The common language runtime provides a thread model in which classes fall into a number of categories that can be synchronized in a variety of different ways depending on the requirements. The following table shows what synchronization support is provided for fields and methods with a given synchronization category.</p>
<div></div>
<div>
<table border="0">
<tbody>
<tr>
<th>Category</th>
<th>Global fields</th>
<th>Static fields</th>
<th>Static methods</th>
<th>Instance fields</th>
<th>Instance methods</th>
<th>Specific code blocks</th>
</tr>
<tr>
<td>No Synchronization</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Synchronized Context</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Synchronized Code Regions</td>
<td>No</td>
<td>No</td>
<td>Only if marked</td>
<td>No</td>
<td>Only if marked</td>
<td>Only if marked</td>
</tr>
<tr>
<td>Manual Synchronization</td>
<td>Manual</td>
<td>Manual</td>
<td>Manual</td>
<td>Manual</td>
<td>Manual</td>
<td>Manual</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<div><!-- ApplyClick with current id --> <img style="vertical-align:middle;border-width:0;" src="http://i.msdn.microsoft.com/Global/Images/clear.gif" alt="" /></div>
</div>
</div>
<div class="CollapseRegionLink"></div>
<div class="CollapseRegionLink">
<h3>Threading and Application Design</h3>
<div>
<p>In general, using the <a id="ctl00_MTCS_main_ctl23_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx">ThreadPool</a> class is the easiest way to handle multiple threads for relatively short tasks that will not block other threads and when you do not expect any particular scheduling of the tasks. However, there are a number of reasons to create <span style="color:#ff0000;">your own threads</span>:</p>
<ul>
<li>If you need a task to have a particular <strong><span style="color:#0000ff;">priority</span></strong>.</li>
<li>If you have a task that might <span style="color:#0000ff;"><strong>run a long time</strong></span> (and therefore block other tasks).</li>
<li>If you need to place threads into a single-threaded apartment (all <strong>ThreadPool </strong>threads are in the multithreaded apartment).</li>
<li>If you need a stable identity associated with the thread. For example, you should use a dedicated thread to abort that thread, suspend it, or discover it by name.</li>
<li>If you need to run background threads that interact with the user interface, the .NET Framework version 2.0 provides a <a id="ctl00_MTCS_main_ctl23_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker</a> component that communicates using events, with cross-thread marshaling to the user-interface thread.</li>
</ul>
</div>
</div>
<div class="CollapseRegionLink"><span style="font-family:Verdana,Arial,Helvetica,sans-serif;"></p>
<h4>Apartment Threading (Single Threaded Apartment)</h4>
<p>Apartment threaded means there are multiple threads within the application. In single threaded apartment (STA) each thread is isolated in a separate apartment underneath the process. The process can have any number of apartments that share data through a proxy. The application defines when and for how long the thread in each apartment should execute. All requests are serialized through the Windows message queue such that only a single apartment is accessed at a time and thus only a single thread will be executing at any one time. STA is the threading model that most Visual Basic developers are familiar with because this is the threading model available to VB applications prior to VB.NET. You can think of it like an apartment building full of a row of one room apartments that are accessible one at a time through a single hallway. The advantage this provides over single threaded is that multiple commands can be issued at one time instead of just a single command, but the commands are still <span style="color:#ff0000;"><strong>sequentially executed</strong></span>.</p>
<h4>Free Threading (Multi Threaded Apartment)</h4>
<p>Free threaded applications were limited to programming languages such as C++ until the release of Microsoft .NET. The free threaded/Multi Threaded Apartment (MTA) model has a single apartment created underneath the process rather than multiple apartments. This single apartment holds multiple threads rather than just a single thread. No message queue is required because all of the threads are a part of the same apartment and <span style="color:#ff0000;"><strong>can share data without a proxy</strong></span>. You can think of it like a building with multiple rooms that are all accessible once you are inside the building. These applications typically execute faster than single threaded and STA because there is less system overhead and can be optimized to eliminate system idle time.</p>
<p>These types of applications are more complex to program. The developer must provide <span style="color:#ff0000;"><strong>thread synchronization</strong></span> as part of the code to ensure that threads do not simultaneously access the same resources. A condition known as a <span style="color:#ff0000;"><strong>race condition</strong></span> can occur when a thread accesses a shared resource and modifies the resource to an invalid state and then another thread accesses the shared resource and uses it in the invalid state before the other thread can return the resource to a valid state. Therefore it is necessary to place a lock on a resource to prevent other threads from accessing the resource until the lock has been removed. However, this can lead to a <span style="color:#ff0000;"><strong>deadlock </strong></span>situation where two threads are competing for resources and neither can proceed.</p>
<p>The only way to avoid situations like these is through good design and testing.</p>
<p></span></div>
<div class="CollapseRegionLink">
<p>Rules for <span style="color:#ff0000;"><strong>single-threaded apartments</strong></span> are simple, but it is important to follow them carefully:</p>
<ul>
<li> Every object should live on only one thread (within a single-threaded apartment).</li>
<li> Initialize the COM library for each thread.</li>
<li> Marshal all pointers to objects when passing them between apartments.</li>
<li> Each single-threaded apartment must have a message loop to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch the broadcast messages that some applications use.</li>
<li>DLL-based or in-process objects do not call the COM initialization functions; instead, they register their threading model with the <strong>ThreadingModel</strong> named-value under the <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/ms682390%28VS.85%29.aspx">InprocServer32</a> key in the registry. Apartment-aware objects must also write DLL entry points carefully. There are special considerations that apply to threading in-process servers. For more information, see <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/ms687205%28VS.85%29.aspx">In-Process Server Threading Issues</a>.</li>
</ul>
</div>
<div class="CollapseRegionLink">
<p>Following are some important considerations regarding <span style="color:#ff0000;"><strong>synchronization for multithreaded apartments</strong></span>:</p>
<ul>
<li> COM provides call synchronization for single-threaded apartments only.</li>
<li> Multithreaded apartments do not receive calls while making calls (on the same thread).</li>
<li> Multithreaded apartments cannot make input-synchronized calls.</li>
<li> Asynchronous calls are converted to synchronous calls in multithreaded apartments.</li>
<li> The message filter is not called for any thread in a multithreaded apartment.</li>
</ul>
</div>
<div class="CollapseRegionLink">
<div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_caution.gif]--><img src="http://i.msdn.microsoft.com/z8chs7ft.alert_caution%28en-us,VS.90%29.gif" alt="Important note" /><strong>Important Note:</strong></th>
</tr>
<tr>
<td>Do not lock the type — that is, <span style="color:#0000ff;">typeof(MyType)</span> in C#, GetType(MyType) in Visual Basic, or <span style="color:#0000ff;">MyType::typeid</span> in C++ — in order to protect <span style="color:#ff0000;"><strong>static </strong></span>methods (Shared methods in Visual Basic). Use a <span style="color:#0000ff;"><strong>private static object i</strong></span>nstead. Similarly, do not use<span style="color:#0000ff;"><strong> this</strong></span> in C# (Me in Visual Basic) to lock instance methods. Use a private object instead. A class or instance<span style="color:#ff0000;"><strong> can be locked by code other than your own</strong></span>, potentially causing deadlocks or performance problems.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="CollapseRegionLink">
<h3>Synchronized Context</h3>
<div style="display:block;"><a id="sectionToggle3"></a>You can use the <strong>SynchronizationAttribute </strong>on any <strong>ContextBoundObject </strong>to synchronize all instance methods and fields. All objects in the same context domain share the same lock. Multiple threads are allowed to access the methods and fields, but only a single thread is allowed at any one time.</p>
</div>
</div>
<div class="CollapseRegionLink"></div>
<div class="CollapseRegionLink"><strong>Setting Up for a Background Operation </strong></div>
<div class="MTPS_CollapsibleSection" style="display:block;"><a id="sectionToggle1"></a>To set up for a background operation, add an event handler for the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx">DoWork</a> event. Call your time-consuming operation in this event handler.To start the operation, call <a id="ctl00_MTCS_main_ctl59_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkerasync.aspx">RunWorkerAsync</a>. To receive notifications of progress updates, handle the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx">ProgressChanged</a> event. To receive a notification when the operation is completed, handle the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx">RunWorkerCompleted</a> event.The methods that handle the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx">ProgressChanged</a> and <a id="ctl00_MTCS_main_ctl59_ctl00_ctl05" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx">RunWorkerCompleted</a> events can access the application&#8217;s user interface, because those events are raised on the thread that called the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl06" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkerasync.aspx">RunWorkerAsync</a> method. However, the <a id="ctl00_MTCS_main_ctl59_ctl00_ctl07" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx">DoWork</a> event handler cannot work with any user-interface objects because it runs on the background thread.</p>
</div>
</div>
<div class="MTPS_CollapsibleRegion">
<div class="CollapseRegionLink"><!-- ApplyClick with current id --> <strong> Creating and Using Threads </strong></div>
<div class="MTPS_CollapsibleSection" style="display:block;"><a id="sectionToggle2"></a>If you need more control over the behavior of your application&#8217;s threads, you can manage the threads yourself. However, realize that writing correct multithreaded applications can be difficult: Your application may stop responding or experience transient errors caused by race conditions. For more information, see <a id="ctl00_MTCS_main_ctl60_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/a8544e2s.aspx">Thread-Safe Components</a>.You create a new thread in Visual Basic by declaring a variable of type <a id="ctl00_MTCS_main_ctl60_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx">Thread</a> and calling the constructor with the <span class="input">AddressOf</span> statement and the name of the procedure or method that you want to execute on the new thread.</div>
</div>
<h3 class="subHeading">Safe Points</h3>
<div class="subsection">
<p>Most of these methods are self-explanatory, but the concept of <span class="parameter">safe points</span> may be new to you. Safe points are locations in code where it is safe for the common language runtime to perform automatic <span class="parameter">garbage collection</span>, the process of releasing unused variables and reclaiming memory. When you call the <a id="ctl00_MTCS_main_ctl60_ctl00_ctl18" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx">Abort</a> or <a id="ctl00_MTCS_main_ctl60_ctl00_ctl19" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx">Suspend</a> method of a thread, the common language runtime analyzes the code and determines the location of an appropriate location for the thread to stop running.</p>
</div>
<p><a href="http://msdn.microsoft.com/en-us/library/ck8bc5c6.aspx">Multithreaded Applications</a>.</p>
<p>You can provide parameters and return values to thread-pool threads by using the optional <span class="input">ByVal</span> state-object variable of the <a id="ctl00_MTCS_main_ctl21_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx">QueueUserWorkItem</a> method. Thread-timer threads also support a state object for this purpose. For information on thread pooling and thread timers, see <a id="ctl00_MTCS_main_ctl21_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/h4732ks0.aspx">Thread Pooling</a> and <a id="ctl00_MTCS_main_ctl21_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/swx5easy.aspx">Thread Timers</a>.</p>
<div class="CollapseRegionLink"><strong><a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx">Thread Pooling</a> Example </strong></div>
<div class="MTPS_CollapsibleSection" style="display:block;"><a id="sectionToggle0"></a>The following example shows how you can use thread pooling to start several tasks.</p>
<div id="snippetGroup"><span id="ctl00_MTCS_main_ctl17_ctl00_ctl00"></p>
<div id="ctl00_MTCS_main_ctl17_ctl00_ctl00_VisualBasic" class="libCScode">
<div class="CodeSnippetTitleBar">
<div class="CodeDisplayLanguage">Visual Basic</div>
</div>
<div style="background-color:#dddddd;" dir="ltr">
<pre class="libCScode" style="white-space:pre-wrap;"><span style="color:blue;">Sub</span> DoWork()
    <span style="color:green;">' Queue a task</span>
    System.Threading.ThreadPool.QueueUserWorkItem( _
        <span style="color:blue;">New</span> System.Threading.WaitCallback(<span style="color:blue;">AddressOf</span> SomeLongTask))
    <span style="color:green;">' Queue another task</span>
    System.Threading.ThreadPool.QueueUserWorkItem( _
        <span style="color:blue;">New</span> System.Threading.WaitCallback(<span style="color:blue;">AddressOf</span> AnotherLongTask))
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>
<span style="color:blue;">Sub</span> SomeLongTask(<span style="color:blue;">ByVal</span> state <span style="color:blue;">As</span> <span style="color:blue;">Object</span>)
    <span style="color:green;">' Insert code to perform a long task.</span>
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>
<span style="color:blue;">Sub</span> AnotherLongTask(<span style="color:blue;">ByVal</span> state <span style="color:blue;">As</span> <span style="color:blue;">Object</span>)
    <span style="color:green;">' Insert code to perform another long task.</span>
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span></pre>
</div>
</div>
<p></span></div>
<p>Thread pooling is useful when you want to start many separate tasks without individually setting the properties of each thread. Each thread starts with a default stack size and priority. By default, up to 25 thread-pool threads can run per system processor. Additional threads in excess of the limit can be queued, but they do not start until other threads finish.</p>
<p>One advantage of thread pooling is that you can pass arguments in a state object to the task procedure. If the procedure you are calling requires more than one argument, you can cast a structure or an instance of a class into an <span class="input">Object</span> data type.</p>
</div>
<h2><strong><a href="http://msdn.microsoft.com/en-us/library/a60kkx8k.aspx">Application Domains and Threads</a></strong>.</h2>
<div>
<p>An application domain forms an isolation boundary for security, versioning, reliability, and unloading of managed code. Threads are the operating system construct used by the common language runtime to execute code. At run time, all managed code is loaded into an application domain and is run by a managed thread.</p>
<p>There is not a one-to-one correlation between application domains and threads. Several threads can be executing in a single application domain at any given time and a particular thread is not confined to a single application domain. That is, threads are free to cross application domain boundaries; a new thread is not created for each application domain.</p>
<p>At any given time, every thread is executing in an application domain. Zero, one, or more than one thread might be executing in any given application domain. The run time keeps track of which threads are running in which application domains. You can locate the domain in which a thread is executing at any time by calling the <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.getdomain.aspx">GetDomain</a> method.</p>
</div>
<div class="MTPS_CollapsibleRegion">
<div class="MTPS_CollapsibleSection" style="display:block;">
<h2><strong><a href="http://msdn.microsoft.com/en-us/library/awbftdfh(VS.80).aspx">Events (C#)</a>.</strong></h2>
<p>Events can be used to <strong>synchronize threads</strong>.</p>
<p><strong>Delegate </strong>is a creatable type. Programming with delegates requires creating delegate objects from delegate types.</p>
<p>Delegates seamlessly support binding a notification source to multiple handler methods through a feature known as <strong>multicasting</strong>.</p>
<p>Delegate binding, see <a id="ctl00_MTCS_main_ctl35_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/96b1ayy4%28VS.80%29.aspx">Delegates in the Common Type System</a> and <a id="ctl00_MTCS_main_ctl35_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/s3860fy3%28VS.80%29.aspx">CreateDelegate(Type,Object,MethodInfo,Boolean)</a>.</p>
<p>Delegates are referred to as</p>
<ul>
<li>multicast, or</li>
<li>combinable,</li>
</ul>
<p>because a delegate can invoke one or more methods and can be used in combining operations.A delegate can represent a</p>
<ul>
<li>static method or</li>
<li>an instance method.</li>
</ul>
<div class="MTPS_CollapsibleSection" style="display:block;">When the delegate represents an instance method, the delegate stores not only a reference to the method&#8217;s entry point, but also a reference to the class instance. Unlike <strong>function pointers</strong>,</div>
<ul>
<li>delegates are object oriented and</li>
<li>type safe.</li>
</ul>
<p><a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/ms366768%28VS.80%29.aspx">How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)</a></p>
<p>The line of code that is needed to subscribe to the event is also automatically generated in the <tt>InitializeComponent</tt> method in the Form1.Designer.cs file in your project. It looks like this:</p>
<p><span id="ctl00_MTCS_main_ctl02"> </span></p>
<div id="ctl00_MTCS_main_ctl02_">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">  this.Load += new System.EventHandler(this.Form1_Load);</pre>
</div>
</div>
<h3>To subscribe to events programmatically</h3>
<div>
<ol>
<li>Define an event handler method whose signature matches the delegate signature for the event. For example, if the event is based on the <a id="ctl00_MTCS_main_ctl03" href="http://msdn.microsoft.com/en-us/library/system.eventhandler%28VS.80%29.aspx">EventHandler</a> delegate type, then the following code represents the method stub:<span id="ctl00_MTCS_main_ctl04"> </span>
<div id="ctl00_MTCS_main_ctl04_">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">  void HandleCustomEvent(object sender, CustomEventArgs a)
  {
     // Do something useful here.
  }</pre>
</div>
</div>
</li>
<li>Use the addition assignment operator (+=) to attach your event handler to the event. In the following example, assume that an object named <tt>publisher</tt> has an event named <tt>RaiseCustomEvent</tt>. Note that the subscriber class needs a reference to the publisher class in order to subscribe to its events.<span id="ctl00_MTCS_main_ctl05"> </span>
<div id="ctl00_MTCS_main_ctl05_">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">publisher.RaiseCustomEvent += HandleCustomEvent;</pre>
</div>
</div>
</li>
</ol>
<p>Note that the above syntax is new in C# 2.0. It is exactly equivalent to the C# 1.0 syntax in which the encapsulating delegate must be explicitly created using the new keyword:</p>
<p><span id="ctl00_MTCS_main_ctl06"> </span></p>
<div id="ctl00_MTCS_main_ctl06_">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">publisher.RaiseCustomEvent += new CustomEventHandler(HandleCustomEvent);</pre>
</div>
</div>
</div>
<h3>To subscribe to events by using an anonymous method</h3>
<div>
<ul>
<li>Use the addition assignment operator (+=) to attach your anonymous method to the event. In the following example, assume that an object named <tt>publisher</tt> has an event named <tt>RaiseCustomEvent </tt>and that a <tt>CustomEventArgs</tt> class has also been defined to carry some kind of specialized event information. Note that the subscriber class needs a reference to <tt>publisher</tt> in order to subscribe to its events.<span id="ctl00_MTCS_main_ctl07"> </span>
<div id="ctl00_MTCS_main_ctl07_">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">  publisher.RaiseCustomEvent += delegate(object o, CustomEventArgs e)
  {
    string s = o.ToString() + " " + e.ToString();
    Console.WriteLine(s);
  };</pre>
</div>
</div>
</li>
</ul>
<p>It is important to note that you cannot easily unsubscribe from an event if you used an anonymous method to subscribe to it. To unsubscribe in this scenario, go back to the code where you subscribe to the event, store the anonymous method in a delegate variable, and then add the delegate to the event.</p>
<p>When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. To invoke events asynchronously, see <strong><a id="ctl00_MTCS_main_ctl03" href="http://msdn.microsoft.com/en-us/library/2e08f6yc%28VS.80%29.aspx">Calling Synchronous Methods Asynchronously</a></strong>.</p>
<p>four common ways to use <strong>BeginInvoke</strong> and <strong>EndInvoke</strong> to make asynchronous calls. After calling <strong>BeginInvoke</strong> you can do the following:</p>
<ul>
<li>Do some work and then call <strong>EndInvoke</strong> to block until the call completes.</li>
<li>Obtain a <a id="ctl00_MTCS_main_ctl05" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle%28VS.80%29.aspx">WaitHandle</a> using the <a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncwaithandle%28VS.80%29.aspx">System.IAsyncResult.AsyncWaitHandle</a> property, use its <a id="ctl00_MTCS_main_ctl07" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitone%28VS.80%29.aspx">WaitOne</a> method to block execution until the <strong>WaitHandle</strong> is signaled, and then call <strong>EndInvoke</strong>.</li>
<li>Poll the <strong>IAsyncResult</strong> returned by <strong>BeginInvoke</strong> to determine when the asynchronous call has completed, and then call <strong>EndInvoke</strong>.</li>
<li>Pass a delegate for a callback method to <strong>BeginInvoke</strong>. The method is executed on a <a id="ctl00_MTCS_main_ctl08" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool%28VS.80%29.aspx">ThreadPool</a> thread when the asynchronous call completes. The callback method calls <strong>EndInvoke</strong>.
<div>
<table border="0" width="100%">
<tbody>
<tr>
<th align="left"><img src="http://i.msdn.microsoft.com/2e08f6yc.note%28en-US,VS.80%29.gif" alt="Note" />Important</th>
</tr>
<tr>
<td>Always call <strong>EndInvoke</strong> to complete your asynchronous call.</td>
</tr>
</tbody>
</table>
</div>
</li>
</ul>
<p>you should know that events in the .NET Framework are layered on top of delegates. When you use an event-driven application framework such as Windows® Forms or ASP.NET, your knowledge of delegates will make you a much stronger developer.</p>
<blockquote><p>Delegates provide the primary means in .NET for executing a method on a secondary thread in an asynchronous fashion. Therefore, delegates open the door to <strong>multithreading</strong>.</p></blockquote>
</div>
<p>the compiler generates a class definition for each delegate type. The class that is generated for each delegate type is a creatable class that inherits from the <strong>System.Multicast </strong>delegate.</p>
<p>compiler has also added a public constructor and three public methods named</p>
<ul>
<li>Invoke,</li>
<li>BeginInvoke, and</li>
<li>EndInvoke.</li>
</ul>
<h2><strong><a id="ctl00_MTCS_main_ctl21" href="http://msdn.microsoft.com/en-us/library/ms228969%28VS.80%29.aspx">Asynchronous Programming Design Patterns</a></strong></h2>
<p>Asynchronous operations are typically used to perform tasks that might take a long time to complete, such as opening large files, connecting to remote computers, or querying a database. An asynchronous operation executes in a thread separate from the main application thread. When an application calls methods to perform an operation asynchronously, the application can continue executing while the asynchronous method performs its task.</p>
<p>Asynchronous programming is a feature supported by many areas of the <strong>.NET Framework</strong>, including:</p>
<ul>
<li>File IO, Stream IO, Socket IO.</li>
<li>Networking.</li>
<li>Remoting channels (HTTP, TCP) and proxies.</li>
<li>XML Web services created using ASP.NET.</li>
<li>ASP.NET Web Forms.</li>
<li>Message Queuing using the <a id="ctl00_MTCS_main_ctl03" href="http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue%28VS.80%29.aspx">MessageQueue</a> class.</li>
</ul>
<p>The .NET Framework provides two design patterns for asynchronous operations:</p>
<ul>
<li>Asynchronous operations that use <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/system.iasyncresult%28VS.80%29.aspx">IAsyncResult</a> objects.</li>
<li>Asynchronous operations that use events. <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/wewwczdw%28VS.80%29.aspx">Event-based Asynchronous Pattern Overview</a>.</li>
</ul>
<p>For relatively simple multithreaded applications, the <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28VS.80%29.aspx">BackgroundWorker</a> component provides a straightforward solution. For more sophisticated asynchronous applications, consider implementing a class that adheres to the Event-based Asynchronous Pattern.
<dl>
<dt><strong><a id="ctl00_MTCS_main_ctl04" href="http://msdn.microsoft.com/en-us/library/ms228963%28VS.80%29.aspx">Asynchronous Programming Overview</a></strong></dt>
<dd>Discusses the <strong>IAsyncResult</strong> based asynchronous design pattern, which provides a flexible programming model to deal with asynchronous operations. </dd>
</dl>
<p>The correct choice depends on whether the application has instructions that can execute while the operation completes. If an application cannot perform any additional work until it receives the results of the asynchronous operation, the application must block until the results are available. <strong>To block until an asynchronous operation completes</strong>, you can use one of the following approaches:</p>
<ul>
<li>Call <strong>End</strong><em>OperationName</em> from the application’s main thread, blocking application execution until the operation is complete. For an example that illustrates this technique, see <a id="ctl00_MTCS_main_ctl16" href="http://msdn.microsoft.com/en-us/library/ms228967%28VS.80%29.aspx">Blocking Application Execution by Ending an Asynchronous Operation</a>.</li>
<li>Use the <strong>AsyncWaitHandle</strong> to block application execution until one or more operations are complete. For an example that illustrates this technique, see <a id="ctl00_MTCS_main_ctl17" href="http://msdn.microsoft.com/en-us/library/ms228962%28VS.80%29.aspx">Blocking Application Execution Using an AsyncWaitHandle</a>.</li>
</ul>
<p>Applications that do <strong>not </strong>need to block while the asynchronous operation completes can use one of the following approaches:</p>
<ul>
<li>Poll for operation completion status by checking the <strong>IsCompleted</strong> property periodically and calling <strong>End</strong><em>OperationName</em> when the operation is complete. For an example that illustrates this technique, see <a id="ctl00_MTCS_main_ctl18" href="http://msdn.microsoft.com/en-us/library/ms228968%28VS.80%29.aspx">Polling for the Status of an Asynchronous Operation</a>.</li>
<li>Use an <strong>AsyncCallback</strong> delegate to specify a method to be invoked when the operation is complete. For an example that illustrates this technique, see <a id="ctl00_MTCS_main_ctl19" href="http://msdn.microsoft.com/en-us/library/ms228972%28VS.80%29.aspx">Using an AsyncCallback Delegate to End an Asynchronous Operation</a>.</li>
</ul>
<p><a id="ctl00_MTCS_main_ctl18" href="http://msdn.microsoft.com/en-us/library/dacysss4%28VS.80%29.aspx">Creating Event Handlers in Windows Forms</a></p>
<ul>
<li><a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/ms366768%28VS.80%29.aspx">How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl07" href="http://msdn.microsoft.com/en-us/library/w369ty8x%28VS.80%29.aspx">How to: Publish Events that Conform to .NET Framework Guidelines (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl08" href="http://msdn.microsoft.com/en-us/library/hy3sefw3%28VS.80%29.aspx">How to: Raise Base Class Events in Derived Classes (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl09" href="http://msdn.microsoft.com/en-us/library/ak9w5846%28VS.80%29.aspx">How to: Implement Interface Events (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl10" href="http://msdn.microsoft.com/en-us/library/ms173179%28VS.80%29.aspx">Thread Synchronization (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl11" href="http://msdn.microsoft.com/en-us/library/z4ka55h8%28VS.80%29.aspx">How to: Use a Dictionary to Store Event Instances (C# Programming Guide)</a></li>
<li><a id="ctl00_MTCS_main_ctl12" href="http://msdn.microsoft.com/en-us/library/y4592h76%28VS.80%29.aspx">Events Sample</a></li>
<li><a id="ctl00_MTCS_main_ctl13" href="http://msdn.microsoft.com/en-us/library/ms229011%28VS.80%29.aspx">Event Design</a></li>
</ul>
<p><a id="ctl00_MTCS_main_ctl16" href="http://msdn.microsoft.com/en-us/library/3es4b6yy%28VS.80%29.aspx">Multithreading in Components</a></p>
<p><a id="ctl00_MTCS_main_ctl14" href="http://msdn.microsoft.com/en-us/library/1c9txz50%28VS.80%29.aspx">Managed Threading Best Practices</a></p>
<dl>
<dt><a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/e7a34yad%28VS.80%29.aspx">Implementing the Event-based Asynchronous Pattern</a></dt>
<dd>Describes the standardized way to package a class that has asynchronous features. </dd>
</dl>
<p>features of the <strong>Event-based Asynchronous Pattern</strong> discussed in this topic.</p>
<ul>
<li>Opportunities for Implementing the Event-based Asynchronous Pattern</li>
<li>Naming Asynchronous Methods</li>
<li>Optionally Support Cancellation</li>
<li>Optionally Support the IsBusy Property</li>
<li>Optionally Provide Support for Progress Reporting</li>
<li>Optionally Provide Support for Returning Incremental Results</li>
<li>Handling Out and Ref Parameters in Methods</li>
</ul>
<dl>
<dt><a id="ctl00_MTCS_main_ctl04" href="http://msdn.microsoft.com/en-us/library/ms228966%28VS.80%29.aspx">Deciding When to Implement the Event-based Asynchronous Pattern</a></dt>
<dd>Describes how to determine when you should choose to implement the Event-based Asynchronous Pattern instead of the <a id="ctl00_MTCS_main_ctl05" href="http://msdn.microsoft.com/en-us/library/system.iasyncresult%28VS.80%29.aspx">IAsyncResult</a> pattern. </dd>
</dl>
<dl>
<dt><a id="ctl00_MTCS_main_ctl12" href="http://msdn.microsoft.com/en-us/library/khfcee8y%28VS.80%29.aspx">Event-based Asynchronous Pattern Technology Sample</a></dt>
<dd>Shows how to use the Event-based Asynchronous Pattern to perform common asynchronous operations. </dd>
</dl>
<p><a id="ctl00_MTCS_main_ctl15" href="http://msdn.microsoft.com/en-us/library/17sde2xt%28VS.80%29.aspx">Events and Delegates</a></p>
<p><strong><a id="ctl00_MTCS_main_ctl16" href="http://msdn.microsoft.com/en-us/library/hybbz6ke%28VS.80%29.aspx">How to: Run an Operation in the Background</a></strong></p>
<p>use the <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28VS.80%29.aspx">BackgroundWorker</a> class to run the operation on another thread.</p>
<p><span id="ctl00_MTCS_main_ctl10"> </span></p>
<h1>Robust Programming</h1>
<div id="robustProgrammingSection">
<div>
<table border="0" width="100%">
<tbody>
<tr>
<th align="left"><img src="http://i.msdn.microsoft.com/waw3xexc.Caution%28en-US,VS.80%29.gif" alt="Caution note" />Caution</th>
</tr>
<tr>
<td>When using multithreading of any sort, you potentially expose yourself to very serious and complex bugs. Consult the <a id="ctl00_MTCS_main_ctl12" href="http://msdn.microsoft.com/en-us/library/1c9txz50%28VS.80%29.aspx">Managed Threading Best Practices</a> before implementing any solution that uses multithreading.</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3>Deadlocks</h3>
<p>A deadlock occurs when each of two threads tries to lock a resource the other has already locked. Neither thread can make any further progress.</p>
<p>Many methods of the managed threading classes provide time-outs to help you detect deadlocks. For example, the following code attempts to acquire a lock on the current instance. If the lock is not obtained in 300 milliseconds, <a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.tryenter%28VS.80%29.aspx">Monitor.TryEnter</a> returns <strong>false</strong>.</p>
<h3>Race Conditions</h3>
<div>
<p>A race condition is a bug that occurs when the outcome of a program depends on which of two or more threads reaches a particular block of code first. Running the program many times produces different results, and the result of any given run cannot be predicted.</p>
<p>A simple example of a race condition is incrementing a field. Suppose a class has a private <strong>static</strong> field (<strong>Shared</strong> in Visual Basic) that is incremented every time an instance of the class is created, using code such as <tt>objCt++;</tt> (C#) or <tt>objCt += 1</tt> (Visual Basic). This operation requires loading the value from <tt>objCt</tt> into a register, incrementing the value, and storing it in <tt>objCt</tt>.</p>
<p>In a multithreaded application, a thread that has loaded and incremented the value might be preempted by another thread which performs all three steps; when the first thread resumes execution and stores its value, it overwrites <tt>objCt</tt> without taking into account the fact that the value has changed in the interim.</p>
<p>This particular race condition is easily avoided by using methods of the <a id="ctl00_MTCS_main_ctl04" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked%28VS.80%29.aspx">Interlocked</a> class, such as <a id="ctl00_MTCS_main_ctl05" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.increment%28VS.80%29.aspx">Interlocked.Increment</a>. To read about other techniques for synchronizing data among multiple threads, see <a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/z8chs7ft%28VS.80%29.aspx">Synchronizing Data for Multithreading</a>.</p>
<p>Race conditions can also occur when you synchronize the activities of multiple threads. Whenever you write a line of code, you must consider what might happen if a thread were preempted before executing the line (or before any of the individual machine instructions that make up the line), and another thread overtook it.</p>
<p><a id="ctl00_MTCS_main_ctl54_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/ms228964.aspx">Overview of Synchronization Primitives</a></p>
<p>The .NET Framework provides a range of synchronization primitives for controlling the interactions of threads and avoiding race conditions. These can be roughly divided into three categories:</p>
<ul>
<li>locking,</li>
<li>signaling, and</li>
<li>interlocked operations.</li>
</ul>
<p>The categories are not tidy and clearly defined: some synchronization mechanisms have characteristics of multiple categories; events that release a single thread at a time are functionally like locks; the release of any lock can be thought of as a signal; and interlocked operations can be used to construct locks. However, the categories are still useful.</p>
<h3>Signaling</h3>
<p><a id="sectionToggle1"></a>The simplest way to wait for a signal from another thread is to call the <a id="ctl00_MTCS_main_ctl100_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.join.aspx">Join</a> method, which blocks until the other thread completes. <a id="ctl00_MTCS_main_ctl100_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.join.aspx">Join</a> has two overloads that allow the blocked thread to break out of the wait after a specified interval has elapsed.</p>
<p>Wait handles provide a much richer set of waiting and signaling capabilities.</p>
<h3>Wait Handles</h3>
<div>
<p>Wait handles derive from the <a id="ctl00_MTCS_main_ctl100_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx">WaitHandle</a> class, which in turn derives from <a id="ctl00_MTCS_main_ctl100_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx">MarshalByRefObject</a>. Thus, wait handles can be used to synchronize the activities of threads across application domain boundaries.</p>
<p>Threads block on wait handles by calling the instance method <a id="ctl00_MTCS_main_ctl100_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitone.aspx">WaitOne</a> or one of the static methods <a id="ctl00_MTCS_main_ctl100_ctl00_ctl05" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitall.aspx">WaitAll</a>, <a id="ctl00_MTCS_main_ctl100_ctl00_ctl06" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitany.aspx">WaitAny</a>, or <a id="ctl00_MTCS_main_ctl100_ctl00_ctl07" href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.signalandwait.aspx">SignalAndWait</a>. How they are released depends on which method was called, and on the kind of wait handles.</p>
<p>For a conceptual overview, see <a id="ctl00_MTCS_main_ctl100_ctl00_ctl08" href="http://msdn.microsoft.com/en-us/library/kad9xah9.aspx">Wait Handles</a>. Also <a id="ctl00_MTCS_main_ctl100_ctl00_ctl19" href="http://msdn.microsoft.com/en-us/library/ksb7zs2x.aspx">EventWaitHandle, AutoResetEvent, and ManualResetEvent</a>.</p>
<h3>Interlocked Operations</h3>
<div style="display:block;"><a id="sectionToggle2"></a>Interlocked operations are simple atomic operations performed on a memory location by static methods of the <a id="ctl00_MTCS_main_ctl101_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.aspx">Interlocked</a> class. Those atomic operations include addition, increment and decrement, exchange, conditional exchange depending on a comparison, and read operations for 64-bit values on 32-bit platforms.</p>
<div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--><img src="http://i.msdn.microsoft.com/ms228964.alert_note%28en-us,VS.90%29.gif" alt="Note" /><strong>Note:</strong></th>
</tr>
<tr>
<td>The guarantee of atomicity is limited to individual operations; when multiple operations must be performed as a unit, a more coarse-grained synchronization mechanism must be used.</td>
</tr>
</tbody>
</table>
</div>
<p>Although none of these operations are locks or signals, they can be used to construct locks and signals. Because they are native to the Windows operating system, interlocked operations are extremely fast.</p>
<p>Interlocked operations can be used with volatile memory guarantees to write applications which exhibit powerful non-blocking concurrency, however they require sophisticated, low level programming, and for most purposes simple locks are a better choice.</p>
<p>For a conceptual overview, see <a id="ctl00_MTCS_main_ctl101_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/sbhbke0y.aspx">Interlocked Operations</a>.</p>
</div>
</div>
<h2><a id="ctl00_MTCS_main_ctl54_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.contexts.synchronizationattribute.aspx">SynchronizationAttribute</a></h2>
<p>When this attribute is applied to an object, only one thread can be executing in all contexts that share an instance of this property. This is done by contributing sinks that intercept and serialize incoming calls for the respective contexts. If the property is marked for reentry, then callouts are intercepted too. The callout interception allows other waiting threads to enter the synchronization domain for maximal throughput.</p>
<div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--><img src="http://i.msdn.microsoft.com/8bf9h00k.alert_note%28en-us,VS.90%29.gif" alt="Note" /><strong>Note:</strong></th>
</tr>
<tr>
<td>There are two classes named SynchronizationAttribute : one in the <a id="ctl00_MTCS_main_ctl56_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.contexts.aspx">System.Runtime.Remoting.Contexts</a> namespace, and the other in the <a id="ctl00_MTCS_main_ctl56_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.enterpriseservices.aspx">System.EnterpriseServices</a> namespace. The <a id="ctl00_MTCS_main_ctl56_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.enterpriseservices.synchronizationattribute.aspx">System.EnterpriseServices..::.SynchronizationAttribute</a> class supports only synchronous calls, and can be used only with serviced components. (For more information on serviced components, see <a id="ctl00_MTCS_main_ctl56_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/7c05y13x.aspx">[&#60;topic://cpconservicedcomponentoverview&#62;]</a>.) The System.Runtime.Remoting.Contexts..::.SynchronizationAttribute supports both synchronous and asynchronous calls, and can be used only with context bound objects. (For more information on context bound objects, see the <a id="ctl00_MTCS_main_ctl56_ctl00_ctl06" href="http://msdn.microsoft.com/en-us/library/system.contextboundobject.aspx">ContextBoundObject</a> class.)</td>
</tr>
</tbody>
</table>
</div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--></th>
</tr>
</tbody>
</table>
<h1>Recommendations for Class Libraries</h1>
<div id="sectionSection4">
<p>Consider the following guidelines when designing class libraries for <strong>multithreading</strong>:</p>
<ul>
<li>Avoid the need for synchronization, if possible. This is especially true for heavily used code. For example, an algorithm might be adjusted to tolerate a race condition rather than eliminate it. Unnecessary synchronization decreases performance and creates the possibility of deadlocks and race conditions.</li>
<li>Make <strong>static </strong>data (<strong>Shared</strong> in Visual Basic) thread safe by default.</li>
<li>Do not make instance data thread safe by default. Adding locks to create thread-safe code decreases <strong>performance, </strong>increases lock contention, and creates the possibility for deadlocks to occur. In common application models, only one thread at a time executes user code, which minimizes the need for thread safety. For this reason, the .NET Framework class libraries are not thread safe by default.</li>
<li>Avoid providing static methods that alter static state. In common server scenarios, static state is shared across requests, which means multiple threads can execute that code at the same time. This opens up the possibility of threading bugs. Consider using a <strong>design pattern that encapsulates data into instances that are not shared across requests</strong>. Furthermore, if static data are synchronized, calls between static methods that alter state can result in deadlocks or redundant synchronization, adversely affecting performance.</li>
</ul>
</div>
<h1>General Recommendations</h1>
<p>Consider the following guidelines when using multiple threads:</p>
<ul>
<li>Don&#8217;t use <a id="ctl00_MTCS_main_ctl09" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.abort%28VS.80%29.aspx">Thread.Abort</a> to terminate other threads. Calling <strong>Abort</strong> on another thread is akin to throwing an exception on that thread, without knowing what point that thread has reached in its processing.</li>
<li>Don&#8217;t use <a id="ctl00_MTCS_main_ctl10" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend%28VS.80%29.aspx">Thread.Suspend</a> and <a id="ctl00_MTCS_main_ctl11" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.resume%28VS.80%29.aspx">Thread.Resume</a> to synchronize the activities of multiple threads. Do use</li>
<li>
<ul>
<li><a id="ctl00_MTCS_main_ctl12" href="http://msdn.microsoft.com/en-us/library/system.threading.mutex%28VS.80%29.aspx">Mutex</a>,</li>
<li><a id="ctl00_MTCS_main_ctl13" href="http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28VS.80%29.aspx">ManualResetEvent</a>,</li>
<li><a id="ctl00_MTCS_main_ctl14" href="http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent%28VS.80%29.aspx">AutoResetEvent</a>,</li>
<li><a id="ctl00_MTCS_main_ctl15" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor%28VS.80%29.aspx">Monitor</a>.</li>
</ul>
</li>
<li><strong>Don&#8217;t control the execution of worker threads from your main program (using events, for ex</strong>ample). Instead, design your program so that worker threads are responsible for waiting until work is available, executing it, and notifying other parts of your program when finished. If your worker threads do not block, consider using <strong><a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx">thread pool</a> threads</strong>. <a id="ctl00_MTCS_main_ctl16" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.pulseall%28VS.80%29.aspx">Monitor.PulseAll</a> is useful in situations where worker threads block.</li>
<li><a href="http://msdn.microsoft.com/en-us/magazine/cc164139.aspx#S1">The Birth of the Thread Pool</a><br />
<a href="http://msdn.microsoft.com/en-us/magazine/cc164139.aspx#S2">Capability 1: Calling a Method Asynchronously</a><br />
<a href="http://msdn.microsoft.com/en-us/magazine/cc164139.aspx#S3">Capability 2: Calling a Method at Timed Intervals</a><br />
<a href="http://msdn.microsoft.com/en-us/magazine/cc164139.aspx#S4">Capability 3: Calling a Method When a Single Kernel Object Becomes Signaled</a></li>
<li>Don&#8217;t use types as lock objects. That is, <strong>avoid code such as <tt>lock(typeof(X))</tt> in C#</strong> or <tt>SyncLock(GetType(X))</tt> in Visual Basic, or the use of <a id="ctl00_MTCS_main_ctl17" href="http://msdn.microsoft.com/en-us/library/de0542zz%28VS.80%29.aspx">System.Threading.Monitor.Enter(System.Object)</a> with <a id="ctl00_MTCS_main_ctl18" href="http://msdn.microsoft.com/en-us/library/system.type%28VS.80%29.aspx">Type</a> objects. For a given type, there is only one instance of <strong>System.Type</strong> per application domain. If the type you take a lock on is public, code other than your own can take locks on it, leading to deadlocks. For additional issues, see <a id="ctl00_MTCS_main_ctl19" href="http://msdn.microsoft.com/en-us/library/ms228970%28VS.80%29.aspx">Reliability Best Practices</a>.</li>
<li>Use caution when locking on instances, for example <tt>lock(this)</tt> in C# or <tt>SyncLock(Me)</tt> in Visual Basic. If <strong>other code in your application, external to the type, takes a lock on the object, deadlocks</strong> could occur.</li>
<li>Do ensure that a thread that has entered a monitor always leaves that monitor, even if an <strong>exception </strong>occurs while the thread is in the monitor. The C# <a id="ctl00_MTCS_main_ctl20" href="http://msdn.microsoft.com/en-us/library/c5kehkcz%28VS.80%29.aspx">lock</a> statement and the Visual Basic <a id="ctl00_MTCS_main_ctl21" href="http://msdn.microsoft.com/en-us/library/3a86s51t%28VS.80%29.aspx">SyncLock</a> statement provide this behavior automatically, employing a <strong>finally</strong> block to ensure that <a id="ctl00_MTCS_main_ctl22" href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.exit%28VS.80%29.aspx">Monitor.Exit</a> is called. If you cannot ensure that <strong>Exit</strong> will be called, consider changing your design to use <strong>Mutex</strong>. A mutex is automatically released when the thread that currently owns it terminates.</li>
<li>Do use multiple threads for tasks that require different resources, and     <strong>avoid assigning multiple threads to a single resource</strong>. For example, any task involving I/O benefits from having its own thread, because that thread will block during I/O operations and thus allow other threads to execute. <strong>User input is another resource that benefits from a dedicated thread</strong>. On a single-processor computer, a task that involves intensive computation coexists with user input and with tasks that involve I/O, but <strong>multiple computation-intensive tasks contend with each other</strong>.</li>
<li>Consider using methods of the <a id="ctl00_MTCS_main_ctl23" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked%28VS.80%29.aspx">Interlocked</a> class for simple state changes, instead of using the <strong>lock</strong> statement (<strong>SyncLock</strong> in Visual Basic). The <strong>lock</strong> statement is a good general-purpose tool, but the <strong>Interlocked</strong> class provides better performance for updates that must be atomic. Internally, it executes a single lock prefix if there is no contention. In code reviews, watch for code like that shown in the following examples. In the first example, a state variable is incremented:</li>
</ul>
<p><strong>Performance Considerations with Locks, Threads, Semaphores</strong></p>
<dl>
<dt>You can improve performance by using the <a id="ctl00_MTCS_main_ctl26" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.increment%28VS.80%29.aspx">Increment</a> method instead of the <strong>lock</strong> statement, as follows: </dt>
<dt>
</dt>
<dt><span id="ctl00_MTCS_main_ctl28"></p>
<div id="ctl00_MTCS_main_ctl28_CSharp">
<div>
<div>C#</div>
</div>
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">System.Threading.Interlocked.Increment(myField);</pre>
</div>
</div>
<p></span></dt>
</dl>
<p>In the second example, a <strong>reference type variable is updated only if it is a null reference</strong> (<em><strong>Nothing </strong></em>in Visual Basic).</p>
<p><span id="ctl00_MTCS_main_ctl30"> </span>C#         <span id="ctl00_MTCS_main_ctl31"> </span></p>
<div id="ctl00_MTCS_main_ctl31_CSharp">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;"><span style="color:blue;">if</span> (x == <span style="color:blue;">null</span>)
{
    lock (lockObject)
    {
        <span style="color:blue;">if</span> (x == <span style="color:blue;">null</span>)
        {
            x = y;
        }
    }
}</pre>
</div>
</div>
<p>Performance can be improved by using the <a id="ctl00_MTCS_main_ctl32" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.compareexchange%28VS.80%29.aspx">CompareExchange</a> method instead, as follows:</p>
<p><span id="ctl00_MTCS_main_ctl33"> </span>C#         <span id="ctl00_MTCS_main_ctl34"> </span></p>
<div id="ctl00_MTCS_main_ctl34_CSharp">
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">System.Threading.Interlocked.CompareExchange(ref x, y, <span style="color:blue;">null</span>);</pre>
</div>
</div>
<p>The methods of this class help protect against errors that can occur when the scheduler switches contexts while a thread is updating a variable that can be accessed by other threads, or when two threads are executing concurrently on separate processors. The members of this class do not throw exceptions.The <a id="ctl00_MTCS_main_ctl32_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.increment%28VS.80%29.aspx">Increment</a> and <a id="ctl00_MTCS_main_ctl32_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.decrement%28VS.80%29.aspx">Decrement</a> methods increment or decrement a variable and store the resulting value in a single operation. On most computers, incrementing a variable is not an atomic operation, requiring the following steps:</p>
<ol>
<li>Load a value from an instance variable into a register.</li>
<li>Increment or decrement the value.</li>
<li>Store the value in the instance variable.</li>
</ol>
<p>If you do not use <strong>Increment</strong> and <strong>Decrement</strong>, a thread can be preempted after executing the first two steps. Another thread can then execute all three steps. When the first thread resumes execution, it overwrites the value in the instance variable, and the effect of the increment or decrement performed by the second thread is lost.</p>
<p>The <a id="ctl00_MTCS_main_ctl32_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.exchange%28VS.80%29.aspx">Exchange</a> method atomically exchanges the values of the specified variables. The <a id="ctl00_MTCS_main_ctl32_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.compareexchange%28VS.80%29.aspx">CompareExchange</a> method combines two operations: comparing two values and storing a third value in one of the variables, based on the outcome of the comparison. The compare and exchange operations are performed as an atomic operation.</p>
<p><span id="ctl00_MTCS_main_ctl33_ctl00_ctl01"> </span></p>
<pre style="white-space:pre-wrap;"><span style="color:green;">//A simple method that denies reentrancy.</span>
        <span style="color:blue;">static</span> <span style="color:blue;">bool</span> UseResource()
        {
            <span style="color:green;">//0 indicates that the method is not in use.</span>
            <span style="color:blue;">if</span>(0 == Interlocked.Exchange(ref usingResource, 1))
            {
                Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"{0} acquired the lock"</span></span>, Thread.CurrentThread.Name);

                <span style="color:green;">//Code to access a resource that is not thread safe would go here.</span>

                <span style="color:green;">//Simulate some work</span>
                Thread.Sleep(500);

                Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"{0} exiting lock"</span></span>, Thread.CurrentThread.Name);

                <span style="color:green;">//Release the lock</span>
                Interlocked.Exchange(ref usingResource, 0);
                <span style="color:blue;">return</span> <span style="color:blue;">true</span>;
            }
            <span style="color:blue;">else</span>
            {
                Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"   {0} was denied the lock"</span></span>, Thread.CurrentThread.Name);
                <span style="color:blue;">return</span> <span style="color:blue;">false</span>;
            }
        }
</pre>
<h2>Source Code Examples</h2>
<dl>
<dt>
<ul>
<li><a title="Part-1 - Creating a thread in Windows Forms" href="http://bigballofmud.wordpress.com/2009/04/01/2009/03/21/thread-marshalling-part-1-creating-a-thread-in-windows-forms/">Part 1 – Creating a thread in Windows Forms</a> -  Some basic ideas about firing a background and how to synchronize it with the UI.</li>
<li><a title="Part 2 - using BackgroundWorker" href="http://bigballofmud.wordpress.com/2009/04/01/2009/03/28/thread-marshalling-part-2-using-backgroundworker/" target="_self">Part 2 – Using BackgroundWorker</a> -  the .NET 2.0 way using a BackgroundWorker component<a title="Part 2 - using BackgroundWorker" href="http://bigballofmud.wordpress.com/2009/04/01/2009/03/28/thread-marshalling-part-2-using-backgroundworker/"></a></li>
</ul>
<ul>
<li><a title="Part 3 - Automatic Marshalling" href="http://bigballofmud.wordpress.com/2009/03/21/2009/04/01/thread-marshalling-part-3-automatic-marshalling/" target="_self">Part 3 – Automatic Marshalling</a> – creating your own component with auto marshalling</li>
</ul>
</dt>
<h2>When Not to Use Thread Pool Threads</h2>
<div style="display:block;"><a id="sectionToggle0"></a>There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:</p>
<ul>
<li>You require a foreground thread.</li>
<li>You require a thread to have a particular priority.</li>
<li>You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.</li>
<li>You need to place threads into a single-threaded apartment. All <a id="ctl00_MTCS_main_ctl64_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx">ThreadPool</a> threads are in the multithreaded apartment.</li>
<li>You need to have a stable identity associated with the thread, or to dedicate a thread to a task.</li>
</ul>
</div>
<dt><strong><a id="ctl00_MTCS_main_ctl01" href="http://msdn.microsoft.com/en-us/library/8xs8549b%28VS.80%29.aspx">BackgroundWorker Component Overview</a></strong></dt>
<dd>Describes the <strong>BackgroundWorker</strong> component, which gives you the ability to execute time-consuming operations asynchronously (&#8220;in the background&#8221;), on a thread different from your application&#8217;s main UI thread.
</dd>
</dl>
</div>
<p>There are many commonly performed operations that can take a long time to execute. For example:</p>
<ul>
<li>Image downloads</li>
<li>Web service invocations</li>
<li>File downloads and uploads (including for peer-to-peer applications)</li>
<li>Complex local computations</li>
<li>Database transactions</li>
<li>Local disk access, given its slow speed relative to memory access</li>
</ul>
<p><a id="ctl00_MTCS_main_ctl45_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject%28VS.80%29.aspx">System.MarshalByRefObject</a><br />
<a id="ctl00_MTCS_main_ctl45_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.component%28VS.80%29.aspx">System.ComponentModel.Component</a><br />
<strong>System.ComponentModel.BackgroundWorker</strong></p>
<dl>
<dt><strong><a id="ctl00_MTCS_main_ctl14" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28VS.80%29.aspx">BackgroundWorker</a> </strong></dt>
<dd>Describes this class and has links to all its members. </dd>
</dl>
<p>To execute a time-consuming operation in the background, create a <strong>BackgroundWorker</strong> and listen for events that report the progress of your operation and signal when your operation is finished. You can create the <strong>BackgroundWorker</strong> programmatically or you can drag it onto your form from the <strong>Components</strong> tab of the <strong>Toolbox</strong>. If you create the <strong>BackgroundWorker</strong> in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.</p>
<p>To set up for a background operation, add an event handler for the <a id="ctl00_MTCS_main_ctl43_ctl00_ctl00" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork%28VS.80%29.aspx">DoWork</a> event. Call your time-consuming operation in this event handler. To start the operation, call <a id="ctl00_MTCS_main_ctl43_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkerasync%28VS.80%29.aspx">RunWorkerAsync</a>. To receive notifications of progress updates, handle the <a id="ctl00_MTCS_main_ctl43_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged%28VS.80%29.aspx">ProgressChanged</a> event. To receive a notification when the operation is completed, handle the <a id="ctl00_MTCS_main_ctl43_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted%28VS.80%29.aspx">RunWorkerCompleted</a> event.</p>
<div>
<table border="0" width="100%">
<tbody>
<tr>
<th align="left"><img src="http://i.msdn.microsoft.com/4852et58.note%28en-US,VS.80%29.gif" alt="Note" />Note:</th>
</tr>
<tr>
<td>You must be careful not to manipulate any user-interface objects in your <strong>DoWork</strong> event handler. Instead, communicate to the user interface through the <strong>ProgressChanged</strong> and <strong>RunWorkerCompleted</strong> events.<strong>BackgroundWorker</strong> events are not marshaled across <a id="ctl00_MTCS_main_ctl43_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.appdomain%28VS.80%29.aspx">AppDomain</a> boundaries. Do not use a <strong>BackgroundWorker</strong> component to perform multithreaded operations in more than one <strong>AppDomain</strong>.</td>
</tr>
</tbody>
</table>
</div>
<p>If your background operation requires a parameter, call <strong>RunWorkerAsync</strong> with your parameter. Inside the <strong>DoWork</strong> event handler, you can extract the parameter from the <a id="ctl00_MTCS_main_ctl43_ctl00_ctl05" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.doworkeventargs.argument%28VS.80%29.aspx">DoWorkEventArgs.Argument</a> property.</p>
<div>System.Threading..::.<span style="text-decoration:underline;"><strong>ThreadPool</strong></span></div>
<div>Remarks</div>
<div style="display:block;"><a id="remarksToggle"></a></p>
<div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--><img src="http://i.msdn.microsoft.com/y5htx827.alert_note%28en-us,VS.90%29.gif" alt="Note" /><strong>Note:</strong></th>
</tr>
<tr>
<td>The <a id="ctl00_MTCS_main_ctl51_ctl00_ctl01" href="http://msdn.microsoft.com/en-us/library/system.security.permissions.hostprotectionattribute.aspx">HostProtectionAttribute</a> attribute applied to this type or member has the following <a id="ctl00_MTCS_main_ctl51_ctl00_ctl02" href="http://msdn.microsoft.com/en-us/library/system.security.permissions.hostprotectionattribute.resources.aspx">Resources</a> property value: <strong>Synchronization &#124; ExternalThreading</strong>. The <a id="ctl00_MTCS_main_ctl51_ctl00_ctl03" href="http://msdn.microsoft.com/en-us/library/system.security.permissions.hostprotectionattribute.aspx">HostProtectionAttribute</a> does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl04" href="http://msdn.microsoft.com/en-us/library/system.security.permissions.hostprotectionattribute.aspx">HostProtectionAttribute</a> class or <a id="ctl00_MTCS_main_ctl51_ctl00_ctl05" href="http://msdn.microsoft.com/en-us/library/ms172338.aspx">SQL Server Programming and Host Protection Attributes</a>.</td>
</tr>
</tbody>
</table>
</div>
<p>Many applications create threads that spend a great deal of time in the sleeping state, waiting for an event to occur. Other threads might enter a sleeping state only to be awakened periodically to poll for a change or update status information. Thread pooling enables you to use threads more efficiently by providing your application with a pool of worker threads that are managed by the system. One thread monitors the status of several wait operations queued to the thread pool. When a wait operation completes, a worker thread from the thread pool executes the corresponding callback function.</p>
<div>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--><img src="http://i.msdn.microsoft.com/y5htx827.alert_note%28en-us,VS.90%29.gif" alt="Note" /><strong>Note:</strong></th>
</tr>
<tr>
<td>The threads in <a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx"><strong>the managed thread pool</strong></a> are background threads. That is, their <a id="ctl00_MTCS_main_ctl51_ctl00_ctl07" href="http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx">IsBackground</a> properties are true. This means that a ThreadPool thread will not keep an application running after all foreground threads have exited.</td>
</tr>
</tbody>
</table>
</div>
<p>You can also queue work items that are not related to a wait operation to the thread pool. To request that a work item be handled by a thread in the thread pool, call the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl08" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx">QueueUserWorkItem</a> method. This method takes as a parameter a reference to the method or delegate that will be called by the thread selected from the thread pool. There is no way to cancel a work item after it has been queued.</p>
<p>Timer-queue timers and registered wait operations also use the thread pool. Their callback functions are queued to the thread pool.</p>
<blockquote><p>There is <strong>one thread pool per process</strong>. The thread pool has a default size of <strong>250 worker threads</strong> per available processor, and 1000 I/O completion threads. The number of threads in the thread pool can be changed by using the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl09" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads.aspx">SetMaxThreads</a> method. Each thread uses the default stack size and runs at the default priority.</p></blockquote>
<p>The thread pool maintains a minimum number of idle threads. For worker threads, the default value of this minimum is the number of processors. The <a id="ctl00_MTCS_main_ctl51_ctl00_ctl11" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.getminthreads.aspx">GetMinThreads</a> method obtains the minimum numbers of idle worker and I/O completion threads.</p>
<p>When all thread pool threads have been assigned to tasks, the thread pool does not immediately begin creating new idle threads. To avoid unnecessarily allocating stack space for threads, it creates new idle threads at intervals. The <strong>interval is currently half a second</strong>, although it could change in future versions of the .NET Framework.</p>
<p>If an application is subject to bursts of activity in which large numbers of thread pool tasks are queued, use the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl12" href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads.aspx">SetMinThreads</a> method to increase the minimum number of idle threads. Otherwise, the built-in delay in creating new idle threads could cause a bottleneck.</p>
<table border="0">
<tbody>
<tr>
<th><!--src=[../icons/alert_note.gif]--><img src="http://i.msdn.microsoft.com/y5htx827.alert_note%28en-us,VS.90%29.gif" alt="Note" /><strong>Note:</strong></th>
</tr>
<tr>
<td>Unnecessarily increasing the number of idle threads can also cause performance problems. Stack space must be allocated for each thread. If too many tasks start at the same time, all of them might appear to be slow. Finding the right balance is a <strong>performance-tuning issue</strong>.</td>
</tr>
</tbody>
</table>
<p>When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl14" href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx">ThreadStaticAttribute</a> attribute. Therefore, data that is placed in thread local storage by one method can be exposed to any other method that is executed by the same thread pool thread. A method that accesses a field that is marked with the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl15" href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx">ThreadStaticAttribute</a> attribute could encounter different data depending on which thread pool thread executes it.</p>
<p>Starting with the .NET Framework version 2.0 Service Pack 1, the throughput of the thread pool is significantly improved for applications that make heavy use of small thread pool tasks. These applications will see improvements in three areas:</p>
<ul>
<li>queuing tasks,</li>
<li>dispatching thread pool threads, and</li>
<li>dispatching I/O completion threads.</li>
</ul>
<p>To use this functionality, your application should target the .NET Framework version 3.5. For more information, see <a id="ctl00_MTCS_main_ctl51_ctl00_ctl16" href="http://msdn.microsoft.com/en-us/library/bb822049.aspx">.NET Framework 3.5 Architecture</a>.</p>
<h2>More Samples, .NET Snippets</h2>
<p>use the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.invoke.aspx" target="_blank">Invoke Method</a> of the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.aspx" target="_blank">ISynchronizeInvoke</a> Interface:</p>
<p><!--c1--></p>
<div>CODE</div>
<div>
#region UpdateStatus<br />
private static void UpdateStatus(string msg, string status)<br />
{<br />
//create our Object Array<br />
object[] items = new object[2];<br />
//populate our array with the parameters<br />
//passed to our method<br />
items[0] = msg;<br />
items[1] = status;<br />
//call the delegate<br />
_synch.Invoke(_status, items);<br />
}<br />
#endregion</div>
<p><!--ec2--></p>
<p>Using this we can update any UI component in the main thread without worrying if the call is being made from the proper thread. So as you can see, there is a thread-safe way to share data between threads. Though we aren&#8217;t using data, we&#8217;re simply updating a component on the UI with the status of our background thread. This process can now rune while leaving the UI in a usable state.</p></div>
<p><strong><a href="http://msdn.microsoft.com/en-us/magazine/cc188792.aspx">Basic Instincts: Implementing Callbacks with a Multicast Delegate</a>.</strong></p>
<p>is a follow-up to the <a href="http://msdn.microsoft.com/msdnmag/issues/02/12/basicinstincts/default.aspx">December 2002</a> installment in which I introduced the basic concepts and programming techniques associated with delegates. I am going to assume you have already read that column and that you are familiar with the fundamentals of programming delegates. For example, you should know how to define a delegate type, how to create a delegate object that&#8217;s bound to a handler method, and how to execute the handler method by calling the delegate object&#8217;s Invoke method.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms173175(VS.80).aspx">How to: Combine Delegates (Multicast Delegates)(C#)</a>.</p>
<p>This example demonstrates how to compose multicast delegates. A useful property of <a id="ctl00_MTCS_main_ctl02" href="http://msdn.microsoft.com/en-us/library/900fyy8e%28VS.80%29.aspx">delegate</a> objects is that they can be assigned to one delegate instance to be multicast using the <strong>+</strong> operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed.</p>
<p>The <strong>-</strong> operator can be used to remove a component delegate from a composed delegate.</p>
<h1>Example</h1>
<div id="codeExampleSection"><span id="ctl00_MTCS_main_ctl03"></p>
<div id="ctl00_MTCS_main_ctl03_CSharp">
<div>
<div>C#</div>
</div>
<div style="background-color:#dddddd;" dir="ltr">
<pre style="white-space:pre-wrap;">delegate <span style="color:blue;">void</span> Del(<span style="color:blue;">string</span> s);

<span style="color:blue;">class</span> TestClass
{
    <span style="color:blue;">static</span> <span style="color:blue;">void</span> Hello(<span style="color:blue;">string</span> s)
    {
        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"  Hello, {0}!"</span></span>, s);
    }

    <span style="color:blue;">static</span> <span style="color:blue;">void</span> Goodbye(<span style="color:blue;">string</span> s)
    {
        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"  Goodbye, {0}!"</span></span>, s);
    }

    <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main()
    {
        Del a, b, c, d;

        <span style="color:green;">// Create the delegate object a that references </span>
        <span style="color:green;">// the method Hello:</span>
        a = Hello;

        <span style="color:green;">// Create the delegate object b that references </span>
        <span style="color:green;">// the method Goodbye:</span>
        b = Goodbye;

        <span style="color:green;">// The two delegates, a and b, are composed to form c: </span>
        c = a + b;

        <span style="color:green;">// Remove a from the composed delegate, leaving d, </span>
        <span style="color:green;">// which calls only the method Goodbye:</span>
        d = c - a;

        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"Invoking delegate a:"</span></span>);
        a(<span style="color:maroon;"><span style="color:maroon;">"A"</span></span>);
        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"Invoking delegate b:"</span></span>);
        b(<span style="color:maroon;"><span style="color:maroon;">"B"</span></span>);
        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"Invoking delegate c:"</span></span>);
        c(<span style="color:maroon;"><span style="color:maroon;">"C"</span></span>);
        System.Console.WriteLine(<span style="color:maroon;"><span style="color:maroon;">"Invoking delegate d:"</span></span>);
        d(<span style="color:maroon;"><span style="color:maroon;">"D"</span></span>);
    }
}</pre>
</div>
</div>
<p></span></p>
<h4>Output</h4>
<pre>Invoking delegate a:
  Hello, A!
Invoking delegate b:
  Goodbye, B!
Invoking delegate c:
  Hello, C!
  Goodbye, C!
Invoking delegate d:
  Goodbye, D!</pre>
</div>
<h1><span id="seeAlsoNoToggle">See Also</span></h1>
<h4>Reference</h4>
<p><a id="ctl00_MTCS_main_ctl04" href="http://msdn.microsoft.com/en-us/library/system.multicastdelegate%28VS.80%29.aspx">MulticastDelegate</a></p>
<h4>Concepts</h4>
<p><a id="ctl00_MTCS_main_ctl05" href="http://msdn.microsoft.com/en-us/library/67ef8sbd%28VS.80%29.aspx">C# Programming Guide</a><br />
<a id="ctl00_MTCS_main_ctl06" href="http://msdn.microsoft.com/en-us/library/awbftdfh%28VS.80%29.aspx">Events (C# Programming Guide)</a></p>
<h2><a href="http://msdn.microsoft.com/en-us/magazine/cc188792.aspx"><span id="ctl00_WikiContent_ctl00_HeaderTitle">Quick factories using delegates</span></a></h2>
<p>Delegates are very useful to act as object factories. In the example below, it is used to allow the function to build another object based on it, while keeping the function unaware about types.</p>
<pre>namespace Microsoft.MSDN.Wiki.Samples
{
    class Vehicle
    {
        int Price;
    }
    class Car : Vehicle
    {
        protected Car(string model, int passengers)
        {
        }
        public static Car Create(string model, int passengers)
        {
            // Perform some check and create a new instance
            return new Car(model, passengers);
        }
        public Vehicle FakeClone(Vehicle src)
        {
            // Cars are special in our example, and will not be cloned
            // return the original object instead
            return src;
        }
    }
    class Bus : Vehicle
    {
        private string model;
        private int passengers;
        private int cargo;
        protected Bus(string model, int passengers, int cargo)
        {
            // Assign variables
            this.model = model;
            this.passengers = passengers;
            this.cargo = cargo;
        }
        public static Bus Create(string model, int passengers, int cargo)
        {
            // TODO: Perform some check and create a new instance
            return new Bus(model, passengers, cargo);
        }
        public Vehicle CloneSpecial(Vehicle src)
        {
            // TODO: Perform some check and clone the instance
            Bus bus = (Bus)src;
            return new Bus(bus.model, bus.passengers, bus.cargo);
        }
    }
    class Program
    {
        <strong>public delegate Vehicle CreateNewVehicle(Vehicle src);</strong>
        public void ProcessBus(Bus bus)
        {
            ProcessVehicle(bus, <strong>bus.CloneSpecial</strong>);
        }
        public void ProcessCar(Car car)
        {
            ProcessVehicle(car, <strong>car.FakeClone</strong>);
        }
        public void ProcessVehicle(Vehicle source, <strong>CreateNewVehicle factory</strong>)
        {
            // This function might need to clone the vehicle. Use the delegate
            // so the caller can control the creation
            bool needToCloneObject = true;
            if (needToCloneObject)
            {
                Vehicle newObj = factory(source);
                if (newObj != null)
                {
                    // Now we have a new object to work with
                }
            }
        }
        [STAThread]
        public static void Main(string[] args)
        {
        }
    }
}
</pre>
<p>As you can see we can take the knowledge about the creation logic outside the function when pure inheritance does not work (e.g. having a generic Clone() function in Vehicle) or when the logic changing depending on the context. For example, you might need to clone in a specific way on one condition and clone it differently on another condition.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/awbftdfh(VS.80).aspx"><br />
</a></p>
<h2><span id="ctl00_WikiContent_ctl01_HeaderTitle">Anonymous delegates can be used to simplify context changes</span></h2>
<p>you can also use an anonymous delegate to simplify the cloning process. But remember that this technique is not constrained to cloning. That was just the motivation for the example.</p>
<pre>namespace Microsoft.MSDN.Wiki.Samples
{
    class Vehicle
    {
        int Price;
    }
    class Car : Vehicle
    {
        protected Car(string model, int passengers)
        {
        }
        public static Car Create(string model, int passengers)
        {
            // Perform some check and create a new instance
            return new Car(model, passengers);
        }
    }
    class Bus : Vehicle
    {
        private string model;
        private int passengers;
        private int cargo;
        protected Bus(string model, int passengers, int cargo)
        {
            // Assign variables
            this.model = model;
            this.passengers = passengers;
            this.cargo = cargo;
        }
        public static Bus Create(string model, int passengers, int cargo)
        {
            // TODO: Perform some check and create a new instance
            return new Bus(model, passengers, cargo);
        }
        public Bus Clone()
        {
            return new Bus(this.model, this.passengers, this.cargo);
        }
    }
    class Program
    {
        public delegate Vehicle CreateNewVehicle(Vehicle src);
        public void ProcessBus(Bus bus)
        {
            ProcessVehicle(bus, <strong>delegate(Vehicle src)
            {
                // Perform some special logic
                return ((Bus)src).Clone();
            }</strong>);
        }
        public void ProcessCar(Car car)
        {
            ProcessVehicle(car, <strong>delegate(Vehicle src)
            {
                // Cars are not cloned in this situation
                return src;
            }</strong>);
        }
        public void ProcessVehicle(Vehicle source, CreateNewVehicle factory)
        {
            // This function might need to clone the vehicle. Use the delegate
            // so the caller can control the creation
            bool needToCloneObject = true;
            if (needToCloneObject)
            {
                Vehicle newObj = factory(source);
                if (newObj != null)
                {
                    // Now we have a new object to work with
                }
            }
        }
        [STAThread]
        public static void Main(string[] args)
        {
        }
    }
}</pre>
</div>
</div>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Threading - part 2]]></title>
<link>http://androidgamez.wordpress.com/2009/09/16/threading-part-2/</link>
<pubDate>Wed, 16 Sep 2009 18:21:45 +0000</pubDate>
<dc:creator>androidgamez</dc:creator>
<guid>http://androidgamez.wordpress.com/2009/09/16/threading-part-2/</guid>
<description><![CDATA[Please see my previous post Mutual Exclusion / Critical Section aka Mutex &#8220;Mutual exclusion]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Please see my <a href="http://wp.me/pB72j-5">previous post </a></p>
<h3>Mutual Exclusion / Critical Section aka Mutex</h3>
<ul>
<li>&#8220;Mutual exclusion&#8221; &#8212; scheduling threads so that only one thread at a time executes a critical section of code. Essentially, keeping the threads from interfering with each other. Avoid reader/writer and writer/writer conflicts on shared memory between threads.</li>
<li>Critical section &#8212; a section of code that manipulates shared memory, and so must not be run by multiple threads at the same time.</li>
<li>&#8220;Race condition&#8221; &#8212; a piece of code with unaddressed concurrency problems, and so depending on how the threads happen to schedule each time, you may get different output.</li>
</ul>
<h4>Coordination</h4>
<ul>
<li>&#8220;Coordination&#8221; &#8212; managing the schedules of multiple threads so they can start/pause/etc. to mesh their schedules.</li>
<li>Typically this centers on handing information from one thread to another, or signaling one thread that another thread has finished doing something.</li>
<li>We will use join() and Semaphores for this</li>
</ul>
<p>Example:</p>
<pre>class Pair {
   private int a, b;
   public Pair()
   {
      a = 0;
      b = 0;
   }
   // Returns the sum of a and b. (reader)
   public int sum()
   {
      return(a+b);
   }
   // Increments both a and b. (writer)
   public void inc()
   {
      a++;
      b++;
   }
}</pre>
<h5>Reader/Writer conflict:</h5>
<ul>
<li>thread1 runs inc() while thread2 runs sum() on the same object.</li>
<li>In that case, the sum() thread could get an incorrect value if the inc() is halfway done.</li>
<li>In part, this happens because the lines of inc() and sum() interleave</li>
<li><strong>N</strong><strong>ote</strong> that this is only a problem if the threads are executing against the same object so they both touch the same memory.</li>
</ul>
<h5>Writer/Writer conflict:</h5>
<ul>
<li>thread1 runs inc() while thread2 runs inc() on the same object.</li>
<li>The two inc()&#8217;s can interleave to mess up the object state</li>
</ul>
<h3>Object Lock + Synchronized Method</h3>
<ul>
<li>In Java, every object has a &#8220;lock&#8221;</li>
<li>A &#8220;synchronized&#8221; method respects the lock of the receiver object.</li>
<li>For a thread to execute a synchronized method against a receiver, it first obtains the lock of the receiver. The lock is released when the method exits</li>
<li>If the lock is already held by another thread, the calling thread blocks (efficiently) waiting for the other thread to exit and so make the lock available.</li>
<li>In this way, multiple threads, in effect, take turns on who can execute against the receiver &#8212; mutual exclusion.</li>
<li>If a method is not synchronized, it will ignore the lock and just go ahead.</li>
</ul>
<p>Example:</p>
<p>In our example above, we solve the race conditions by declaring the critical section as <strong>synchronized</strong> &#8212; they respect the lock on the receiver object.</p>
<pre>public synchronized int sum()
{
   return(a+b);
}
// Increments both a and b. (writer)
public synchronized void inc()
{
   a++;
   b++;
}</pre>
<h5>Synchronized(obj) {&#8230;} Block</h5>
<ul>
<li>A variant of the synchronized method.</li>
<li>Acquire/Release lock for a specific object.</li>
<li>Allows us to specify exactly where to lock, and with what object</li>
<li>A little slower, a little less readable</li>
</ul>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[File Semaphore / Mutex]]></title>
<link>http://sanchit6.wordpress.com/2009/08/19/file-semaphore-mutex/</link>
<pubDate>Wed, 19 Aug 2009 14:16:52 +0000</pubDate>
<dc:creator>sanchit6</dc:creator>
<guid>http://sanchit6.wordpress.com/2009/08/19/file-semaphore-mutex/</guid>
<description><![CDATA[Below is an implementation of a File based mutex in Java import java.io.File; import java.io.FileWri]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Below is an implementation of a File based mutex in Java</p>
<pre>
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * A file based mutual exclusion lock that allows to acquire and release the underlying
 * resource. On acquiring the lock the file is created with the given contents. The
 * lock is free on construction.
 *
 * The lock is also associated with a maximum unused period. If the lock is not
 * updated during the given period, any other thread waiting to acquire the lock
 * will be given the lock.
 *
 * The lock is said to be acquired only when the file exists and file last used
 * timestamp is within unusedMsec. permitted
 *
 * The read and write operation to the mutex file are synchronized across
 * different process by encapsulating that with a create &#38; lock operation
 * on a separate file. So before a process can acquire/read mutex (update mutex
 * content &#38; timestamp) , it needs to create &#38; lock another predefined
 * file and also delete the lock file after the operation on mutex are done. This is
 * to ensure that multiple processes/threads dont acquire the mutex at the same time
 */
public class FileMutex {
    /** The lock file name **/
    private String mutex = System.getProperty("java.io.tmpdir") + "app.lck";

    /** Msecs. file can be left unused before another thread acquires the lock File **/
    private long unusedMSec;

    /** file contents telling which component/application is holding the lock **/
    private String mutexContent;

    /** FileName of the lock for mutex **/
    private String lockOnMutex = mutex + ".lck";

    public static final long ONE_SECOND = 1000;
    public static final long ONE_MINUTE = 60 * ONE_SECOND;

    /**
     * @param lockFileContent
     * @param unusedMSec
     */
    public FileMutex(String lockFileContent, long unusedMSec) {
        this.mutexContent = lockFileContent;
        this.unusedMSec = unusedMSec;
    }

    public void acquire() throws InterruptedException {
        try {
            while (isInuseMutex())
                Thread.sleep(ONE_SECOND);
            createMutex();
        } catch (Exception e) {
            throw new InterruptedException("Error acquiring lock: " + e.getMessage());
        }
    }

    public synchronized void release() {
        releaseMutex();
    }

    public boolean tryAcquire() throws InterruptedException {
        try {
            if (!isInuseMutex()) {
                boolean created = createMutex();
                return created;
            }
        } catch (Exception e) {
            throw new InterruptedException("Error acquiring lock: " + e.getMessage());
        }
        return false;
    }

    public boolean update() {
        createMutex();
        return true;
    }

    /**
     * @return
     */
    private boolean isInuseMutex() {
        File lockOnMutexFile = new File(lockOnMutex);
        FileChannel channel = null;
        FileLock lock = null;
        try {
            channel = new RandomAccessFile(lockOnMutexFile, "rw").getChannel();
            lockOnMutexFile.createNewFile();
            try {
                lock = channel.tryLock();
            } catch (OverlappingFileLockException e) {
                // File is already locked in this thread or virtual machine
                return true;
            }

            File mutexFile = new File(mutex);
            if (!mutexFile.exists()) {
                return false;
            }

            Date lockLastUsedDate = new Date(mutexFile.lastModified());
            Date oldestAllowedLockUsedDate =
                         new Date(System.currentTimeMillis() - unusedMSec);
            if (lockLastUsedDate.before(oldestAllowedLockUsedDate)) {
                return false;
            }
        } catch (Exception e) {
            System.out.println("Error acquiring lock: " + e.getMessage());
        } finally {
            try {
                if (lock != null)
                    lock.release();
            } catch (IOException e) {
            }
            try {
                channel.close();
            } catch (IOException e) {
            }
            lockOnMutexFile.delete();
        }
        return true;
    }

    /**
     * Creates the lock file with the given contents.
     *
     */
    private boolean createMutex() {
        File lockOnMutexFile = new File(lockOnMutex);
        FileChannel channel = null;
        FileLock lock = null;
        try {
            channel = new RandomAccessFile(lockOnMutexFile, "rw").getChannel();
            lockOnMutexFile.createNewFile();
            try {
                lock = channel.tryLock();
            } catch (OverlappingFileLockException e) {
                // File is already locked in this thread or virtual machine
                return false;
            }

            File mutexFile = new File(mutex);
            mutexFile.createNewFile();
            FileWriter writer = new FileWriter(mutexFile);
            writer.append(mutexContent);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            System.out.println("Error acquiring lock: " + e.getMessage());
        } finally {
            try {
                if (lock != null)
                    lock.release();
            } catch (IOException e) {
            }
            try {
                channel.close();
            } catch (IOException e) {
            }
            lockOnMutexFile.delete();
        }
        return true;
    }

    /**
     * Release the mutex .. that is Delete the underlying lock file, if it exists
     */
    private void releaseMutex() {
        File mutexFile = new File(mutex);
        mutexFile.delete();
    }
}
</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Differnce betweeen Binary Semaphore and Mutex]]></title>
<link>http://susenj.wordpress.com/2009/07/29/differnce-betweeen-binary-semaphore-and-mutex/</link>
<pubDate>Wed, 29 Jul 2009 17:18:27 +0000</pubDate>
<dc:creator>maverick</dc:creator>
<guid>http://susenj.wordpress.com/2009/07/29/differnce-betweeen-binary-semaphore-and-mutex/</guid>
<description><![CDATA[First of all&#8230;you can see an example here which may clarify your concept! Their synchronization]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>First of all&#8230;you can see an <a href="http://geekswithblogs.net/shahed/archive/2006/06/09/81268.aspx">example here </a>which may clarify your concept!</p>
<p>Their synchronization semantics are very different:</p>
<blockquote><p><em>* mutexes allows serialization of access to a given resource i.e. multiple threads wait for a lock, one at a time, the thread owns the lock until it is done, most importantly only this particular thread can unlocks it.</em></p>
<p><em>* a binary semaphore is a counter with value 0 and 1, a task blocking on it until any task does a <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/sem_post.html">sem_post</a>. The semaphore advertise that a resource is available, and it provides the mechanism to wait until it is signaled as being available.</em></p></blockquote>
<p>As such one can see a mutex as a token passed from task to tasks and a semaphore as traffic red-light (it signals someone that it can proceed).<br />
There is a lot of confusion around these two concepts. They permit the same kind of feature i.e. synchronization but as I explained with different semantics.</p>
<p>Semaphores are usually more expensive to use than mutex, but they provide you sometimes more reliability. For example, let’s say you have two threads, thread A and thread B trying to serialize access to resource R.</p>
<p>The mutex way would be;</p>
<blockquote><p><em>step 00 – create a lock object<br />
step 01 – A request the lock<br />
step 02 – B request the lock<br />
step 03 – A grab the lock<br />
step 04 – A uses R<br />
step 05 – A release the lock<br />
step 06 – B grab the lock<br />
step 07 – B uses R<br />
step 08 – A request the lock<br />
step 09 – B release the lock<br />
step 10 – A grab the lock</em></p></blockquote>
<p>Q: What happens if thread B returns between step 7 and step 9 ?<br />
A: step 10 may never occurs …</p>
<p>In this scenario there is no way to recover, the mutex is an opaque object and you are stuck in a deadlock situation.</p>
<p>With semaphores things are a little bit different, you can make step 10 happen even if your thread leaves early.</p>
<p>Since people are confused by semaphores or misuse them more frequently, here are a few examples on how to use them.</p>
<p><strong>Serializing access using a semaphore</strong></p>
<p>Note that when using a binary semaphore to serialize access to a resource, you need to initialize it with the value 1, meaning resource available.</p>
<p>Requesting the lock is done by calling sem_wait() on it, and releasing the lock is done by calling sem_post():</p>
<blockquote>
<blockquote><p><em>// thirst get a semaphore and initialize it with the value 1<br />
sem_init( … )<br />
…</em></p>
<p><em>// lock the resource<br />
sem_wait( …)</em></p>
<p><em>… do some stuff …</em></p>
<p><em>// release the resource<br />
sem_post( … )</em></p></blockquote>
</blockquote>
<p><strong>The consumer / producer model</strong></p>
<p>if you initialize the semaphore withe the value 0 and call sem_wait on it, the semantic is different, you are waiting for some one to signal you that there is some job to do.<br />
In this scenario, the semaphore is not used to serialize access to a particular resource, it is used as signaling mechanism.<br />
A consumer is waiting for a consumer to signal him that some data is available or that there is work to do.</p>
<p>very simple and effective, that’s why semaphore are used so frequently for producer/consumer work flows.</p>
<blockquote>
<blockquote><p><em>// thirst get a semaphore and initialize it with the value 0<br />
sem_init( … )<br />
…</em></p>
<p><em>while( true )<br />
{<br />
// wait for some job to do<br />
sem_wait( …)</em></p>
<p><em>… work hard on it …<br />
}</em></p>
<p><em>// yep there is no sem_post here: the producer, that gives this task<br />
// work to do is supposed to be the one calling sem_post()</em></p></blockquote>
</blockquote>
<p>check your platform semaphore implementation, sem_post(), sem_wait() and sem_init() are the API to use <a title="POSIX semaphores" href="http://www.opengroup.org/onlinepubs/000095399/basedefs/semaphore.h.html" target="_blank">POSIX semaphores</a>.</p>
<p>Your platform API may be different, but the concepts are the same.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Locking mechanisms]]></title>
<link>http://adilevin.wordpress.com/2009/06/04/locking-mechanisms/</link>
<pubDate>Thu, 04 Jun 2009 19:10:34 +0000</pubDate>
<dc:creator>adilevin</dc:creator>
<guid>http://adilevin.wordpress.com/2009/06/04/locking-mechanisms/</guid>
<description><![CDATA[This post discusses four locking mechanisms that are essential for multithreaded programming: Mutex.]]></description>
<content:encoded><![CDATA[This post discusses four locking mechanisms that are essential for multithreaded programming: Mutex.]]></content:encoded>
</item>
<item>
<title><![CDATA[Exercise 17: Concurrency Terms]]></title>
<link>http://itc594af.wordpress.com/2009/05/12/exercise-17-concurrency-terms/</link>
<pubDate>Tue, 12 May 2009 10:47:25 +0000</pubDate>
<dc:creator>itc594af</dc:creator>
<guid>http://itc594af.wordpress.com/2009/05/12/exercise-17-concurrency-terms/</guid>
<description><![CDATA[Find definitions for eight terms and concepts used in threaded programming: 1. Thread Synchronisatio]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>Find definitions for eight terms and concepts used in threaded programming:</strong></p>
<p><strong>1. Thread Synchronisation</strong></p>
<p>Thread synchronisation is also referred to as process synchronisation. &#8220;Process synchronization refers to the coordination of simultaneous threads or processes to complete a task in order to get correct runtime order and avoid unexpected race conditions&#8221; (Wikipedia &#8211; Synchronization, n.d.).<a title="Race condition" href="http://en.wikipedia.org/wiki/Race_condition"><br />
</a></p>
<p><strong>2. Locks</strong></p>
<p>&#8220;a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many <span class="mw-redirect">threads of execution</span>. Locks are one way of enforcing concurrency control policies&#8221; (Wikipedia &#8211; Lock, n.d.).</p>
<p><strong>3. Deadlocks</strong></p>
<p>The best definition I could find for a deadlock is an example when programming in Java. &#8220;Deadlocks can occur in Java because the <code>synchronized</code> keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Since the thread might already hold locks associated with other objects, two threads could each be waiting for the other to release a lock; in such a case, they will end up waiting forever&#8221; (Goetz, 2001).</p>
<p><strong>4. Semaphores</strong></p>
<p>&#8220;a semaphore is a protected variable or abstract data type which constitutes the classic method for restricting access to shared resources such as shared memory in a <span class="mw-redirect">multiprogramming</span> environment. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by <span class="mw-redirect">Edsger Dijkstra</span>. Semaphores are the classic solution to preventing <span class="mw-redirect">race conditions</span> in the dining philosophers problem, although they do not prevent resource deadlocks&#8221; (Wikipedia &#8211; Semaphore, n.d.).</p>
<p><strong>5. Mutex (mutual exclusion)</strong></p>
<p>&#8220;Mutual exclusion (often abbreviated to mutex) algorithms are used in <span class="mw-redirect">concurrent programming</span> to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.</p>
<p>Examples of such resources are fine-grained flags, counters or queues, used to communicate between code that runs concurrently, such as an application and its interrupt handlers. The problem is acute because a thread can be stopped or started at any time.</p>
<p>To illustrate: suppose a section of code is altering a piece of data over several program steps, when another thread, perhaps triggered by some unpredictable event, starts executing. If this second thread reads from the same piece of data, the data, which is in the process of being overwritten, is in an inconsistent and unpredictable state. If the second thread tries overwriting that data, the ensuing state will probably be unrecoverable. These shared data being accessed by critical sections of code, must therefore be protected, so that other processes which read from or write to the chunk of data are excluded from running.</p>
<p>A mutex is also a common name for a program object that negotiates mutual exclusion among threads, also called a lock&#8221; (Wikipedia &#8211; Mutual exclusion, n.d.).</p>
<p><strong>6. Thread</strong></p>
<p>&#8220;a thread of execution results from a fork of a computer program into two or more concurrentlytasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources&#8221; (Wikipedia &#8211; Thread, n.d.).</p>
<p><strong>7. Event</strong></p>
<p>An event is a sensor output, user action, or a message from other programs or threads (Wikipedia &#8211; Event-driven programming, n.d.).</p>
<p><strong>8. Waitable timer.</strong></p>
<p>Waitable timers were introduced with Windows 98 and Windows NT 4.0. and they were designed to work with threads that need to block for some times. These timers are kernel objects which are signaled in the specified time, or in regular time intervals. They can specify the callback function (actually, an asynchronous procedure call, or APC) which gets called when timer gets signaled. This callback function is usually called a <em>completion routine</em>.</p>
<p>In order to enable execution of the completion routine, the thread must be in alertable state (executing <code>SleepEx()</code>, <code>WaitForSingleObjectEx()</code> , <code>WaitForMultipleObjectsEx()</code>, <code>MsgWaitForMultipleObjectsEx()</code> , <code>SignalObjectAndWait()</code> functions). In practice, this means that our main thread will be blocked while we are using waitable timers.</p>
<p>To start working with a waitable timer, we must open an existing timer, or create the new one (Trifunovic, 2001).</p>
<p><strong>References</strong></p>
<p>Wikipedia &#8211; Synchronization (n.d.). Retrieved 11th May 2009 from http://en.wikipedia.org/wiki/Synchronization_(computer_science)</p>
<p>Wikipedia &#8211; Lock (n.d.). Retrieved 11th May 2009 from http://en.wikipedia.org/wiki/Lock_(computer_science)</p>
<p>Goetz, B. (2001) Avoid Synchronization Deadlocks. Retrieved 11th May 2009 from http://www.javaworld.com/javaworld/jw-10-2001/jw-1012-deadlock.html</p>
<p>Wikipedia &#8211; Semaphore (n.d.). Retrieved 11th May 2009 from http://en.wikipedia.org/wiki/Semaphore_(programming)</p>
<p>Wikipedia &#8211; Mutual exclusion (n.d.). Retrieved 11th May 2009 from http://en.wikipedia.org/wiki/Mutex</p>
<p>Wikipedia &#8211; Thread (n.d.). Retrieved 11th May 2009 from <a href="http://en.wikipedia.org/wiki/Thread_join">http://en.wikipedia.org/wiki/Thread_join</a></p>
<p>Wikipedia &#8211; Event-driven programming (n.d.). Retrieved 12th May 2009 from <a href="http://en.wikipedia.org/wiki/Event-driven_programming">http://en.wikipedia.org/wiki/Event-driven_programming</a></p>
<p>Trifunovic (2001). CodeProject: Timers Tutorial. Retrieved 12th May 2009 from <a href="http://www.codeproject.com/KB/system/timers_intro.aspx#WaitableTimers">http://www.codeproject.com/KB/system/timers_intro.aspx#WaitableTimers</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Escrita/leitura com mutex e semáforo]]></title>
<link>http://braziliangecko.wordpress.com/2009/05/06/escritaleitura-com-mutex-e-semaforo/</link>
<pubDate>Wed, 06 May 2009 19:57:09 +0000</pubDate>
<dc:creator>thiagobrandam</dc:creator>
<guid>http://braziliangecko.wordpress.com/2009/05/06/escritaleitura-com-mutex-e-semaforo/</guid>
<description><![CDATA[Problemas de acessores que lêem e escrevem em um dado recurso são bastante comuns no dia-dia, se con]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Problemas de acessores que lêem e escrevem em um dado recurso são bastante comuns no dia-dia, se considerarmos a importância de bancos de dados na vida moderna. O programa abaixo implementa um algoritmo para sincronizar operações de leitura e escrita de maneira segura. Esse algoritmo, entretanto, privilegia os acessores leitores, que passam a dominar o recurso assim que um esteja utilizando.</p>
<pre class="brush: cpp;">
#include &#60;stdio.h&#62;
#include &#60;stdlib.h&#62;
#include &#60;unistd.h&#62;
#include
&#60;pthread.h&#62;
#include &#60;semaphore.h&#62;

#define ESCRITORES 1
#define LEITORES 5
#define VEL_CHEGADA 2

pthread_mutex_t mutex_variavel_ql;
sem_t banco;
int quer_ler = 0;

void ler_banco_dados()
{

	printf(&#34;Lendo banco de dados...n&#34;);
	sleep(rand()%5 + 1);
}

void processar_dados()
{
	printf(&#34;Processando dados...n&#34;);
	sleep(rand()%5 + 1);

}

void produzir_dados()
{
	printf(&#34;Produzindo dados...n&#34;);
	sleep(rand()%5 + 1);
}

void escrever_no_banco()
{
	printf(&#34;Escrevendo no banco...n&#34;);
	sleep(rand()%5 + 1);
}

void* leitor(void *args)
{
		sleep(2);
		pthread_mutex_lock(&#38;mutex_variavel_ql);
		quer_ler++;
		printf(&#34;%d threads querem ler do banco.n&#34;, quer_ler);
		if(quer_ler == 1)
			sem_wait(&#38;banco);
		pthread_mutex_unlock(&#38;mutex_variavel_ql);
		ler_banco_dados();
		pthread_mutex_lock(&#38;mutex_variavel_ql);
		quer_ler--;
		if(quer_ler == 0)
			sem_post(&#38;banco);
		pthread_mutex_unlock(&#38;mutex_variavel_ql);
		processar_dados();
		pthread_exit(NULL);
}

void* escritor(void *args)
{
		sleep(2);
		produzir_dados();
		sem_wait(&#38;banco);
		escrever_no_banco();
		sem_post(&#38;banco);

} 

int main()
{

	int i,res;
	pthread_t leitor_thread[LEITORES];
	pthread_t escritor_thread[ESCRITORES];
	srand(time(NULL));

	res = pthread_mutex_init(&#38;mutex_variavel_ql,NULL);
	if(res != 0)
	{
		perror(&#34;Não foi possível criar mutex!&#34;);
		exit(EXIT_FAILURE);
	}

	res = sem_init(&#38;banco,0,1);
	if(res != 0)
	{
		perror(&#34;Não foi possível criar semáforo!&#34;);
		exit(EXIT_FAILURE);
	}

	for(i = 0; i &#60; ESCRITORES; i++)
	{
		res = pthread_create(&#38;escritor_thread[i],NULL,escritor,NULL);
		if(res != 0)
		{
			perror(&#34;Não foi possível criar thread!&#34;);
			exit(EXIT_FAILURE);
		}
	}

	for(i = 0; i &#60; LEITORES; i++)
	{
		res = pthread_create(&#38;leitor_thread[i],NULL,leitor,NULL);
		if(res != 0)
		{
			perror(&#34;Não foi possível criar thread!&#34;);
			exit(EXIT_FAILURE);
		}
	}

	for(i = 0; i &#60; ESCRITORES; i++)
	{
		pthread_join(escritor_thread[i],NULL);
		if(res != 0)
		{
			perror(&#34;Thread join failed&#34;);
			exit(EXIT_FAILURE);
		}
	}

	for(i = 0; i &#60; LEITORES; i++)
	{
		pthread_join(leitor_thread[i],NULL);
		if(res != 0)
		{
			perror(&#34;Thread join failed&#34;);
			exit(EXIT_FAILURE);
		}
	}

	sem_destroy(&#38;banco);
	pthread_mutex_destroy(&#38;mutex_variavel_ql);

	exit(EXIT_SUCCESS);
}
</pre>
<p>Resultado do programa:</p>
<p><img class="alignnone size-full wp-image-76" title="Resultado do programa" src="http://braziliangecko.wordpress.com/files/2009/05/snapshot_teste61.png" alt="Resultado do programa" width="285" height="257" /></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Library cache latch has gone?]]></title>
<link>http://dioncho.wordpress.com/2009/03/26/library-cache-latch-has-gone/</link>
<pubDate>Thu, 26 Mar 2009 04:21:23 +0000</pubDate>
<dc:creator>Dion Cho</dc:creator>
<guid>http://dioncho.wordpress.com/2009/03/26/library-cache-latch-has-gone/</guid>
<description><![CDATA[As Tanel Poder declared with big proud, all the library cache related latches have gone in Oracle 11]]></description>
<content:encoded><![CDATA[As Tanel Poder declared with big proud, all the library cache related latches have gone in Oracle 11]]></content:encoded>
</item>
<item>
<title><![CDATA[Preventing multiple instance of an Application]]></title>
<link>http://yenkay.wordpress.com/2009/02/04/preventing-multiple-instance-of-an-application/</link>
<pubDate>Wed, 04 Feb 2009 15:50:35 +0000</pubDate>
<dc:creator>yenkay</dc:creator>
<guid>http://yenkay.wordpress.com/2009/02/04/preventing-multiple-instance-of-an-application/</guid>
<description><![CDATA[Hi, .Net framework&#8217;s System.Threading namespace provides Mutex class which is a synchronizatio]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Hi,</p>
<p>.Net framework&#8217;s System.Threading namespace provides Mutex class which is a synchronization primitive that can also be used for interprocess synchronization. Using this mutex object we can also control running of multiple instances of a same application. Here is the sample code to do that. The code should be wriiten on the starting point of the application obviously for a windows or console application it would be the Main() function.</p>
<blockquote><p><span style="color:#0000ff;">static class Program<br />
{<br />
///&#60;summary&#62;<br />
/// The main entry point for the application.<br />
///&#60;/summary&#62;<br />
[STAThread]<br />
static void Main()<br />
{<br />
bool instanceCountOne = false;<br />
using (Mutex mtex = new Mutex(true, &#8220;MyRunningApp&#8221;, out instanceCountOne))<br />
{<br />
if (instanceCountOne)<br />
{<br />
Application.EnableVisualStyles();<br />
Application.SetCompatibleTextRenderingDefault(false);<br />
Application.Run(new Form1());<br />
mtex.ReleaseMutex();<br />
}<br />
else<br />
{<br />
MessageBox.Show(&#8220;An application instance is already running&#8221;);<br />
}<br />
}<br />
}<br />
}</span></p></blockquote>
<p>Note: Replace the application name with your corresponding application name. Include the namespace System.Threading.</p>
<p>Happy coding&#8230;</p>
<p>yenkay</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[C#: Disallow multiple instances of your application. The Mutex way.]]></title>
<link>http://hashfactor.wordpress.com/2009/02/02/c-disallow-multiple-instances-of-your-application-the-mutex-way/</link>
<pubDate>Sun, 01 Feb 2009 14:39:00 +0000</pubDate>
<dc:creator>hashfactor</dc:creator>
<guid>http://hashfactor.wordpress.com/2009/02/02/c-disallow-multiple-instances-of-your-application-the-mutex-way/</guid>
<description><![CDATA[Previously I used the Process class to find out if a process with the same name is already running. ]]></description>
<content:encoded><![CDATA[Previously I used the Process class to find out if a process with the same name is already running. ]]></content:encoded>
</item>
<item>
<title><![CDATA[Mutex &amp; Semaphore's]]></title>
<link>http://usefulaspandcsharp.wordpress.com/2009/01/19/mutex-semaphores/</link>
<pubDate>Mon, 19 Jan 2009 14:57:24 +0000</pubDate>
<dc:creator>Sarah</dc:creator>
<guid>http://usefulaspandcsharp.wordpress.com/2009/01/19/mutex-semaphores/</guid>
<description><![CDATA[The difference between a mutex and a semaphore is something I often forget. So while this is fresh i]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>The difference between a mutex and a semaphore is something I often forget. So while this is fresh in my mind&#8230;</p>
<p>Mutex: Locks a critcal section of code when in use by a thread, and unlocks it only when that thread is complete. It works on a one out, one in system but only ever allows one thread at any time. If a thread tries to access the mutex when it is locked, it is forced to wait in a queue until it is unlocked.</p>
<p>Semaphore: A semaphore is an extension of a mutex and also locks a critical section of code when in use by a thread. However with a semaphore you can specify how many threads are allowed simultaneous access. For example, your semaphore allows 4 simultaneous threads. Each time a thread uses the semaphore, the semaphore decreases its counter by 1 until it is zero, at which point no more threads are allowed access and are forced to queue. When a thread finishes using the semaphore, the semaphore increaes it&#8217;s counter by one to allow the next thread in the queue access.</p>
<p>This site provided me with the best explanation: <a title="GeeksWithBlogs.net" href="http://geekswithblogs.net/shahed/archive/2006/06/09/81268.aspx" target="_blank">http://geekswithblogs.net/shahed/archive/2006/06/09/81268.aspx</a></p>
<p>Also, put even simpler by this site <a href="http://www.cs.tau.ac.il/~hayim/courses/os/lecture_notes/lecture5/lecture.html" target="_blank">http://www.cs.tau.ac.il/~hayim/courses/os/lecture_notes/lecture5/lecture.html</a> :</p>
<p>&#8220;A mutex allows one thread inside the critical section, a semaphore allows  n threads in a critical section (when the number n is given as a parameter on the initialization).&#8221;</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Mutex Could not be Created]]></title>
<link>http://bajosusalas.wordpress.com/2008/11/23/mutex-could-not-be-created/</link>
<pubDate>Sun, 23 Nov 2008 00:13:00 +0000</pubDate>
<dc:creator>Andrea</dc:creator>
<guid>http://bajosusalas.wordpress.com/2008/11/23/mutex-could-not-be-created/</guid>
<description><![CDATA[Si les da este error cuando tratan de navegar un site hecho en ASP.net 2, con Visual Studio 2005, si]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Si les da este error cuando tratan de navegar un site hecho en <strong>ASP.net 2</strong>, con <strong>Visual Studio 2005</strong>, sigan estos pasos para solucionarlo:</p>
<ol>
<li>Si tenes el VS abierto, cerralo</li>
<li>Anda a la carpeta de archivos temporales de ASP.NET:  <em>&#60;Windows dir&#62;\Microsoft.Net\Framework\v2.0&#60;nro. del framework&#62;\Temporary ASPNET pages</em></li>
<li>Borra la carpeta de tu aplicacion (o todas, si tenes ganas &#8212; son temporales)</li>
<li>Corre <em>iisreset</em> para reiniciar el IIS</li>
<li>Abri un browser y navega tu sitio</li>
<li>Ahora abri el Visual Studio.</li>
</ol>
<p>Parece ser un problema con los permisos en algunas carpetas internas.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Mutex (Мьютекс) – запрет на запуск более чем одной копии программы]]></title>
<link>http://bitonic.wordpress.com/2008/10/09/mutex-%d0%bc%d1%8c%d1%8e%d1%82%d0%b5%d0%ba%d1%81-%e2%80%93-%d0%b7%d0%b0%d0%bf%d1%80%d0%b5%d1%82-%d0%bd%d0%b0-%d0%b7%d0%b0%d0%bf%d1%83%d1%81%d0%ba-%d0%b1%d0%be%d0%bb%d0%b5%d0%b5-%d1%87%d0%b5%d0%bc/</link>
<pubDate>Thu, 09 Oct 2008 06:21:23 +0000</pubDate>
<dc:creator>bitonic</dc:creator>
<guid>http://bitonic.wordpress.com/2008/10/09/mutex-%d0%bc%d1%8c%d1%8e%d1%82%d0%b5%d0%ba%d1%81-%e2%80%93-%d0%b7%d0%b0%d0%bf%d1%80%d0%b5%d1%82-%d0%bd%d0%b0-%d0%b7%d0%b0%d0%bf%d1%83%d1%81%d0%ba-%d0%b1%d0%be%d0%bb%d0%b5%d0%b5-%d1%87%d0%b5%d0%bc/</guid>
<description><![CDATA[Решился написать серию микро-статей о программировании под Win32. Статья про Мьютексы будет первая. ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Решился написать серию микро-статей о программировании под Win32. Статья про Мьютексы будет первая.</p>
<p>Частенько сталкивался с вопросом новичков в программировании – «Как сделать так , чтобы запускалась только одна копия программы?».  Все очень просто – как один из способов, нужно использовать Мьютексы (англ. Mutex  &#8211; объект взаимного исключения). Вообще-то Мьютексы используются для синхронизации потоков, но они очень хорошо подходят и для много другого (когда человек пишет троянца или кейлогер, то нужно быть стопроцентно уверенным, что запустится только одна копия программы, иначе могут быть конфликты).</p>
<p>Так как основным языком программирования для меня является C++, то и пример того, как надо использовать Мьютексы я буду реализовывать на этом языке. Пример очень простой. Все объяснения находятся внутри кода. Алгоритм работы: создаем мьютекс с  определенным именем; если мьютекс создался (не вернул код ошибки), то это значит, что запущена только одна копия программы и поэтому работа программы продолжается; если мьютекс не был создан (вернул код ошибки), то это значит, что уже запущена одна копия программы и работу второй копии программы нужно завершить (выход из программы через “..return 0;”).</p>
<p>// Mutex.cpp : Defines the entry point for the console application.<br />
//</p>
<p>#include &#8220;stdafx.h&#8221;<br />
#include &#60;iostream&#62;<br />
#include &#60;tchar.h&#62;<br />
#include &#60;windows.h&#62;</p>
<p>bool Alone()<br />
{<br />
HANDLE hMutex = CreateMutex(0,TRUE,&#8221;UniqueMutex!&#8221;);</p>
<p>if(GetLastError() == ERROR_ALREADY_EXISTS)<br />
{<br />
CloseHandle(hMutex);<br />
return false;<br />
}<br />
return true;<br />
}</p>
<p>int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
if(!Alone())<br />
return 0;<br />
else<br />
{<br />
for(int i=0;i&#60;10;i++)<br />
{<br />
std::cout &#60;&#60; &#8220;I am alone <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8221; &#60;&#60; std::endl;</p>
<p>Sleep(2000);<br />
}<br />
}<br />
return 0;<br />
}</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Mutual Exclusion Definitions]]></title>
<link>http://ibuilthiscage.com/2008/07/29/mutual-exclusion-definitions/</link>
<pubDate>Wed, 30 Jul 2008 00:23:17 +0000</pubDate>
<dc:creator>moffdub</dc:creator>
<guid>http://ibuilthiscage.com/2008/07/29/mutual-exclusion-definitions/</guid>
<description><![CDATA[In my world, concurrency is poorly understood. Most of my experience with it has been with simple lo]]></description>
<content:encoded><![CDATA[In my world, concurrency is poorly understood. Most of my experience with it has been with simple lo]]></content:encoded>
</item>
<item>
<title><![CDATA[Death of the Thread Model]]></title>
<link>http://ibuilthiscage.com/2008/07/19/death-of-the-thread-model/</link>
<pubDate>Sat, 19 Jul 2008 20:21:37 +0000</pubDate>
<dc:creator>moffdub</dc:creator>
<guid>http://ibuilthiscage.com/2008/07/19/death-of-the-thread-model/</guid>
<description><![CDATA[You haven&#8217;t really had the fear of Turing struck into you until you&#8217;ve tried concurrent ]]></description>
<content:encoded><![CDATA[You haven&#8217;t really had the fear of Turing struck into you until you&#8217;ve tried concurrent ]]></content:encoded>
</item>
<item>
<title><![CDATA[Forcing just one instance of an Application to execute at a time]]></title>
<link>http://syedgakbar.wordpress.com/2008/07/14/forcing-just-one-instance-of-an-application-to-execute-at-a-time/</link>
<pubDate>Mon, 14 Jul 2008 17:57:27 +0000</pubDate>
<dc:creator>syedgakbar</dc:creator>
<guid>http://syedgakbar.wordpress.com/2008/07/14/forcing-just-one-instance-of-an-application-to-execute-at-a-time/</guid>
<description><![CDATA[When developing a desktop application, it&#8217;s often required that only one instance of applicati]]></description>
<content:encoded><![CDATA[When developing a desktop application, it&#8217;s often required that only one instance of applicati]]></content:encoded>
</item>
<item>
<title><![CDATA[The Threading Discussion]]></title>
<link>http://twigleaf.wordpress.com/2008/07/02/the-threading-discussion/</link>
<pubDate>Wed, 02 Jul 2008 22:29:18 +0000</pubDate>
<dc:creator>twigleaf</dc:creator>
<guid>http://twigleaf.wordpress.com/2008/07/02/the-threading-discussion/</guid>
<description><![CDATA[I always get cranky when reading discussions about Threads. For some reason a large group of program]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I always get cranky when reading discussions about Threads. For some reason a large group of programmers think coding threads is hard. And with every news article that touches the issue, the discussion reignites.</p>
<p>Yes, threads can be hard and complicated at times, especially when dealing with locks, mutex&#8217;s, semaphores, etc. but when you rewind your way of thinking there&#8217;s some easy guidelines that will make threading a second nature.</p>
<p><strong>First</strong> one; think object-oriented. Thinking in a straight serialized way like the way for example the C-language is seen, limits your actual options. You need to think in separate units. When dealing with classes it&#8217;s easier to separate functionalities and make them function on their own without depending on others.</p>
<p><strong>Second</strong> one; every application that has a user-interface is an opportunity to use threads. Everything the user clicks on can potentially spawn a new thread or reuse one.</p>
<p>The slight downside to a GUI application, is that some GUI&#8217;s don&#8217;t really like multithreading. GTK is one of them. The Win32 API is generally really cool about it, and Cocoa can do it as well. In environments like GTK, you have to think of a messaging system that hands over feedback in-between GUI thread iterations.</p>
<p>A simple system I have come up with for GTK, is to message and buffer function-pointers, so an entire custom function can handle all of the GUI changes.</p>
<p><strong>Third</strong>; don&#8217;t over-think locking mechanisms nor mutexes. Think of ways to avoid communication between multiple threads that are running by giving the data they need in the beginning. If you simply have to, find a way to just use a limited amount of functions and centralize them so you can encase them with locks.</p>
<p>If you want to share an object filled with data, the easiest thing you can do is to make a copy with one synchronized function.</p>
<p><strong>Fourth</strong>; think generic. If you&#8217;ve written a piece of code before, you can separate it into a new function or even a new object given specific variables. If you can do that, then you&#8217;re a step closer to figuring out if that piece of code can be threaded.</p>
<p><strong>Fifth</strong>; Don&#8217;t write something you&#8217;re not yet done with. You see it all the time, even I succumb to the temptation quite often &#8211; functions that hint at something that&#8217;s not yet ready. Functions like newRecord() that append an empty object to an array, while you still have to fill in the values. Or public record counts that are incremented before the record has been added.</p>
<p>If you see a code snip like this;</p>
<pre><code>void addNewValue( string myvalue ) {
   myValueCount++;
   resizeMyArrayTo( myValueCount );
   myValues[myValueCount-1] = myvalue;
   myvalue = myvalue + " my appendix";
}</code></pre>
<p>You&#8217;re in serious need of a rewrite to this;</p>
<pre><code>void addNewValue( string myvalue ) {
   myvalue = myvalue + " my appendix";
   resizeMyArrayTo( myValueCount + 1 );
   myValues[myValueCount] = myvalue;
   myValueCount++;
}</code></pre>
<p>If you&#8217;re careful enough to make your code depend on variable assignments that have the least chance of being split into separate opcodes, then you don&#8217;t even have to use locks in most threaded environments. So sure, myValueCount++ will probably be split into 2 opcodes, one that increments a register, and one that assigns the value to the actual variable. So eventhough in an extremely rare case that those two opcodes get split, and at the same time another item wants to change the same variable, your variable will be off by 1, but at least your application won&#8217;t crash. In the first example your application has the potential to crash, as the variable that&#8217;s incremented before the actual value has been appended will give other threads the opportunity to access a non-existing object.</p>
<p>The same thing happens when you use things like this;</p>
<pre><code>function doSomething( int i ) {
   CMyValue *obj = myTable-&#62;record[i]-&#62;newValue();
   obj-&#62;datatype = STR;
   obj-&#62;value = "this is my value"
}</code></pre>
<p>Make your code prepare the object before you give it to someone else;</p>
<pre><code>function doSomething( int i ) {
   CMyValue *obj = new CMyValue();
   obj-&#62;datatype = STR;
   obj-&#62;value = "this is my value"

   myTable-&#62;record[i]-&#62;appendValue( obj );
}</code></pre>
<p>Threading is way of thinking, and once you&#8217;re able to think in that direction, it won&#8217;t seem so hard anymore.</p>
</div>]]></content:encoded>
</item>

</channel>
</rss>
