<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx</link><description>They allow for atomic updates, but that doesn't necessarily mean you can skip the critical section.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230570</link><pubDate>Thu, 16 Sep 2004 19:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230570</guid><dc:creator>Trent Glascock</dc:creator><description>MikeF: Waiting on a critical section is an expensive operation.  So expensive that on multi-processor systems you can elect to &amp;quot;spin&amp;quot; before waiting on the critical section.  The code  in the original post could loop through a hundred iterations and still come out ahead of waiting on a busy critical section.&lt;br&gt;&lt;br&gt;See InitializeCriticalSectionAndSpinCount&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://msdn.microsoft.com/library/en-us/dllproc/base/initializecriticalsectionandspincount.asp"&gt;http://msdn.microsoft.com/library/en-us/dllproc/base/initializecriticalsectionandspincount.asp&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230570" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230416</link><pubDate>Thu, 16 Sep 2004 15:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230416</guid><dc:creator>Ben Hutchings</dc:creator><description>Mike wrote: &amp;quot;After a bus assert I suppose the CPU clears the dcache but now I'm just guessing :)&amp;quot;&lt;br&gt;&lt;br&gt;That would take far too long. Actually processors in SMP systems obey a cache coherency protocol which ensures that memory that's been modified in cache but not yet written back is forwarded from processor to processor (possibly by writing it back to memory first) and memory that's about to be modified by one processor is invalidated in the caches of the others.&lt;br&gt;&lt;br&gt;The problems of memory coherence are not due to caching but to reordering of instructions in the processor core and of memory operations issued by the processor core to its cache(s).&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230416" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230391</link><pubDate>Thu, 16 Sep 2004 14:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230391</guid><dc:creator>Seth McCarus</dc:creator><description>Thanks for your answers, Raymond and Mike.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230391" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230377</link><pubDate>Thu, 16 Sep 2004 13:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230377</guid><dc:creator>MikeF</dc:creator><description>Possibly silly question: What is the &amp;quot;critical section bottleneck&amp;quot;, and why is looping over a cmpxchg better?&lt;br&gt;&lt;br&gt;My understanding is that a pure critical section approach (a) lets the OS sleep the blocked thread, and (b) wastes less time than the cmpxchg loop anyway.&lt;br&gt;&lt;br&gt;I'm almost certainly missing something here but I can't see what. Is the overhead on critical sections particularly big?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230377" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230366</link><pubDate>Thu, 16 Sep 2004 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230366</guid><dc:creator>Mike</dc:creator><description>Why are they called the interlocked functions rather than the atomic functions? ie AtomicIncrement etc. That would be a slightly more intuitive name (albiet, doesn't sound as cool :)&lt;br&gt;&lt;br&gt;Seth: yes the sync is done with hardware.&lt;br&gt;&lt;br&gt;InterlockedCompareExchange() is some assembly like this:&lt;br&gt;&lt;br&gt;lock cmpxchg a, b&lt;br&gt;&lt;br&gt;The lock prefix causes the CPU to perform a &amp;quot;bus assert&amp;quot; which basically locks the bus. It prevents other processors from accessing memory while the bus is held. At least this is my understanding.&lt;br&gt;&lt;br&gt;After a bus assert I suppose the CPU clears the dcache but now I'm just guessing :)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230366" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230337</link><pubDate>Thu, 16 Sep 2004 10:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230337</guid><dc:creator>lowercase josh</dc:creator><description>If I'm not mistaken, what you have to lock for the critical section to work is the data, not the code.  If all access to that data goes through the same critical section, that first InterlockedMultiply would work correctly.  To me, &amp;quot;critical section&amp;quot; seems to imply a &amp;quot;section&amp;quot; of code, but you nearly always want to lock the data.  :/&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230337" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230131</link><pubDate>Wed, 15 Sep 2004 21:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230131</guid><dc:creator>foxyshadis</dc:creator><description>Interestingly, one of the best explanations of ABA google came up with was in this blog.&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://weblogs.asp.net/oldnewthing/archive/2004/08/17/215682.aspx#217600"&gt;http://weblogs.asp.net/oldnewthing/archive/2004/08/17/215682.aspx#217600&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230131" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230126</link><pubDate>Wed, 15 Sep 2004 21:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230126</guid><dc:creator>Pavel Lebedinsky</dc:creator><description>The MSDN article that Jordan referred to is not quite correct, by the way:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://groups.google.com/groups?selm=3f79e7ee%241%40news.microsoft.com&amp;amp;output=gplain"&gt;http://groups.google.com/groups?selm=3f79e7ee%241%40news.microsoft.com&amp;amp;output=gplain&lt;/a&gt;&lt;br&gt;&lt;br&gt;Win32 should have provided a memory barrier macro/intrinsic that is guaranteed to be respected by both the compiler and hardware. As far as I know, there's still no such thing (there are various bits and pieces like the MemoryBarrier macro but nothing that prevents both CPU and compiler reordering).&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230126" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230037</link><pubDate>Wed, 15 Sep 2004 18:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230037</guid><dc:creator>Jordan Russell</dc:creator><description>An &amp;quot;InterlockedAssign&amp;quot; function would be handy because while writes to aligned 32-bit variables are always atomic, they're not always _ordered_ on non-x86 platforms as explained at &amp;lt;&lt;a target="_new" href="http://msdn.microsoft.com/library/en-us/dllproc/base/synchronization_and_multiprocessor_issues.asp&amp;gt;"&gt;http://msdn.microsoft.com/library/en-us/dllproc/base/synchronization_and_multiprocessor_issues.asp&amp;gt;&lt;/a&gt;. That article suggests using InterlockedExchange instead of a direct assignment, but that seems like overkill on platforms where writes are always ordered, e.g. x86.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230037" width="1" height="1"&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2004/09/15/229915.aspx#230035</link><pubDate>Wed, 15 Sep 2004 18:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230035</guid><dc:creator>Johan</dc:creator><description>It's not clear to me what the intended memory access/ordering semantics of the plain (non-*Acquire/*Release) Interlocked* operations are. The documentation says nothing about it, but there's an IA64-specific note indicating an associated *full* memory barrier. Is that the intended semantics? Or was that fence just added to avoid breaking all the existing code which assumes an x86-like memory model?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=230035" width="1" height="1"&gt;</description></item></channel></rss>