<?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/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>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Inerlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#229919</link><pubDate>Wed, 15 Sep 2004 14:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:229919</guid><dc:creator>Chris</dc:creator><description>Shouldn't that while condition be &amp;quot;!= lOriginal&amp;quot;?  I thought InterlockedCompareExchange returned the initial value of the destination, and we want to keep looping if the result wasn't what we expected.</description></item><item><title>re: Inerlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#229929</link><pubDate>Wed, 15 Sep 2004 14:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:229929</guid><dc:creator>Raymond Chen</dc:creator><description>D'oh! You're right.</description></item><item><title>re: Inerlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#229931</link><pubDate>Wed, 15 Sep 2004 14:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:229931</guid><dc:creator>anon</dc:creator><description>Spelled Interlocked wrong in title:&lt;br&gt;&amp;gt;Inerlocked&amp;lt; operations don't solve everything &lt;br&gt;&lt;br&gt;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#230010</link><pubDate>Wed, 15 Sep 2004 17:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230010</guid><dc:creator>jeffdav</dc:creator><description>I came across someone who really did not understand InterlockedIncrement() once.  I had to remove the following:&lt;br&gt;&lt;br&gt;/* Synchronization */&lt;br&gt;#define InterlockedIncrement(plong) (++(*(plong)))&lt;br&gt;#define InterlockedDecrement(plong) (--(*(plong)))&lt;br&gt;&lt;br&gt;&lt;br&gt;I was sad.</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#230016</link><pubDate>Wed, 15 Sep 2004 17:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230016</guid><dc:creator>Seth McCarus</dc:creator><description>Okay, two simple and probably stupid questions:&lt;br&gt;&lt;br&gt;Why is there not InterlockedAssign function?  (I guess InterlockedExchange could be used there, but still...)&lt;br&gt;&lt;br&gt;How can one iterate a list created with InitializeSListHead?</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#230023</link><pubDate>Wed, 15 Sep 2004 17:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230023</guid><dc:creator>Raymond Chen</dc:creator><description>1. Please read the linked page that opens the article.&lt;br&gt;2. You can't walk a linked list atomically.</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/09/15/229915.aspx#230033</link><pubDate>Wed, 15 Sep 2004 18:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:230033</guid><dc:creator>Seth McCarus</dc:creator><description>What about on a multiprocessor system?  If two CPUs try to write to the same memory location &amp;quot;simultaneously,&amp;quot; would there not be problems?&lt;br&gt;&lt;br&gt;Maybe synchronization is performed on a hardware level??</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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?</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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.</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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).</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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;</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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.  :/</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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 :)</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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?</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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.</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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).</description></item><item><title>re: Interlocked operations don't solve everything</title><link>http://blogs.msdn.com/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;</description></item></channel></rss>