<?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: Race condition in C wrapper of mutex class</title>
	<atom:link href="http://www.jeffhung.net/blog/articles/jeffhung/994/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeffhung.net/blog/articles/jeffhung/994/</link>
	<description>(My smile insists of having nose. :-)</description>
	<lastBuildDate>Thu, 03 Nov 2011 00:43:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2-alpha</generator>
	<item>
		<title>By: fr3@K</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81968</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Wed, 26 Dec 2007 09:41:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81968</guid>
		<description>Hi Jeff,

你說的對, 我上一個 comment 提到的等效方法看來 pure C 是不支持的.

另, 在&lt;a href=&quot;http://www.jeffhung.net/blog/articles/jeffhung/994/#footnote-16-994&quot; rel=&quot;nofollow&quot;&gt;這篇的第 16 個 footnote&lt;/a&gt;, 你提到 pthread_mutex_init() 與這篇的 mutex_init() 除了 pthread_mutexattr_t 參數之外是一樣的. 提醒一下, 雖然 interface 是一致的, 但語意確不相同. Quote from &lt;a href=&quot;http://linux.die.net/man/3/pthread_mutex_init&quot; rel=&quot;nofollow&quot;&gt;man 3 pthread_mutex_init&lt;/a&gt;:
&lt;blockquote&gt;
Attempting to initialize an already initialized mutex results in undefined behavior.
&lt;/blockquote&gt;</description>
		<content:encoded><![CDATA[<p>Hi Jeff,</p>
<p>你說的對, 我上一個 comment 提到的等效方法看來 pure C 是不支持的.</p>
<p>另, 在<a href="http://www.jeffhung.net/blog/articles/jeffhung/994/#footnote-16-994" rel="nofollow">這篇的第 16 個 footnote</a>, 你提到 pthread_mutex_init() 與這篇的 mutex_init() 除了 pthread_mutexattr_t 參數之外是一樣的. 提醒一下, 雖然 interface 是一致的, 但語意確不相同. Quote from <a href="http://linux.die.net/man/3/pthread_mutex_init" rel="nofollow">man 3 pthread_mutex_init</a>:</p>
<blockquote><p>
Attempting to initialize an already initialized mutex results in undefined behavior.
</p></blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffhung</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81951</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Wed, 26 Dec 2007 01:39:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81951</guid>
		<description>&lt;p&gt;fr3@K,&lt;/p&gt;
&lt;p&gt;無須致歉啊。:-)&lt;/p&gt;
&lt;p&gt;我有印象 static variable 的 initialization 不能夠呼叫 function。查了一下 C99，6.7.8.4 這麼說：&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;All the expressions in an initializer for an object that has static storage duration shall be
constant expressions or string literals.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;因為 mutex_create() 是 C 的 API，可能被純 C 的程式呼叫，因此這一行是違反 standard 的：&lt;/p&gt;
&lt;pre&gt;
static mutex_t* s_serial_mutex = mutex_create();
&lt;/pre&gt;
&lt;p&gt;雖然現在的 compiler 多是 C/C++ 混合版，沒那麼嚴格。&lt;/p&gt;
&lt;p&gt;另外，我認為 API 的設計，最好是能夠「堵死」所有可能的出錯寫法。所以，mutex_init() 會比 mutex_create() 好。&lt;/p&gt;
&lt;p&gt;至於怎樣應用 OS 提供的 API 以確保 thread-safe-mutex-initialization，您提得這些都是很有效的辦法，頗值得再細究一番。&lt;/p&gt;
&lt;p&gt;Jeff Hung&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>fr3@K,</p>
<p>無須致歉啊。:-)</p>
<p>我有印象 static variable 的 initialization 不能夠呼叫 function。查了一下 C99，6.7.8.4 這麼說：</p>
<blockquote>
<p>All the expressions in an initializer for an object that has static storage duration shall be<br />
constant expressions or string literals.</p>
</blockquote>
<p>因為 mutex_create() 是 C 的 API，可能被純 C 的程式呼叫，因此這一行是違反 standard 的：</p>
<pre>
static mutex_t* s_serial_mutex = mutex_create();
</pre>
<p>雖然現在的 compiler 多是 C/C++ 混合版，沒那麼嚴格。</p>
<p>另外，我認為 API 的設計，最好是能夠「堵死」所有可能的出錯寫法。所以，mutex_init() 會比 mutex_create() 好。</p>
<p>至於怎樣應用 OS 提供的 API 以確保 thread-safe-mutex-initialization，您提得這些都是很有效的辦法，頗值得再細究一番。</p>
<p>Jeff Hung</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81933</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Tue, 25 Dec 2007 17:33:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81933</guid>
		<description>如果原來的 client code, 也就是 get_serial_number 改成這樣:
