Urban Hafner : Tag thread, everything about thread http://bettong.net/tag/thread.rss en-us 40 CTM: Exercise 4-6 <h3>The problem</h3> <p><em>Thread scheduling.</em> Section 4.8.3.2 shows how to skip over already-calculated elements of a stream. If we use this technique to sum the elements of the integer stream in section 4.3.1, the result is much smaller than 11249925000, which is the sum of the integers in the stream. Why is it so much smaller? Explain this result in terms of thread scheduling.</p> <h3>My solution</h3> <p>What probably happens is that generating another element in the production thread is so fast that more than one ca be generated in one time slice. When the consumer thread restarts in the next slice it only takes the last element computed, so it loses some (or many) elements.</p> Sun, 06 Nov 2005 16:39:50 +0000 urn:uuid:bdb6299a-55ff-4033-bca6-749d958562a0 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/06/ctm-exercise-4-6#comments CTM exercise book mozart oz thread scheduling http://bettong.net/2005/11/06/ctm-exercise-4-6 CTM: Exercise 4-2 <h3>The problem</h3> <p><em>Threads and garbage collection.</em> This exercise examines how garbage collection behaves with threads and dataflow variables. Consider the following program:</p> <div class="typocode"><pre><code class="typocode_default ">proc {B _} {Wait _} end proc {A} Collectible={NewDictionary} in {B Collectible} end</code></pre></div> <p>After the call <code>{A}</code> is done, will <code>Collectible</code> become garbage? That is, will the memory occupied by <code>Collectible</code> be recovered? Give an answer by thinking about the semantics. Verify that the Mozart system behaves in this way.</p> <h3>My solution</h3> <p>At first I was a bit confused by what is meant with after the call is done and how <code>_</code> behaves. But then I understood. <code>Wait</code> waits until its argument becomes determined. But when you use <code>_</code> as an argument that can never happen because it creates a new unbound variable.</p> <p>This I why I think <code>Collectible</code> can&#8217;t be garbage collected. It can&#8217;t be done because the call to <code>A</code> isn&#8217;t finished yet, because the call to <code>B</code> isn&#8217;t finished.</p> Wed, 02 Nov 2005 16:12:33 +0000 urn:uuid:e8adc799-89f8-4bf4-9f02-2969e213d2b2 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-2#comments CTM book exercise thread garbage collection mozart oz http://bettong.net/2005/11/02/ctm-exercise-4-2 CTM: Exercise 4-1 <h3>The problem</h3> <p><em>Thread semantics.</em> Consider the following variation of the statement used in section 4.1.3 to illustrate thread semantics:</p> <div class="typocode"><table class="typocode_linenumber"><tr><td class="lineno"> <pre> 1 2 3 4 5 </pre> </td><td width="100%"><pre><code class="typocode_default ">local B in thread B=true end thread B=false end if B then {Browse yes} end end</code></pre></td></tr></table></div> <p>For this exercise, do the following:</p> <p><b>(a)</b> Enumerate all possible executions of this statement.</p> <p><b>(b)</b> Some of these executions cause the program to terminate abnormally. Make a small change to the program to avoid these abnormal terminations.</p> <h3>My solution</h3> <p><b>(a)</b> Line 4 has to wait until <code>B</code> becomes bound, so either line 2 or 3 has to execute first. The following combinations are possible:</p> <ul> <li>2,3,4</li> <li>2,4,3</li> <li>3,2,4</li> <li>3,4,2</li> </ul> <p><b>(b)</b> Actually <em>all</em> executions terminate abnormally because all of the eventually try to unify <code>true</code> and <code>false</code>. So what we have to check in the thread in line 2 and 3 is if <code>B</code> is already bound. This leads to the following program:</p> <div class="typocode"><pre><code class="typocode_default ">local B in thread if {IsDet B} then skip else B=true end end thread if {IsDet B} then skip else B=false end end if B then {Browse yes} end end</code></pre></div> Wed, 02 Nov 2005 16:10:56 +0000 urn:uuid:af91283c-c753-4e39-a300-ab391000a458 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-1#comments CTM book exercise mozart oz thread semantics http://bettong.net/2005/11/02/ctm-exercise-4-1