<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>My Adventures with Microsoft Technologies</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/rnarayan/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/rnarayan/feed.xml</link>
    <description />
    <lastBuildDate>Tue, 16 Apr 2019 05:26:41 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Azure management Libraries for .NET</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/azure-management-libraries-for-net</link>
      <pubDate>Mon, 26 May 2014 13:54:56 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2014/05/26/azure-management-libraries-for-net/</guid>
      <description><![CDATA[Ever wondered on how to manage Azure using .NET instead of PowerShell.&nbsp; Get yourselves...]]></description>
      <content:encoded><![CDATA[<p>Ever wondered on how to manage Azure using .NET instead of PowerShell.&nbsp; Get yourselves introduced to MAML</p>
<p><a href="https://www.bradygaster.com/">https://www.bradygaster.com/</a></p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Microsoft Maniacs - User group</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/microsoft-maniacs-user-group</link>
      <pubDate>Sun, 13 Apr 2014 13:05:54 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2014/04/13/microsoft-maniacs-user-group/</guid>
      <description><![CDATA[Are you living in and around DC? Would you prefer to attend 100+ Microsoft fans meet-up and...]]></description>
      <content:encoded><![CDATA[<p>Are you living in and around DC? Would you prefer to attend 100+ Microsoft fans meet-up and collaborate with them once a month? Here you go. Register right away and be part of 100s of Microsoft Technology fans here - <a href="https://www.meetup.com/microsoft-Maniacs/">https://www.meetup.com/microsoft-Maniacs/</a>&nbsp;We invite MVPs to give a presentation on various topics on Microsoft Technology Stack.&nbsp; Do you want to talk in our group? Contact me through the meet up site.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Azure and MSDN subscription</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/azure-and-msdn-subscription</link>
      <pubDate>Sun, 13 Apr 2014 13:00:16 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2014/04/13/azure-and-msdn-subscription/</guid>
      <description><![CDATA[Do you know that your MSDN subscription can bring some Azure credits to you monthly? Yes.&nbsp;...]]></description>
      <content:encoded><![CDATA[<p>Do you know that your MSDN subscription can bring some Azure credits to you monthly? Yes.&nbsp; Based on your level of subscription, you can get $50 to $150 per month. For more details refer this link - <a href="https://azure.microsoft.com/en-us/pricing/member-offers/msdn-benefits-details/">https://azure.microsoft.com/en-us/pricing/member-offers/msdn-benefits-details/</a></p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Want to try Azure for free?</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/want-to-try-azure-for-free</link>
      <pubDate>Fri, 09 Dec 2011 04:38:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2011/12/09/want-to-try-azure-for-free/</guid>
      <description><![CDATA[If you want to try Windows Azure, need not to worry for paying to Microsoft. Get the free pass from...]]></description>
      <content:encoded><![CDATA[<p>If you want to try Windows Azure, need not to worry for paying to Microsoft. Get the free pass from <a href="https://windowsazurepass.com/">https://windowsazurepass.com/</a>. Use Promo code as <b>MSL001 </b>and proceed with sign-up process. The sign-up process would ask you to provide Firstname, lastname and address details. Make sure the firstname and lastname matche with the login credentials details. Thats it. You are good to go.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Learn on IOCompletion threads and worker threads from this short video</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/learn-on-iocompletion-threads-and-worker-threads-from-this-short-video</link>
      <pubDate>Thu, 16 Jun 2011 05:21:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2011/06/16/learn-on-iocompletion-threads-and-worker-threads-from-this-short-video/</guid>
      <description><![CDATA[Watch this video to learn about
1.&nbsp; When to use IO Completion threads and worker threads.
2....]]></description>
      <content:encoded><![CDATA[<p>Watch this <a href="https://channel9.msdn.com/Shows/AppFabric-tv/AppFabrictv-Threading-with-Jeff-Richter">video</a> to learn about</p>
<p>1.&nbsp; When to use IO Completion threads and worker threads.</p>
<p>2. The common mistakes the developer do when connecting to other web services / IO bound operation that block the worker threads</p>
<p>3. What actually Min and max Worker Threads count to the thread pool means? </p>
<p>Short good video</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Threading deep dive – Day 10</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/threading-deep-dive-day-10</link>
      <pubDate>Tue, 09 Sep 2008 06:42:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2008/09/09/threading-deep-dive-day-10/</guid>
      <description><![CDATA[In the previous day we saw what would happen underneath when we create a managed thread and how it...]]></description>
      <content:encoded><![CDATA[<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>In the previous day we saw what would happen underneath when we create a managed thread and how it maps to native thread. That is what would happen when a managed thread executes a native / managed code.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes"></SPAN></FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>There are situations where native thread executing the native code would need to execute managed code. For example, we can consider the case of a com component executing managed code.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In those cases, when the native thread, for the first time, execute the managed code, the native object [not the managed thread object] would be created and the execution would proceed. Here the managed thread object would never get created unless the managed code refers the executing thread using the API like Thread.CurrentThread<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>What are Fibers? </FONT></SPAN></B></P>
<P style="MARGIN-BOTTOM: 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Fibers are like light weight threading objects that can be executed in parallel. Multiple fibers can be executed in a single thread. The advantage over here is that the same thread would be serving multiple fibers and the application can take the responsibility of scheduling the fibers. In case of threads, the scheduling responsibility is with the operating system. If we go with fibers, we can control scheduling of the fibers. On the negative end, Fibers are so far not yet supported in Managed code. Fibers do not support synchronization like threads. Hence the overhead fibers create overweighs the light weightiness it provide. CLR team faced some issues before they release .NET v2.0. So they removed the Fiber support from V2.0. Please refer this links for more details.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>a.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><A href="https://blogs.msdn.com/dinoviehland/archive/2005/09/15/469642.aspx"><FONT face=Calibri color=#800080 size=3>https://blogs.msdn.com/dinoviehland/archive/2005/09/15/469642.aspx</FONT></A><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>b.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><A href="https://blogs.msdn.com/cbrumme/archive/2003/04/15/51351.aspx"><FONT face=Calibri color=#800080 size=3>https://blogs.msdn.com/cbrumme/archive/2003/04/15/51351.aspx</FONT></A><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;<SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN-BOTTOM: 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>CoRoutine - Yield </FONT></SPAN></B></P>
<P style="MARGIN-BOTTOM: 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>We still have native support to Fibers. Only managed support is not there. We can Pinvoke and use fibers whenever we want. Or use Managed Extentions for C++ to implement class that internally used native fibers.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We all might have used yield keyword before the return keyword from the method so as to maintain the state of the method. This link from MSDN Magazine uses the later technique to implement yield functionality. It is very interesting. One may not want to miss it. </FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><A href="https://msdn.microsoft.com/en-us/magazine/cc164086.aspx"><FONT face=Calibri color=#800080 size=3>https://msdn.microsoft.com/en-us/magazine/cc164086.aspx</FONT></A><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Fiber or Thread: Which is best?</FONT></SPAN></B></P>
<P style="MARGIN-BOTTOM: 0pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri size=3>Simple. One size does not fit for all. You will have to choose whatever works for you. But before you choose one, why not get an expert idea from this link - </FONT><A href="https://blogs.msdn.com/cbrumme/archive/2003/04/15/51351.aspx"><FONT face=Calibri color=#800080 size=3>https://blogs.msdn.com/cbrumme/archive/2003/04/15/51351.aspx</FONT></A><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>T</FONT></FONT><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>omorrow let us jump into Threadpool and explore.<o:p></o:p></FONT></FONT></SPAN></P>]]></content:encoded>
    </item>
    <item>
      <title>Threading deep dive – Day 9</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/threading-deep-dive-day-9</link>
      <pubDate>Fri, 08 Aug 2008 08:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2008/08/08/threading-deep-dive-day-9/</guid>
      <description><![CDATA[Atomic operation on Multi-core / Multi processors machine.
Executing atomic operation is relatively...]]></description>
      <content:encoded><![CDATA[<P style="MARGIN-BOTTOM: 0pt"><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Atomic operation on Multi-core / Multi processors machine.</FONT></FONT></SPAN></B></P>
<P style="MARGIN-BOTTOM: 0pt"><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Executing atomic operation is relatively less expensive In a single processor / core machine, when compared to the multi processor machine. Operating system on single processor machine can simply make<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>sure the scheduler does not preempt the atomic operation executing thread by raising the IRQL<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>is sufficient. In fact some of the atomic operations [like XADD] are supported at assembly level.</FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>But in case of multi-core / processor machine, there may be chance that multiple processor are trying to modify the same memory location. This case has to be handled differently.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We all know the processor access the values<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>from the caches.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The caches are capable of holding multiple values. When one processor modifies one specific cache-line, the corresponding cache-lines accessed by other processors would be invalidated. This is achieved with the help of bus transaction. During the bus transaction all the processors are stopped and corresponding cache-lines are invalidated. Then the modified value would be transferred to the main memory. So when other processors need the value , it would try to access it from the cache. Since the cache-line is invalidated, the value would be fetched from memory. The processor that writes the cache-line owns the owner key and that key would be moved across the processors whoever writes the same cache line. The more number of owner keys need to be transferred across processors involves more number bus transactions. Here I briefed the common technique.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>But this implementation vary from one architecture to other. There are around 8 to 9<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>protocols on how to share values across processors caches. More number of Bus transactions reduces application performance. For more details refer this link - </FONT></FONT><A href="https://cache-www.intel.com/cd/00/00/01/76/17689_w_spinlock.pdf" mce_href="https://cache-www.intel.com/cd/00/00/01/76/17689_w_spinlock.pdf"><FONT face=Calibri color=#800080 size=3><A href="https://cache-www.intel.com/cd/00/00/01/76/17689_w_spinlock.pdf" mce_href="https://cache-www.intel.com/cd/00/00/01/76/17689_w_spinlock.pdf">https://cache-www.intel.com/cd/00/00/01/76/17689_w_spinlock.pdf</FONT></A></SPAN><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN></B></A></P>
<P style="MARGIN-BOTTOM: 0pt"><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Managed Thread and Native Thread</FONT></FONT></SPAN></B></P>
<P style="MARGIN-BOTTOM: 0pt"><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Operating systems do not understand what is managed thread. Os know only native threads. So when you create a managed thread, you are simply creating a managed object. That is not a thread. This object holds fields like delegate method, exception state and name etc. But these information is not sufficient to create a native thread which is actually a real thread . CLR internally maintains another object that<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>contains all the fields required to create a native thread. So the managed thread object refers to a native object created by CLR and this native object refers to the actual thread. So when a developer creates the managed thread the below happens.</FONT></FONT></SPAN></P><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore">a.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'">Managed Thread object created. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore">b.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'">CLR internally creates a native object that holds all the info enough to create a thread.<o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"></FONT></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>When the developer calls a start method on the managed thread object below steps happen.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo3; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>a.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>The internal native object creates<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>a new / re-uses a native thread<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo4; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>b.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>The native thread start executing the appropriate delegate.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>So the native thread is always referred through native object internally created by the CLR. The advantage here is the native object can decide whether to reuse the old native thread or to create a new native thread. When the thread execution is over the native thread is not gets deleted immediately. The CLR keep the native thread for sometime even after the execution is over. Within the stipulated time if another request for a thread comes, the native object reuses the already created native thread.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Go through the below code snippet , execute and see the result to understand how native threads are re used in CLR.<o:p></o:p></FONT></FONT></SPAN></P><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int endCount = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int NonDuplicateCount = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static void Main(string[] args)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine("Start");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.ReadLine();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i = 1; i &lt; 4000; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread t = new Thread(WaitCallback);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.Start();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.ReadLine();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void WaitCallback(object state)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ht.ContainsKey(AppDomain.GetCurrentThreadId()))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine("Same native thread");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine("The Native ThreadID {0} and old managed Thread ID {1}", AppDomain.GetCurrentThreadId(), ht[AppDomain.GetCurrentThreadId()]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine("The Native ThreadID {0} and new managed Thread ID {1}", AppDomain.GetCurrentThreadId(), <BR>Thread.CurrentThread.ManagedThreadId);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(++endCount);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine("NonDuplicateCount {0}", NonDuplicateCount);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.ReadLine();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ht.Add(AppDomain.GetCurrentThreadId(), Thread.CurrentThread.ManagedThreadId);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR></SPAN></FONT></SPAN></P></FONT></SPAN>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>CLR team might have thought of using fibers in place of native thread. I hope that’s why they have provided the one indirection between managed thread and the native thread.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>As far my knowledge goes, fibers are used only in SQLServer. What are fibers? Let us see about them next day.</FONT></FONT></SPAN></P>]]></content:encoded>
    </item>
    <item>
      <title>Threading deep dive – Day 8</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/threading-deep-dive-day-8</link>
      <pubDate>Mon, 04 Aug 2008 11:27:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2008/08/04/threading-deep-dive-day-8/</guid>
      <description><![CDATA[Volatile Read / Writes
&nbsp;MSDN says "The volatile modifier is usually used for a field that is...]]></description>
      <content:encoded><![CDATA[<P class=PadderBetweenControlandBody style="MARGIN: 0in 0in 6pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Volatile Read / Writes<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><SPAN style="mso-spacerun: yes"><FONT face=Calibri size=3>&nbsp;</FONT></SPAN><A href="https://msdn.microsoft.com/en-us/library/x13ttww7(VS.71).aspx"><FONT face=Calibri size=3>MSDN</FONT></A><FONT face=Calibri size=3> says "<I>The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.</I>"<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>And refer </FONT><A href="https://msdn.microsoft.com/en-us/library/aa645755(VS.71).aspx"><FONT face=Calibri color=#800080 size=3>C# language specification for volatile field</FONT></A><FONT size=3><FONT face=Calibri>.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Now you might have good idea on what is happening when Volatile read / writes are performed. Yes. You are right. Volatile keyword tells the compiler to not to optimize the field. So that instruction reordering will not happen for reads and writes of volatile field. Volatile fields simply guarantees<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>that the sequence of writes and reads of values would be performed as specified in the code. JIT optimization does not affect volatile fields.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We can also say volatile always makes the read happening from the RAM<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Interlocked vs Volatile.<o:p></o:p></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Volatile fields only mandate sequence. Not atomicity.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We may not get up-to-date value if we use volatile as they are not atomic. Between volatile read<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>and volatile write,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>the data processing thread may get preempted and the value in the RAM may get updated by other threads [from other processors]. When the thread in question gets the processor time, it would continue process the old value as the value from the RAM is different from the time it is read.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>One should consider using Volatile on following criteria<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>Value need not to be very recent.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri><B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'">JIT</SPAN></B><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"> optimization is not required as the instruction reordering may disturb the business logic.<o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>I would suggest you the following link to go through to have good Idea on the difference between Atomic operation and Volatile keyword.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><A href="https://forums.msdn.microsoft.com/en-US/clr/thread/ec656080-7521-4f61-8df3-d7168da5cf4c"><FONT face=Calibri color=#800080 size=3>https://forums.msdn.microsoft.com/en-US/clr/thread/ec656080-7521-4f61-8df3-d7168da5cf4c</FONT></A><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Memory Barriers<o:p></o:p></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Instructions re-ordering happen at two levels. 1. A the compiler level which can be controlled with Volatile keyword. By including Volatile keyword we instruct the compiler, not to optimize the code by re-ordering the instructions. 2. Second level of reordering happens at the processor level. Memory barrier is the way to tell the underlying hardware to skip any reordering that happens at the hardware level.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Interestingly Thread.VolatilRead and Thread.VolatileWrite use memory barriers internally. Below is the reflected code snippet for Thread.VolatileRead and VolatileWrite for Int32 overloads<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">&nbsp;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">[</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplAttribute/.ctor(System.Runtime.CompilerServices.MethodImplOptions)">MethodImpl</A><SPAN style="COLOR: black">(</SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplOptions">MethodImplOptions</A><SPAN style="COLOR: black">.</SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplOptions/NoInlining">NoInlining</A><SPAN style="COLOR: black">)]<BR></SPAN><SPAN style="COLOR: #1000a0">public</SPAN><SPAN style="COLOR: black"> </SPAN><SPAN style="COLOR: #1000a0">static</SPAN><SPAN style="COLOR: black"> </SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32">int</A><SPAN style="COLOR: black"> </SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Threading.Thread/VolatileRead(Int32&amp;):Int32"><B>VolatileRead</B></A><SPAN style="COLOR: black">(</SPAN><SPAN style="COLOR: #1000a0">ref</SPAN><SPAN style="COLOR: black"> </SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32">int</A><SPAN style="COLOR: black"> address)<BR>{<BR><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32">int</A><SPAN style="COLOR: black"> <B>num</B> = address;<BR><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Threading.Thread/MemoryBarrier()">MemoryBarrier</A><SPAN style="COLOR: black">();<BR><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="COLOR: #1000a0">return</SPAN><SPAN style="COLOR: black"> num;<BR>}</SPAN><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">For Thread.VolatileWrite</SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">[</SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplAttribute/.ctor(System.Runtime.CompilerServices.MethodImplOptions)"><SPAN style="FONT-SIZE: 10pt">MethodImpl</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">(</SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplOptions"><SPAN style="FONT-SIZE: 10pt">MethodImplOptions</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">.</SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Runtime.CompilerServices.MethodImplOptions/NoInlining"><SPAN style="FONT-SIZE: 10pt">NoInlining</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">)]<BR></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #1000a0; FONT-FAMILY: 'Tahoma','sans-serif'">public</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> </SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #1000a0; FONT-FAMILY: 'Tahoma','sans-serif'">static</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> </SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Void"><SPAN style="FONT-SIZE: 10pt">void</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> </SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Threading.Thread/VolatileWrite(Int32&amp;,Int32)"><B><SPAN style="FONT-SIZE: 10pt">VolatileWrite</SPAN></B></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">(</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #1000a0; FONT-FAMILY: 'Tahoma','sans-serif'">ref</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> </SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32"><SPAN style="FONT-SIZE: 10pt">int</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> address, </SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Int32"><SPAN style="FONT-SIZE: 10pt">int</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'"> value)<BR>{<BR><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 'Tahoma','sans-serif'"><A href="https://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Threading.Thread/MemoryBarrier()"><SPAN style="FONT-SIZE: 10pt">MemoryBarrier</SPAN></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">();<BR><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>address = value;<BR>}</SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Tahoma','sans-serif'"><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Tahoma','sans-serif'">&nbsp;</SPAN><FONT size=3><FONT face=Calibri><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">Ok. Now I have set of<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>questions for you. </SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>1.Can you write atomic functions on your own? <o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>2.Can I some how avoid thread pre-emption?<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>3.Who will preempt windows scheduler?<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>4.I know about pre-emption. What is interruption?<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>5.What is Acquire and Release from Memory barrier?<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>For answers,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>refer below the links:<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"><A href="https://download.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/Locks.doc"><FONT color=#800080>https://download.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/Locks.doc</FONT></A><SPAN style="COLOR: #333333"> </SPAN></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: #333333; FONT-FAMILY: 'Verdana','sans-serif'">&nbsp;</SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><A href="https://download.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/IRQL_thread.doc"><FONT face=Calibri size=3>https://d</FONT><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'">ownload.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/IRQL_thread.doc</SPAN></A></SPAN><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>These are must read links that gives in-depth knowledge on Memory barrier,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Atomicity, Locking and Thread IRQL etc.<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><o:p>&nbsp;</o:p></SPAN><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Processor affinity</FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>On a multi-processor environment, a thread can run on any processor whenever the thread gets switched.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>OS scheduler decides on assigning different thread to different processor. There is no guarantee that the a particular thread will always run on a particular processor.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We can confine a thread to run on a particular processor. This is what called setting processor affinity to a thread. Unless there is a compelling reason , one should not set processor affinity to a thread. Setting processor affinity to a thread is not good for your program performance.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Setting processor affinity through code may lead to under utilizing some of the processors. ProcessThread class lets you assign affinity to one processor to a thread. Here you have to enumerate through all the threads from the process to get hold of ProcessThread. We cannot create Process Thread as this class does not have any constructor or singleton supporting static methods. If I'm not wrong<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>we have to use Native Thread ID to match a Thread and ProcessThread.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>What is the current thread means on a multi processor environment?</FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>A process running on a multi-processor environment may have more than one thread. In<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>that case which thread would be represented by Thread.CurrentThread property? This property always return the object of the thread that is currently executing that statement. Do not assume a process will have only one current thread at any point in time<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>in multi-processor environment.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;<SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Lock convoy?</FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri size=3>Think about this situation. Many threads are waiting for a lock. But no one owns the lock. This is kind of situation is called lock convoy. Interesting. Read more from the below link. </FONT><A href="https://blogs.msdn.com/larryosterman/archive/2004/03/29/101329.aspx"><FONT face=Calibri color=#800080 size=3>https://blogs.msdn.com/larryosterman/archive/2004/03/29/101329.aspx</FONT></A></SPAN><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>]]></content:encoded>
    </item>
    <item>
      <title>Threading deep dive – Day 7</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/threading-deep-dive-day-7</link>
      <pubDate>Tue, 08 Jul 2008 05:02:00 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2008/07/08/threading-deep-dive-day-7/</guid>
      <description><![CDATA[Reads / Writes to the memory 
Before we jump in to atomic operations, I would like to give some...]]></description>
      <content:encoded><![CDATA[<SPAN style="FONT-SIZE: 13pt">
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Reads / Writes to the memory <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Before we jump in to atomic operations, I would like to give some information on order of reads and writes into the<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>main memory. The chip makers are under pressure to make the processor faster and faster.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>To improve processor performance, the chip makers introduce multiple caches between the processor and the RAM.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><IFRAME style="BORDER-RIGHT: #dde5e9 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #dde5e9 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 3px; BORDER-LEFT: #dde5e9 1px solid; WIDTH: 240px; PADDING-TOP: 0px; BORDER-BOTTOM: #dde5e9 1px solid; HEIGHT: 66px; BACKGROUND-COLOR: #ffffff" marginWidth=0 marginHeight=0 src="https://cid-da01e65c618e772e.skydrive.live.com/embedrowdetail.aspx/Blog/ProcArch.JPG" frameBorder=0 scrolling=no mce_src="https://cid-da01e65c618e772e.skydrive.live.com/embedrowdetail.aspx/Blog/ProcArch.JPG"></IFRAME>Refer the diagram for clear understanding] The reads and writes are not performed the same order as our program does even in case single processor machines. Like single processor counterparts multiple processor machines perform read and writes in a more performing way that order of the reads and writes are not considered. When a value is read from the memory, other values that exist adjacent to the value being read also brought to the caches of the processor. So when the processor wants a value from the RAM, it looks into its local cache first. If the required value is not available in the local cache, then only the processor would try to bring the value from the main memory. If the value is available in the local cache, the processor would not go to RAM to fetch the value.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In a multi processor scenario, there could be chances that the value available in the processor’s local cache may be stale.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>To avoid the issue of reading stale data, we put locks. What does lock do? It instructs the processor to follow some order while reading and writing the data to/from the RAM.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The .NET 2.0 rune time instructs the processor(s) to follow the below protocol when the CLR executes the statement that is inside the lock block.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>Read should never go before the time the lock is acquired.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 27pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2; tab-stops: list .5in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>Write should never go beyond the lock is released.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>This also means,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>the reads and writes inside the lock block can be of any order. This freedom is given for processors<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>to improve their performance. When we force the processor to perform reads and writes in certain order, it affects the speed of the processor.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In case of locks we trade off processor performance with synchronization.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT><FONT face=Calibri><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">What are Atomic operations? </SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>Operations that would get executed fully before the thread that executes the operation pre-empted are called atomic operation.<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>In other words, The thread that executes the atomic operations would not get preempted before completing the atomic operation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Interlocked class from .NET framework provides atomic functionality.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Atomic operations are faster then lock.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Atomic operations guarantee that the atomic reads are always from the main memory and atomic writes are immediately updated to the main memory immediately.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>One can write effective locking with the help of Interlocked. Exchange method.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Though locks are best fit for many places and solves most of the synchronization related issues, performance of the locks are not so great when we compare them with interlocked.<o:p></o:p></FONT></SPAN></FONT></P>
<P style="MARGIN-BOTTOM: 0pt"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Locks vs atomic operations<o:p></o:p></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Locks controls instruction reordering where as interlocked operations guarantees reads /writes from/to the processors is immediate and would be available to other processors / threads that does appropriate reads. In fact acquiring lock is an atomic operation. If acquiring lock is not an atomic operation, then more than one thread could acquire same lock. I have seen code where people use locks to just to increment one value.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For these cases interlocked class is of good fit.<BR></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Refer below the code snippets: <o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri></FONT></FONT></SPAN>&nbsp;</P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>// Uses Interlocked classes<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>static void Main(string[] args)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>int j = -10;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>Stopwatch sw = new Stopwatch();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>sw.Start();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>for (int i = 0; i &lt; Int32.MaxValue; i++)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>j++;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>sw.Stop();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Console.WriteLine("Total time taken {0} ms", sw.ElapsedMilliseconds);<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Console.ReadLine();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}</FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><o:p></o:p></FONT></FONT></SPAN>&nbsp;</P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>// Uses Locks<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>static void Main(string[] args)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>object lockObj = new object();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>int j = -10;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Stopwatch sw = new Stopwatch();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>sw.Start();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>for (int i = 0; i &lt; Int32.MaxValue; i++)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>lock (lockObj)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>j++;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>sw.Stop();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Console.WriteLine("Total time taken {0} ms", sw.ElapsedMilliseconds);<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Console.ReadLine();<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 27pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}</FONT></FONT></SPAN>&nbsp;&nbsp;</P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>I have written these code snippets to compare the speed of lock and interlocked statement execution. When I executed above snippets from my laptop [Toshiba Tecra-M5, 2 GHZ, Dual core, 4GB RAM- Win2K3], below are results that I got.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;</FONT></FONT></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><STRONG>Interlocked - Total time taken 64326 ms:<o:p></o:p></STRONG></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><STRONG>Lock - Total time taken 141203 ms:<o:p></o:p></STRONG></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>It is clear that locks take more time.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Whenever possible, try to use methods from interlocked classes.</FONT></FONT></SPAN></P>
<P style="MARGIN-BOTTOM: 0pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"></SPAN><SPAN style="FONT-SIZE: 13pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT face=Calibri>Atomic operations on Multi-core / Multi processors machines.<o:p></o:p></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 22.45pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Though atomic operations guarantee to reflect the latest modification to the primary memory and in the processor that executes, it the responsibility of the code to use atomic operations wherever required.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Assume the following<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 40.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list 1.25in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>You have got two processor machines.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 40.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2; tab-stops: list 1.25in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>Processor 1 Execute one atomic operation on a variable var1.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 40.45pt; VERTICAL-ALIGN: middle; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo3; tab-stops: list 1.25in"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: Calibri; mso-bidi-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>3.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'"><FONT size=3><FONT face=Calibri>At the same time Processor2 access var2 using normal read.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 22.45pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt 22.45pt"><SPAN style="mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri"><FONT size=3><FONT face=Calibri>In this case, Processor2 may get stale value as it has no idea that var2 value is changed in the memory.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>To make the processor 2 to read new up to-date value, code that reads var1 from processor 2, should do volatile read / make use of lock as the processor 2 may have stale data in its cache which might not have been invalidated.<o:p></o:p></FONT></FONT></SPAN></P>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
    <item>
      <title>How to constrain your server?</title>
      <link>https://docs.microsoft.com/archive/blogs/rnarayan/how-to-constrain-your-server</link>
      <pubDate>Thu, 26 Jun 2008 10:31:51 GMT</pubDate>
      <dc:creator><![CDATA[Rajapadmanaban Narayanaswamy]]></dc:creator>
      <guid
        isPermaLink="false">https://qa.msdn.wdslab.com/rnarayan/2008/06/26/how-to-constrain-your-server/</guid>
      <description><![CDATA[Today our team wanted to perform load testing our application on a low end server. The server we...]]></description>
      <content:encoded><![CDATA[<p>Today our team wanted to perform load testing our application on a low end server. The server we have is of 8 proc, 16 GB RAM and 64bit configuration. We wanted to load test our application multiple times by making it run on different number of processors and different amount of physical memory each time. 
</p><p>To constrain processor, the Initial idea was to setting the processor affinity to the application process through task manager or sys internal's process explorer tool. But the idea became invalid as the OS and other services that support our application may be using all other processors as we are setting affinity to our application process only. Our test team members felt that, this case may not simulate the real time environment. So we left this idea.
</p><p>Then we have found out the tool "Consume.exe" that is shipped with VS.NET 8.0 tools directory. Consume.exe can block the following resources.
</p><ol><li>Physical memory
</li><li>Page file
</li><li>Disk space
</li><li>CPU time
</li><li>Kernel pool
</li></ol><p>It is a good tool that can eat all the mentioned resources and make the application to use only the remaining resources. Our team members suspected that, with this approach, there could be unknown factors that would influence our application's performance in negative / positive way.   This tool can be used to simulate a situation where other applications also runs along with your application.   So we had to leave this idea also as our application is server application and it would be running alone in the server.
</p><p> 
 </p><p>Finally found out one good approach that was accepted by all of our team members. That was nothing but modifying boot.ini file. So we used the following switches from boot.ini file for our requirement.
</p><p><span style="color:#1f497d">/NUMPROC = 1 
</span></p><p><span style="color:#1f497d">/MAXMEM = 1024
</span></p><p>After modifying and rebooting the system, the OS was showing / utilizing only the mentioned amount of resources. To read more about other switches and its details refer <a href="https://technet.microsoft.com/en-us/sysinternals/bb963892.aspx">Technet</a> site.
</p>]]></content:encoded>
    </item>
  </channel>
</rss>