<?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: Immutable Trees and Threading Evil &#8211; Part 2</title>
	<atom:link href="http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2/feed" rel="self" type="application/rss+xml" />
	<link>http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2</link>
	<description>Adventures in Programming</description>
	<lastBuildDate>Wed, 04 Jan 2012 02:38:56 -0800</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
	<item>
		<title>By: David</title>
		<link>http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2/comment-page-1#comment-17</link>
		<dc:creator>David</dc:creator>
		<pubDate>Thu, 24 Jan 2008 19:27:24 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2#comment-17</guid>
		<description>I don&#039;t know know enough about Java to comment on your post from that perspective. However, since the post was inspired from another post about .NET, I thought I would point out that what you described is impossible in .NET. Your scenario relies on the fact that once the memory for an object has been allocated, other objects can reference that object, even if it has not been constructed yet. In .NET this is not possible. There can never be an optimization such that an object is referenced before it is fully constructed. So this would never be a problem, and Eric&#039;s original statement on his blog post holds.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know know enough about Java to comment on your post from that perspective. However, since the post was inspired from another post about .NET, I thought I would point out that what you described is impossible in .NET. Your scenario relies on the fact that once the memory for an object has been allocated, other objects can reference that object, even if it has not been constructed yet. In .NET this is not possible. There can never be an optimization such that an object is referenced before it is fully constructed. So this would never be a problem, and Eric&#8217;s original statement on his blog post holds.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian McKeever</title>
		<link>http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2/comment-page-1#comment-14</link>
		<dc:creator>Brian McKeever</dc:creator>
		<pubDate>Thu, 20 Dec 2007 07:10:46 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2#comment-14</guid>
		<description>I agree that modifying shared resources without locking can lead to trouble.  But what I was getting at was that if the instruction reordering you suggested were allowed, then the problem would be visible from within a single thread (if put and take used the same index).  Right?

And as you say, the model doesn&#039;t allow this, so it follows that the instruction reordering is not allowed.  That was the contradiction I was trying to point out.</description>
		<content:encoded><![CDATA[<p>I agree that modifying shared resources without locking can lead to trouble.  But what I was getting at was that if the instruction reordering you suggested were allowed, then the problem would be visible from within a single thread (if put and take used the same index).  Right?</p>
<p>And as you say, the model doesn&#8217;t allow this, so it follows that the instruction reordering is not allowed.  That was the contradiction I was trying to point out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mcherm</title>
		<link>http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2/comment-page-1#comment-12</link>
		<dc:creator>mcherm</dc:creator>
		<pubDate>Thu, 20 Dec 2007 02:21:19 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2#comment-12</guid>
		<description>Brian:

(1) No, it couldn&#039;t happen with a single thread, because the Java Memory Model (which governs what kind of reordering the compiler can do) specifically states that any re-orderings must not be visible to the SAME thread, but allows them to be visible to OTHER threads.

(2) Whether it&#039;s one line or multiple lines doesn&#039;t change things at all. In the end, the compiler is optimizing pretty much the same code. (All optimizing compilers are smart enough to realize when a variable can&#039;t be seen outside of the method.)

(3) I&#039;m not quite sure what you mean by &quot;strict order of evaluation&quot;.

(4) No, my code never printed that. I have two reasons for that. First, I&#039;m not using both processors (when running it uses only 50% CPU) and these things won&#039;t typically show up when running in time slices on one CPU because the crucial &quot;two things happening at about the same time&quot; isn&#039;t occurring. Secondly, this example isn&#039;t particularly realistic: the optimization I describe is *allowed* but is probably not *sensible* in this case. But similar (and equally puzzling) things DO occur in real programs.</description>
		<content:encoded><![CDATA[<p>Brian:</p>
<p>(1) No, it couldn&#8217;t happen with a single thread, because the Java Memory Model (which governs what kind of reordering the compiler can do) specifically states that any re-orderings must not be visible to the SAME thread, but allows them to be visible to OTHER threads.</p>
<p>(2) Whether it&#8217;s one line or multiple lines doesn&#8217;t change things at all. In the end, the compiler is optimizing pretty much the same code. (All optimizing compilers are smart enough to realize when a variable can&#8217;t be seen outside of the method.)</p>
<p>(3) I&#8217;m not quite sure what you mean by &#8220;strict order of evaluation&#8221;.</p>
<p>(4) No, my code never printed that. I have two reasons for that. First, I&#8217;m not using both processors (when running it uses only 50% CPU) and these things won&#8217;t typically show up when running in time slices on one CPU because the crucial &#8220;two things happening at about the same time&#8221; isn&#8217;t occurring. Secondly, this example isn&#8217;t particularly realistic: the optimization I describe is *allowed* but is probably not *sensible* in this case. But similar (and equally puzzling) things DO occur in real programs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian McKeever</title>
		<link>http://mcherm.com/permalinks/1/immutable-trees-and-threading-evil-part-2/comment-page-1#comment-11</link>
		<dc:creator>Brian McKeever</dc:creator>
		<pubDate>Thu, 20 Dec 2007 01:19:39 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2#comment-11</guid>
		<description>If your explanation were correct, you don&#039;t need multiple threads to encounter the problem, do you?  Your &quot;other possible ordering&quot; with just a single thread could result in a node being its own parent (if one of the gets uses the same index as the put), right?

Supposing you rewrote the code to be a single statement:
nodePool.putNode(new TreeNode(nodePool.takeRandomNode(), nodePool.takeRandomNode()));

How would you reconcile the possibility of cyclic trees with the strict order of evaluation that Java applies to its statements?

Did your code ever print &quot;We Found A Loop!!&quot;?</description>
		<content:encoded><![CDATA[<p>If your explanation were correct, you don&#8217;t need multiple threads to encounter the problem, do you?  Your &#8220;other possible ordering&#8221; with just a single thread could result in a node being its own parent (if one of the gets uses the same index as the put), right?</p>
<p>Supposing you rewrote the code to be a single statement:<br />
nodePool.putNode(new TreeNode(nodePool.takeRandomNode(), nodePool.takeRandomNode()));</p>
<p>How would you reconcile the possibility of cyclic trees with the strict order of evaluation that Java applies to its statements?</p>
<p>Did your code ever print &#8220;We Found A Loop!!&#8221;?</p>
]]></content:encoded>
	</item>
</channel>
</rss>