&lt;code&gt;
unsigned int get_serial_number()
{
    static unsigned int s_serial = 0;
    static mutex_t*     s_serial_mutex = mutex_create();
    // ...
&lt;/code&gt;
其實就跟改進後的 API 實效差異不大. 都是依靠一個 file local static instance 來保護另外一個 instance.

如果 OS 是 unix-alike 或底層可用 pthread, 可以試試 &lt;a href=&quot;http://linux.die.net/man/3/pthread_once&quot; rel=&quot;nofollow&quot;&gt;pthread_once&lt;/a&gt; 或是以 &lt;a href=&quot;http://linux.die.net/man/3/pthread_mutex_init&quot; rel=&quot;nofollow&quot;&gt;PTHREAD_MUTEX_INITIALIZER&lt;/a&gt; 來啟始一個 file static 的 pthread_mutex_t 物件.

如果 OS 是 Windows, 可以嘗試以 &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms683614.aspx&quot; rel=&quot;nofollow&quot;&gt;InterlockedIncrement&lt;/a&gt; 等的原子運算去操作一個 file scope static 的 integer, 將該 integer 當作一個 spin lock 使用.

再次為之前的 comment 致歉.</description>
		<content:encoded><![CDATA[<p>如果原來的 client code, 也就是 get_serial_number 改成這樣:<br />
<code><br />
unsigned int get_serial_number()<br />
{<br />
    static unsigned int s_serial = 0;<br />
    static mutex_t*     s_serial_mutex = mutex_create();<br />
    // ...<br />
</code><br />
其實就跟改進後的 API 實效差異不大. 都是依靠一個 file local static instance 來保護另外一個 instance.</p>
<p>如果 OS 是 unix-alike 或底層可用 pthread, 可以試試 <a href="http://linux.die.net/man/3/pthread_once" rel="nofollow">pthread_once</a> 或是以 <a href="http://linux.die.net/man/3/pthread_mutex_init" rel="nofollow">PTHREAD_MUTEX_INITIALIZER</a> 來啟始一個 file static 的 pthread_mutex_t 物件.</p>
<p>如果 OS 是 Windows, 可以嘗試以 <a href="http://msdn2.microsoft.com/en-us/library/ms683614.aspx" rel="nofollow">InterlockedIncrement</a> 等的原子運算去操作一個 file scope static 的 integer, 將該 integer 當作一個 spin lock 使用.</p>
<p>再次為之前的 comment 致歉.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffhung</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81931</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Tue, 25 Dec 2007 16:03:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81931</guid>
		<description>&lt;p&gt;fr3@K,&lt;/p&gt;
&lt;p&gt;那最終還是要看 OS 提供的 API 的能力，如果 OS 提供的 API，沒辦法解決這個問題的話，那整件事情就多半無解了。&lt;/p&gt;
&lt;p&gt;不過話又說回來，如果不考慮「不經由我們的library而建立的thread」的話，大可以再建立thread之前，先確保這些該初始化過的mutex先初始好。&lt;/p&gt;
&lt;p&gt;Jeff Hung&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>fr3@K,</p>
<p>那最終還是要看 OS 提供的 API 的能力，如果 OS 提供的 API，沒辦法解決這個問題的話，那整件事情就多半無解了。</p>
<p>不過話又說回來，如果不考慮「不經由我們的library而建立的thread」的話，大可以再建立thread之前，先確保這些該初始化過的mutex先初始好。</p>
<p>Jeff Hung</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81909</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Tue, 25 Dec 2007 08:50:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81909</guid>
		<description>不好意思, 之前 comment 的時候完全搞錯. 問題跟進入 main() 與否一點關係也沒有. Sorry.

但 race condition 依然存在. 若有一個以上的 thread 呼叫 mutex_init(), 就有多個 thread 競爭同一個可能未初始化的 function static instance (a mutex, in this case) 的狀況.</description>
		<content:encoded><![CDATA[<p>不好意思, 之前 comment 的時候完全搞錯. 問題跟進入 main() 與否一點關係也沒有. Sorry.</p>
<p>但 race condition 依然存在. 若有一個以上的 thread 呼叫 mutex_init(), 就有多個 thread 競爭同一個可能未初始化的 function static instance (a mutex, in this case) 的狀況.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81214</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Sun, 16 Dec 2007 11:30:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81214</guid>
		<description>如果一個以上的 thread 在進入 main 之前使用 init_mutex 呢?</description>
		<content:encoded><![CDATA[<p>如果一個以上的 thread 在進入 main 之前使用 init_mutex 呢?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffhung</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81204</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Sun, 16 Dec 2007 06:20:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81204</guid>
		<description>fr3@K,

Static variable inside a function 保證在第一次進入該 function，走到該行時，被初始化(成 0 或呼叫 ctor)，而 mutex 的初始化，最終會呼叫到 OS 所提供的 API，所以只要 OS 所提供的 API 沒有問題，那 mxmx 的正確起始，應該就沒有問題。

Jeff Hung</description>
		<content:encoded><![CDATA[<p>fr3@K,</p>
<p>Static variable inside a function 保證在第一次進入該 function，走到該行時，被初始化(成 0 或呼叫 ctor)，而 mutex 的初始化，最終會呼叫到 OS 所提供的 API，所以只要 OS 所提供的 API 沒有問題，那 mxmx 的正確起始，應該就沒有問題。</p>
<p>Jeff Hung</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fr3@K</title>
		<link>http://www.jeffhung.net/blog/articles/jeffhung/994/comment-page-1/#comment-81201</link>
		<dc:creator>fr3@K</dc:creator>
		<pubDate>Sun, 16 Dec 2007 05:52:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.jeffhung.net/blog/articles/jeffhung/994/#comment-81201</guid>
		<description>我認為 init_mutex 還是不夠安全的, 至少以泛用 library 的角度來看.

例如 user 在進入 main 之前使用 init_mutex 來啟始一個 lifetime 是全局的 mutex, 那誰來確保 init_mutx 當中的 mxmx 的正確起始呢?

這個實作依然沒成功跳脫出 &quot;使用一個 global instance 去確保另一個 global instance 的正確啟始的矛盾&quot;.</description>
		<content:encoded><![CDATA[<p>我認為 init_mutex 還是不夠安全的, 至少以泛用 library 的角度來看.</p>
<p>例如 user 在進入 main 之前使用 init_mutex 來啟始一個 lifetime 是全局的 mutex, 那誰來確保 init_mutx 當中的 mxmx 的正確起始呢?</p>
<p>這個實作依然沒成功跳脫出 "使用一個 global instance 去確保另一個 global instance 的正確啟始的矛盾".</p>
]]></content:encoded>
	</item>
</channel>
</rss>

