<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: pthread spinlocks</title>
	<atom:link href="http://www.alexonlinux.com/pthread-spinlocks/feed" rel="self" type="application/rss+xml" />
	<link>http://www.alexonlinux.com/pthread-spinlocks</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2012 21:17:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Mike</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-27752</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Sun, 20 Nov 2011 04:55:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-27752</guid>
		<description>Ngargh! This post is ancient but the commenters compel me to reply!

The point of using spinlocks is never to hold them very long, e.g. for modifying a small bit of memory like Alex said. Then release them again. You shouldn&#039;t even make system calls or anything while holding them. They are extremely hot potatoes that you do not want to hold.

Cyril: After thread A gets the spinlock, it should also be at 100% cpu until it releases it again. It should also hold it for very little time; much less than a timeslice. If by luck its timeslice ends and B is scheduled, B may spin for a timeslice, it&#039;s true. But B should be no busier than A. Thread A should be hustling while it has that lock.

peterix: Same as above. You&#039;re right, they don&#039;t play nice with schedulers. The idea is not to let it get to the scheduler most of the time. Grab and release the lock quickly so that the holding thread rarely gets descheduled while holding it. Don&#039;t make syscalls which could deschedule the thread while holding it.

If the contending threads are constantly rapidly taking and releasing the spinlock, such that they&#039;re likely to be interrupted while holding the lock, you&#039;re spending too much time on synchronization anyway. It would be better to batch the communications somehow, then use mutexes that can safely be held for longer times. And maybe use spinlocks for a short, uncommon metadata transfers outside the batch.&lt;div class=&quot;comment-remix-meta&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;replyto&quot; onclick=&quot;replyto(&#039;27752&#039;,&#039;Mike&#039;); return false;&quot;&gt;Reply&lt;/a&gt;  - &lt;a href=&quot;#&quot; class=&quot;quote&quot; onclick=&quot;quote(&#039;27752&#039;,&#039;Mike&#039;,&#039;Ngargh! This post is ancient but the commenters compel me to reply!\r\n\r\nThe point of using spinlocks is never to hold them very long, e.g. for modifying a small bit of memory like Alex said. Then release them again. You shouldn\&#039;t even make system calls or anything while holding them. They are extremely hot potatoes that you do not want to hold.\r\n\r\nCyril: After thread A gets the spinlock, it should also be at 100% cpu until it releases it again. It should also hold it for very little time; much less than a timeslice. If by luck its timeslice ends and B is scheduled, B may spin for a timeslice, it\&#039;s true. But B should be no busier than A. Thread A should be hustling while it has that lock.\r\n\r\npeterix: Same as above. You\&#039;re right, they don\&#039;t play nice with schedulers. The idea is not to let it get to the scheduler most of the time. Grab and release the lock quickly so that the holding thread rarely gets descheduled while holding it. Don\&#039;t make syscalls which could deschedule the thread while holding it.\r\n\r\nIf the contending threads are constantly rapidly taking and releasing the spinlock, such that they\&#039;re likely to be interrupted while holding the lock, you\&#039;re spending too much time on synchronization anyway. It would be better to batch the communications somehow, then use mutexes that can safely be held for longer times. And maybe use spinlocks for a short, uncommon metadata transfers outside the batch.&#039;); return false;&quot;&gt;Quote&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>Ngargh! This post is ancient but the commenters compel me to reply!</p>
<p>The point of using spinlocks is never to hold them very long, e.g. for modifying a small bit of memory like Alex said. Then release them again. You shouldn&#8217;t even make system calls or anything while holding them. They are extremely hot potatoes that you do not want to hold.</p>
<p>Cyril: After thread A gets the spinlock, it should also be at 100% cpu until it releases it again. It should also hold it for very little time; much less than a timeslice. If by luck its timeslice ends and B is scheduled, B may spin for a timeslice, it&#8217;s true. But B should be no busier than A. Thread A should be hustling while it has that lock.</p>
<p>peterix: Same as above. You&#8217;re right, they don&#8217;t play nice with schedulers. The idea is not to let it get to the scheduler most of the time. Grab and release the lock quickly so that the holding thread rarely gets descheduled while holding it. Don&#8217;t make syscalls which could deschedule the thread while holding it.</p>
<p>If the contending threads are constantly rapidly taking and releasing the spinlock, such that they&#8217;re likely to be interrupted while holding the lock, you&#8217;re spending too much time on synchronization anyway. It would be better to batch the communications somehow, then use mutexes that can safely be held for longer times. And maybe use spinlocks for a short, uncommon metadata transfers outside the batch.
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('27752','Mike'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('27752','Mike','Ngargh! This post is ancient but the commenters compel me to reply!\r\n\r\nThe point of using spinlocks is never to hold them very long, e.g. for modifying a small bit of memory like Alex said. Then release them again. You shouldn\'t even make system calls or anything while holding them. They are extremely hot potatoes that you do not want to hold.\r\n\r\nCyril: After thread A gets the spinlock, it should also be at 100% cpu until it releases it again. It should also hold it for very little time; much less than a timeslice. If by luck its timeslice ends and B is scheduled, B may spin for a timeslice, it\'s true. But B should be no busier than A. Thread A should be hustling while it has that lock.\r\n\r\npeterix: Same as above. You\'re right, they don\'t play nice with schedulers. The idea is not to let it get to the scheduler most of the time. Grab and release the lock quickly so that the holding thread rarely gets descheduled while holding it. Don\'t make syscalls which could deschedule the thread while holding it.\r\n\r\nIf the contending threads are constantly rapidly taking and releasing the spinlock, such that they\'re likely to be interrupted while holding the lock, you\'re spending too much time on synchronization anyway. It would be better to batch the communications somehow, then use mutexes that can safely be held for longer times. And maybe use spinlocks for a short, uncommon metadata transfers outside the batch.'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: 簡單而有趣的雙執行緒同步問題 &#171; Hitripod</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-24757</link>
		<dc:creator>簡單而有趣的雙執行緒同步問題 &#171; Hitripod</dc:creator>
		<pubDate>Tue, 11 Jan 2011 17:25:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-24757</guid>
		<description></description>
		<content:encoded><![CDATA[<p>[...] 2, [...]
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('24757','&ccedil;&deg;&iexcl;&aring;&reg;&egrave;&aelig;&egrave;&para;&pound;&ccedil;&eacute;&aring;&middot;&egrave;&iexcl;&ccedil;&middot;&aring;&aelig;&shy;&yen;&aring;&eacute;&iexcl; &amp;laquo; Hitripod'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('24757','&ccedil;&deg;&iexcl;&aring;&reg;&egrave;&aelig;&egrave;&para;&pound;&ccedil;&eacute;&aring;&middot;&egrave;&iexcl;&ccedil;&middot;&aring;&aelig;&shy;&yen;&aring;&eacute;&iexcl; &amp;laquo; Hitripod','&amp;#91;...&amp;#93; 2, &amp;#91;...&amp;#93;'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterix</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-23806</link>
		<dc:creator>peterix</dc:creator>
		<pubDate>Thu, 18 Mar 2010 22:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-23806</guid>
		<description>When you do IPC (setting PTHREAD_PROCESS_SHARED and putting the spinlock into shared memory to control access to some shared resource), the scheduler becomes your enemy.

Say you have a dual-core CPU and two processes use a spinlock to regulate access to a shared memory segment (sharing an int for example). Then comes a third CPU-hungry process and suddenly your performance goes to hell with latency going from microseconds to seconds.

So, beware. Spinlocks are of limited usefulness even on SMP machines. The kernel can afford that, because it has no &#039;completely fair&#039; scheduler to deal with :/&lt;div class=&quot;comment-remix-meta&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;replyto&quot; onclick=&quot;replyto(&#039;23806&#039;,&#039;peterix&#039;); return false;&quot;&gt;Reply&lt;/a&gt;  - &lt;a href=&quot;#&quot; class=&quot;quote&quot; onclick=&quot;quote(&#039;23806&#039;,&#039;peterix&#039;,&#039;When you do IPC (setting PTHREAD_PROCESS_SHARED and putting the spinlock into shared memory to control access to some shared resource), the scheduler becomes your enemy.\r\n\r\nSay you have a dual-core CPU and two processes use a spinlock to regulate access to a shared memory segment (sharing an int for example). Then comes a third CPU-hungry process and suddenly your performance goes to hell with latency going from microseconds to seconds.\r\n\r\nSo, beware. Spinlocks are of limited usefulness even on SMP machines. The kernel can afford that, because it has no \&#039;completely fair\&#039; scheduler to deal with :\/&#039;); return false;&quot;&gt;Quote&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>When you do IPC (setting PTHREAD_PROCESS_SHARED and putting the spinlock into shared memory to control access to some shared resource), the scheduler becomes your enemy.</p>
<p>Say you have a dual-core CPU and two processes use a spinlock to regulate access to a shared memory segment (sharing an int for example). Then comes a third CPU-hungry process and suddenly your performance goes to hell with latency going from microseconds to seconds.</p>
<p>So, beware. Spinlocks are of limited usefulness even on SMP machines. The kernel can afford that, because it has no &#8216;completely fair&#8217; scheduler to deal with :/
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('23806','peterix'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('23806','peterix','When you do IPC (setting PTHREAD_PROCESS_SHARED and putting the spinlock into shared memory to control access to some shared resource), the scheduler becomes your enemy.\r\n\r\nSay you have a dual-core CPU and two processes use a spinlock to regulate access to a shared memory segment (sharing an int for example). Then comes a third CPU-hungry process and suddenly your performance goes to hell with latency going from microseconds to seconds.\r\n\r\nSo, beware. Spinlocks are of limited usefulness even on SMP machines. The kernel can afford that, because it has no \'completely fair\' scheduler to deal with :\/'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexander Sandler</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-23667</link>
		<dc:creator>Alexander Sandler</dc:creator>
		<pubDate>Sat, 16 Jan 2010 20:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-23667</guid>
		<description></description>
		<content:encoded><![CDATA[<p><a href="#comment-23657' rel="nofollow">@FrÃ©dÃ©rik</a><br />
What test code?
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('23667','Alexander Sandler'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('23667','Alexander Sandler','&lt;a href=\'#comment-23657\' rel=\&quot;nofollow\&quot;&gt;@Fr&Atilde;&Acirc;&copy;d&Atilde;&Acirc;&copy;rik&lt;\/a&gt;\r\nWhat test code?'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frédérik</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-23657</link>
		<dc:creator>Frédérik</dc:creator>
		<pubDate>Fri, 15 Jan 2010 13:50:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-23657</guid>
		<description>Well I have run the test code on a 2 dual core CPU,so 4 cores (dual Intel Xeon E5420  @2.50GHz) running linux 2.6.22. Then the result is that Mutex are faster.

With mutex:
  Consumer TID 14146
  Consumer TID 14147
  Result - 4.739984

With Spinlock:
  Consumer TID 14464
  Consumer TID 14465
  Result - 5.785072

With -O2 optimisation flags the difference is reduced but mutex are still faster.&lt;div class=&quot;comment-remix-meta&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;replyto&quot; onclick=&quot;replyto(&#039;23657&#039;,&#039;Fr&#195;&#169;d&#195;&#169;rik&#039;); return false;&quot;&gt;Reply&lt;/a&gt;  - &lt;a href=&quot;#&quot; class=&quot;quote&quot; onclick=&quot;quote(&#039;23657&#039;,&#039;Fr&#195;&#169;d&#195;&#169;rik&#039;,&#039;Well I have run the test code on a 2 dual core CPU,so 4 cores (dual Intel Xeon E5420  @2.50GHz) running linux 2.6.22. Then the result is that Mutex are faster.\r\n\r\nWith mutex:\r\n  Consumer TID 14146\r\n  Consumer TID 14147\r\n  Result - 4.739984\r\n\r\nWith Spinlock:\r\n  Consumer TID 14464\r\n  Consumer TID 14465\r\n  Result - 5.785072\r\n\r\nWith -O2 optimisation flags the difference is reduced but mutex are still faster.&#039;); return false;&quot;&gt;Quote&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>Well I have run the test code on a 2 dual core CPU,so 4 cores (dual Intel Xeon E5420  @2.50GHz) running linux 2.6.22. Then the result is that Mutex are faster.</p>
<p>With mutex:<br />
  Consumer TID 14146<br />
  Consumer TID 14147<br />
  Result &#8211; 4.739984</p>
<p>With Spinlock:<br />
  Consumer TID 14464<br />
  Consumer TID 14465<br />
  Result &#8211; 5.785072</p>
<p>With -O2 optimisation flags the difference is reduced but mutex are still faster.
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('23657','Fr&Atilde;&copy;d&Atilde;&copy;rik'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('23657','Fr&Atilde;&copy;d&Atilde;&copy;rik','Well I have run the test code on a 2 dual core CPU,so 4 cores (dual Intel Xeon E5420  @2.50GHz) running linux 2.6.22. Then the result is that Mutex are faster.\r\n\r\nWith mutex:\r\n  Consumer TID 14146\r\n  Consumer TID 14147\r\n  Result - 4.739984\r\n\r\nWith Spinlock:\r\n  Consumer TID 14464\r\n  Consumer TID 14465\r\n  Result - 5.785072\r\n\r\nWith -O2 optimisation flags the difference is reduced but mutex are still faster.'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cyril</title>
		<link>http://www.alexonlinux.com/pthread-spinlocks/comment-page-1#comment-23451</link>
		<dc:creator>Cyril</dc:creator>
		<pubDate>Thu, 20 Aug 2009 14:38:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674#comment-23451</guid>
		<description>Well, this is wrong. 
It&#039;s only true for SMP machine (the one with REAL multiple processor, and not hyperthreading).

If you only have a single CPU (or HT), then whatever you do, only one thread at a time can actually modify a memory value. 
So while you&#039;re spinlocking, you&#039;re simply consuming CPU power for nothing, as, actually get the spinlock, you&#039;ll have to wait for a kernel based context switch, and hoping you&#039;re not elected back.

This means that, depending on kernel scheduler :
Thread A               
take spin lock
write value to protected int

Thread B
want to take spin lock
(use 100% cpu for the whole thread time slice)
 Here, the kernel can decide to reelect thread B, as it&#039;s the thread doing the more work
Thread B
want to take spin lock
(use 100% cpu for the whole thread time slice)
 and so on...

This shame is very very dangerous, that&#039;s why in linux kernel, spinlock is a macro (not a function) that get disabled on SUP machine.&lt;div class=&quot;comment-remix-meta&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;replyto&quot; onclick=&quot;replyto(&#039;23451&#039;,&#039;Cyril&#039;); return false;&quot;&gt;Reply&lt;/a&gt;  - &lt;a href=&quot;#&quot; class=&quot;quote&quot; onclick=&quot;quote(&#039;23451&#039;,&#039;Cyril&#039;,&#039;Well, this is wrong. \r\nIt\&#039;s only true for SMP machine (the one with REAL multiple processor, and not hyperthreading).\r\n\r\nIf you only have a single CPU (or HT), then whatever you do, only one thread at a time can actually modify a memory value. \r\nSo while you\&#039;re spinlocking, you\&#039;re simply consuming CPU power for nothing, as, actually get the spinlock, you\&#039;ll have to wait for a kernel based context switch, and hoping you\&#039;re not elected back.\r\n\r\nThis means that, depending on kernel scheduler :\r\nThread A               \r\ntake spin lock\r\nwrite value to protected int\r\n\r\nThread B\r\nwant to take spin lock\r\n(use 100% cpu for the whole thread time slice)\r\n Here, the kernel can decide to reelect thread B, as it\&#039;s the thread doing the more work\r\nThread B\r\nwant to take spin lock\r\n(use 100% cpu for the whole thread time slice)\r\n and so on...\r\n\r\nThis shame is very very dangerous, that\&#039;s why in linux kernel, spinlock is a macro (not a function) that get disabled on SUP machine.&#039;); return false;&quot;&gt;Quote&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>Well, this is wrong.<br />
It&#8217;s only true for SMP machine (the one with REAL multiple processor, and not hyperthreading).</p>
<p>If you only have a single CPU (or HT), then whatever you do, only one thread at a time can actually modify a memory value.<br />
So while you&#8217;re spinlocking, you&#8217;re simply consuming CPU power for nothing, as, actually get the spinlock, you&#8217;ll have to wait for a kernel based context switch, and hoping you&#8217;re not elected back.</p>
<p>This means that, depending on kernel scheduler :<br />
Thread A<br />
take spin lock<br />
write value to protected int</p>
<p>Thread B<br />
want to take spin lock<br />
(use 100% cpu for the whole thread time slice)<br />
 Here, the kernel can decide to reelect thread B, as it&#8217;s the thread doing the more work<br />
Thread B<br />
want to take spin lock<br />
(use 100% cpu for the whole thread time slice)<br />
 and so on&#8230;</p>
<p>This shame is very very dangerous, that&#8217;s why in linux kernel, spinlock is a macro (not a function) that get disabled on SUP machine.
<div class="comment-remix-meta"><a href="#" class="replyto" onclick="replyto('23451','Cyril'); return false;">Reply</a>  &#8211; <a href="#" class="quote" onclick="quote('23451','Cyril','Well, this is wrong. \r\nIt\'s only true for SMP machine (the one with REAL multiple processor, and not hyperthreading).\r\n\r\nIf you only have a single CPU (or HT), then whatever you do, only one thread at a time can actually modify a memory value. \r\nSo while you\'re spinlocking, you\'re simply consuming CPU power for nothing, as, actually get the spinlock, you\'ll have to wait for a kernel based context switch, and hoping you\'re not elected back.\r\n\r\nThis means that, depending on kernel scheduler :\r\nThread A               \r\ntake spin lock\r\nwrite value to protected int\r\n\r\nThread B\r\nwant to take spin lock\r\n(use 100% cpu for the whole thread time slice)\r\n Here, the kernel can decide to reelect thread B, as it\'s the thread doing the more work\r\nThread B\r\nwant to take spin lock\r\n(use 100% cpu for the whole thread time slice)\r\n and so on...\r\n\r\nThis shame is very very dangerous, that\'s why in linux kernel, spinlock is a macro (not a function) that get disabled on SUP machine.'); return false;">Quote</a></div>
]]></content:encoded>
	</item>
</channel>
</rss>

