<?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: Volatile: C Keyword Myths Dispelled</title>
	<atom:link href="http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Thu, 18 Feb 2010 05:11:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: mjordan</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-3490</link>
		<dc:creator>mjordan</dc:creator>
		<pubDate>Thu, 18 Feb 2010 05:11:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-3490</guid>
		<description>There is another keyword I can&#039;t understand well: restrict for pointers.</description>
		<content:encoded><![CDATA[<p>There is another keyword I can&#8217;t understand well: restrict for pointers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RAHUL YADAV</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-3454</link>
		<dc:creator>RAHUL YADAV</dc:creator>
		<pubDate>Mon, 28 Dec 2009 19:55:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-3454</guid>
		<description>volatile is related to optimization which impacts the hardwar related access of variables. WHile multi threading requires exclusive operations (meaning no other thread is alowed to access the critical section). These both may be required at times, but totally un related.
Suppose, a program is multithreaded but it uses a variable which stores some hardware address, and 2 threads accesses that variable. Then, too variable can be declared as volatile and can be protected with mutex.</description>
		<content:encoded><![CDATA[<p>volatile is related to optimization which impacts the hardwar related access of variables. WHile multi threading requires exclusive operations (meaning no other thread is alowed to access the critical section). These both may be required at times, but totally un related.<br />
Suppose, a program is multithreaded but it uses a variable which stores some hardware address, and 2 threads accesses that variable. Then, too variable can be declared as volatile and can be protected with mutex.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GregK</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-1171</link>
		<dc:creator>GregK</dc:creator>
		<pubDate>Mon, 20 Apr 2009 15:25:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-1171</guid>
		<description>Author completely misunderstood &#039;volatile&#039; and multithreading. Multithread program completely do not need atomicly access! what for? What if platform do not support atomic access? Atomic access is also not  specified in Ansi C 89 (i do not know what about C99). Mutual exclusion is enough to secure multithreading access.</description>
		<content:encoded><![CDATA[<p>Author completely misunderstood &#8216;volatile&#8217; and multithreading. Multithread program completely do not need atomicly access! what for? What if platform do not support atomic access? Atomic access is also not  specified in Ansi C 89 (i do not know what about C99). Mutual exclusion is enough to secure multithreading access.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-322</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Tue, 10 Mar 2009 17:49:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-322</guid>
		<description>Derek, this is interesting. BTW I have used gcc 3.x and gcc 4.2.4 and both give similar results (at O1 as well as O2). Both these versions output different code but do move around the statement that stores 2 to j. I guess that is all the more reason to not use volatiles with this particular &quot;feature&quot; in mind as many compilers haven&#039;t got it right ;)</description>
		<content:encoded><![CDATA[<p>Derek, this is interesting. BTW I have used gcc 3.x and gcc 4.2.4 and both give similar results (at O1 as well as O2). Both these versions output different code but do move around the statement that stores 2 to j. I guess that is all the more reason to not use volatiles with this particular &#8220;feature&#8221; in mind as many compilers haven&#8217;t got it right <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derek Jones</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-321</link>
		<dc:creator>Derek Jones</dc:creator>
		<pubDate>Tue, 10 Mar 2009 16:28:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-321</guid>
		<description>@Shantanu: A compiler that performs the optimization you give does not conform to the requirements contained in the C Standard.

Two sentences does from the previously given quote:
http://c0x.coding-guidelines.com/6.7.3.html Sentence 1484
&quot;Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine ...&quot;</description>
		<content:encoded><![CDATA[<p>@Shantanu: A compiler that performs the optimization you give does not conform to the requirements contained in the C Standard.</p>
<p>Two sentences does from the previously given quote:<br />
<a href="http://c0x.coding-guidelines.com/6.7.3.html" rel="nofollow">http://c0x.coding-guidelines.com/6.7.3.html</a> Sentence 1484<br />
&#8220;Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine &#8230;&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-316</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Tue, 10 Mar 2009 04:57:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-316</guid>
		<description>@Derek: Not necessarily when mixed with non-volatiles. Consider the following example:
&lt;code&gt;
int main()
{
  int i = 2;
  volatile int j = 1;
  while ( i )
  {
    printf(&quot;%d\n&quot;, i);
    --i;
    j = 2;
  }
  return 0;
}&lt;/code&gt;
If you were to compile it with optimization on, most compilers will move the &quot;j=2&quot; before the loop.</description>
		<content:encoded><![CDATA[<p>@Derek: Not necessarily when mixed with non-volatiles. Consider the following example:<br />
<code><br />
int main()<br />
{<br />
  int i = 2;<br />
  volatile int j = 1;<br />
  while ( i )<br />
  {<br />
    printf("%d\n", i);<br />
    --i;<br />
    j = 2;<br />
  }<br />
  return 0;<br />
}</code><br />
If you were to compile it with optimization on, most compilers will move the &#8220;j=2&#8243; before the loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derek Jones</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-310</link>
		<dc:creator>Derek Jones</dc:creator>
		<pubDate>Mon, 09 Mar 2009 12:20:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-310</guid>
		<description>Declaring objects to be volatile does cause the order of access to be fixed (there may be cases where multiple orders of access are permitted by the language, but that is another issue):
&quot;Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3.&quot;

http://c0x.coding-guidelines.com/6.7.3.html Sentence 1482

It is true that: All shared data in multi-threaded programs must be declared volatile.  Atomicity is an unrelated issue and covered by the type sig_atomic_t

It seems that developers are not the only people that sometimes have trouble with volatile, compilers often get it wrong as well
http://shape-of-code.coding-guidelines.com/2009/03/volatile-handling-sometimes-overly-volatile/</description>
		<content:encoded><![CDATA[<p>Declaring objects to be volatile does cause the order of access to be fixed (there may be cases where multiple orders of access are permitted by the language, but that is another issue):<br />
&#8220;Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3.&#8221;</p>
<p><a href="http://c0x.coding-guidelines.com/6.7.3.html" rel="nofollow">http://c0x.coding-guidelines.com/6.7.3.html</a> Sentence 1482</p>
<p>It is true that: All shared data in multi-threaded programs must be declared volatile.  Atomicity is an unrelated issue and covered by the type sig_atomic_t</p>
<p>It seems that developers are not the only people that sometimes have trouble with volatile, compilers often get it wrong as well<br />
<a href="http://shape-of-code.coding-guidelines.com/2009/03/volatile-handling-sometimes-overly-volatile/" rel="nofollow">http://shape-of-code.coding-guidelines.com/2009/03/volatile-handling-sometimes-overly-volatile/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackjeff</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-204</link>
		<dc:creator>jackjeff</dc:creator>
		<pubDate>Wed, 25 Feb 2009 10:59:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-204</guid>
		<description>@harringf  You totally misunderstood my point... :)

I totally agree that volatile as multithreaded synchronization is just plainly wrong. It&#039;s wrong because automicity is not garanteed and it cannot be used as a synchronization primitive safely. But it&#039;s not wrong because of speed.

Volatile, despite being totally utterly wrong, is probably faster than most of the synchronization primitives offered by modern operating system. The latter are likely to trigger system calls and/or require memory synchronization instructions. Proper synchronization primitives are expensive.</description>
		<content:encoded><![CDATA[<p>@harringf  You totally misunderstood my point&#8230; <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I totally agree that volatile as multithreaded synchronization is just plainly wrong. It&#8217;s wrong because automicity is not garanteed and it cannot be used as a synchronization primitive safely. But it&#8217;s not wrong because of speed.</p>
<p>Volatile, despite being totally utterly wrong, is probably faster than most of the synchronization primitives offered by modern operating system. The latter are likely to trigger system calls and/or require memory synchronization instructions. Proper synchronization primitives are expensive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-202</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Wed, 25 Feb 2009 09:00:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-202</guid>
		<description>Jackjeff, yes sync primitives and memory barriers might be slower but the point is that they are anyways needed even if you do use volatile. The point was that if you use these semantics correctly, you don&#039;t need volatile at all, using volatile in these cases will not make your program any more safe but just slow it down further.</description>
		<content:encoded><![CDATA[<p>Jackjeff, yes sync primitives and memory barriers might be slower but the point is that they are anyways needed even if you do use volatile. The point was that if you use these semantics correctly, you don&#8217;t need volatile at all, using volatile in these cases will not make your program any more safe but just slow it down further.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffreyb</title>
		<link>http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html#comment-201</link>
		<dc:creator>jeffreyb</dc:creator>
		<pubDate>Wed, 25 Feb 2009 07:47:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=33#comment-201</guid>
		<description>@Caleb: I think jackjeff is talking about the actual builtins used to implement synchronization primitives (e.g. compare and swap, etc) which are usually 100-300 times slower than their non-atomic counterparts.</description>
		<content:encoded><![CDATA[<p>@Caleb: I think jackjeff is talking about the actual builtins used to implement synchronization primitives (e.g. compare and swap, etc) which are usually 100-300 times slower than their non-atomic counterparts.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
