<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>MLtonThread</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>MLtonThread</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">signature</span><span class="w"> </span><span class="n">MLTON_THREAD</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">structure</span><span class="w"> </span><span class="n">AtomicState</span><span class="p">:</span><span class="w"></span>
<span class="w">         </span><span class="k">sig</span><span class="w"></span>
<span class="w">            </span><span class="k">datatype</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">NonAtomic</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">Atomic</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">int</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">atomically</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">atomicBegin</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">atomicEnd</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">atomicState</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">AtomicState</span><span class="p">.</span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">structure</span><span class="w"> </span><span class="n">Runnable</span><span class="p">:</span><span class="w"></span>
<span class="w">         </span><span class="k">sig</span><span class="w"></span>
<span class="w">            </span><span class="k">type</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">atomicSwitch</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">new</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">prepend</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;b</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">prepare</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">switch</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p><span class="monospaced">MLton.Thread</span> provides access to MLton&#8217;s user-level thread
implementation (i.e. not OS-level threads).  Threads are lightweight
data structures that represent a paused computation.  Runnable threads
are threads that will begin or continue computing when <span class="monospaced">switch</span>-ed to.
<span class="monospaced">MLton.Thread</span> does not include a default scheduling mechanism, but it
can be used to implement both preemptive and non-preemptive threads.</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">type AtomicState.t</span>
</p>
<div class="paragraph"><p>the type of atomic states.</p></div>
</li>
<li>
<p>
<span class="monospaced">atomically f</span>
</p>
<div class="paragraph"><p>runs <span class="monospaced">f</span> in a critical section.</p></div>
</li>
<li>
<p>
<span class="monospaced">atomicBegin ()</span>
</p>
<div class="paragraph"><p>begins a critical section.</p></div>
</li>
<li>
<p>
<span class="monospaced">atomicEnd ()</span>
</p>
<div class="paragraph"><p>ends a critical section.</p></div>
</li>
<li>
<p>
<span class="monospaced">atomicState ()</span>
</p>
<div class="paragraph"><p>returns the current atomic state.</p></div>
</li>
<li>
<p>
<span class="monospaced">type Runnable.t</span>
</p>
<div class="paragraph"><p>the type of threads that can be resumed.</p></div>
</li>
<li>
<p>
<span class="monospaced">type 'a t</span>
</p>
<div class="paragraph"><p>the type of threads that expect a value of type <span class="monospaced">'a</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">atomicSwitch f</span>
</p>
<div class="paragraph"><p>like <span class="monospaced">switch</span>, but assumes an atomic calling context.  Upon
<span class="monospaced">switch</span>-ing back to the current thread, an implicit <span class="monospaced">atomicEnd</span> is
performed.</p></div>
</li>
<li>
<p>
<span class="monospaced">new f</span>
</p>
<div class="paragraph"><p>creates a new thread that, when run, applies <span class="monospaced">f</span> to the value given to
the thread.  <span class="monospaced">f</span> must terminate by `switch`ing to another thread or
exiting the process.</p></div>
</li>
<li>
<p>
<span class="monospaced">prepend (t, f)</span>
</p>
<div class="paragraph"><p>creates a new thread (destroying <span class="monospaced">t</span> in the process) that first
applies <span class="monospaced">f</span> to the value given to the thread and then continues with
<span class="monospaced">t</span>.  This is a constant time operation.</p></div>
</li>
<li>
<p>
<span class="monospaced">prepare (t, v)</span>
</p>
<div class="paragraph"><p>prepares a new runnable thread (destroying <span class="monospaced">t</span> in the process) that
will evaluate <span class="monospaced">t</span> on <span class="monospaced">v</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">switch f</span>
</p>
<div class="paragraph"><p>applies <span class="monospaced">f</span> to the current thread to get <span class="monospaced">rt</span>, and then start running
thread <span class="monospaced">rt</span>.  It is an error for <span class="monospaced">f</span> to perform another <span class="monospaced">switch</span>.  <span class="monospaced">f</span>
is guaranteed to run atomically.</p></div>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_of_non_preemptive_threads">Example of non-preemptive threads</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">Queue</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">new</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">enque</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">deque</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">option</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">datatype</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">ref</span><span class="p">,</span><span class="w"> </span><span class="n">back</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">ref</span><span class="p">}</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[],</span><span class="w"> </span><span class="n">back</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[]}</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">enque</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">back</span><span class="p">,</span><span class="w"> </span><span class="p">...},</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">back</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">!back</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">deque</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="p">,</span><span class="w"> </span><span class="n">back</span><span class="p">})</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="k">case</span><span class="w"> </span><span class="n">!front</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">            </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">!back</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                      </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>
<span class="w">                    </span><span class="p">|</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">let</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">rev</span><span class="w"> </span><span class="n">l</span><span class="w"></span>
<span class="w">                           </span><span class="k">in</span><span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                              </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="s">&quot;deque&quot;</span><span class="w"></span>
<span class="w">                            </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">back</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[];</span><span class="w"> </span><span class="n">front</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">                           </span><span class="k">end</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">front</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">Thread</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">exit</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">run</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">spawn</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">yield</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">MLton</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">Thread</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">topLevel</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>

<span class="w">      </span><span class="k">local</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">threads</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">      </span><span class="k">in</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">ready</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">            </span><span class="n">Queue</span><span class="p">.</span><span class="n">enque</span><span class="p">(</span><span class="n">threads</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">            </span><span class="k">case</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">deque</span><span class="w"> </span><span class="n">threads</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">               </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">valOf</span><span class="w"> </span><span class="p">(</span><span class="n">!topLevel</span><span class="p">)</span><span class="w"></span>
<span class="w">             </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">):</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">f</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">handle</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="w">                                </span><span class="p">;</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">())),</span><span class="w"></span>
<span class="w">          </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">schedule</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">ready</span><span class="w"> </span><span class="n">t</span><span class="p">;</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">yield</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">schedule</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="p">())))</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">spawn</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ready</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">new</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">run</span><span class="p">():</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                  </span><span class="p">(</span><span class="n">topLevel</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="p">()))</span><span class="w"></span>
<span class="w">                   </span><span class="p">;</span><span class="w"> </span><span class="n">next</span><span class="p">()))</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">topLevel</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">print</span><span class="p">(</span><span class="n">concat</span><span class="p">[</span><span class="n">Int</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">])</span><span class="w"></span>
<span class="w">            </span><span class="p">;</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">yield</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">;</span><span class="w"> </span><span class="n">loop</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">spawn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="n">n</span><span class="p">);</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">2</span><span class="p">))</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">spawn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">run</span><span class="w"> </span><span class="p">()</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="s">&quot;success</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_of_preemptive_threads">Example of preemptive threads</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">Queue</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">new</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">enque</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">deque</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">option</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">datatype</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">ref</span><span class="p">,</span><span class="w"> </span><span class="n">back</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">ref</span><span class="p">}</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[],</span><span class="w"> </span><span class="n">back</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[]}</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">enque</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">back</span><span class="p">,</span><span class="w"> </span><span class="p">...},</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">back</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">!back</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">deque</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="n">front</span><span class="p">,</span><span class="w"> </span><span class="n">back</span><span class="p">})</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="k">case</span><span class="w"> </span><span class="n">!front</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">            </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">!back</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                      </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>
<span class="w">                    </span><span class="p">|</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">let</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">rev</span><span class="w"> </span><span class="n">l</span><span class="w"></span>
<span class="w">                           </span><span class="k">in</span><span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                              </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="s">&quot;deque&quot;</span><span class="w"></span>
<span class="w">                            </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">back</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[];</span><span class="w"> </span><span class="n">front</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">                           </span><span class="k">end</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">front</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">l</span><span class="p">;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">Thread</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">exit</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">run</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">spawn</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">yield</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">Posix</span><span class="p">.</span><span class="n">Signal</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">MLton</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">Itimer</span><span class="w"> </span><span class="n">Signal</span><span class="w"> </span><span class="n">Thread</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">topLevel</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>

<span class="w">      </span><span class="k">local</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">threads</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">      </span><span class="k">in</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">ready</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">            </span><span class="n">Queue</span><span class="p">.</span><span class="n">enque</span><span class="p">(</span><span class="n">threads</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">            </span><span class="k">case</span><span class="w"> </span><span class="n">Queue</span><span class="p">.</span><span class="n">deque</span><span class="w"> </span><span class="n">threads</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">               </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">valOf</span><span class="w"> </span><span class="p">(</span><span class="n">!topLevel</span><span class="p">)</span><span class="w"></span>
<span class="w">             </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">):</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">Runnable</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">f</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">handle</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="w">                                </span><span class="p">;</span><span class="w"> </span><span class="n">exit</span><span class="w"> </span><span class="p">())),</span><span class="w"></span>
<span class="w">          </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">schedule</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">ready</span><span class="w"> </span><span class="n">t</span><span class="p">;</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="p">())</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">yield</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">schedule</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="p">())))</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">spawn</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ready</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">new</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">setItimer</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">Itimer</span><span class="p">.</span><span class="n">set</span><span class="w"> </span><span class="p">(</span><span class="n">Itimer</span><span class="p">.</span><span class="n">Real</span><span class="p">,</span><span class="w"></span>
<span class="w">                     </span><span class="p">{</span><span class="n">value</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="n">interval</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">t</span><span class="p">})</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">run</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">switch</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                  </span><span class="p">(</span><span class="n">topLevel</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">prepare</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="p">()))</span><span class="w"></span>
<span class="w">                   </span><span class="p">;</span><span class="w"> </span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">setHandler</span><span class="w"> </span><span class="p">(</span><span class="n">alrm</span><span class="p">,</span><span class="w"> </span><span class="n">Handler</span><span class="p">.</span><span class="n">handler</span><span class="w"> </span><span class="n">schedule</span><span class="p">)</span><span class="w"></span>
<span class="w">                                    </span><span class="p">;</span><span class="w"> </span><span class="n">setItimer</span><span class="w"> </span><span class="p">(</span><span class="n">Time</span><span class="p">.</span><span class="n">fromMilliseconds</span><span class="w"> </span><span class="mi">20</span><span class="p">)))))</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">setItimer</span><span class="w"> </span><span class="n">Time</span><span class="p">.</span><span class="n">zeroTime</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">ignore</span><span class="w"> </span><span class="n">alrm</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">topLevel</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">delay</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">delay</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">delay</span><span class="w"> </span><span class="mi">500000</span><span class="p">;</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">Thread</span><span class="p">.</span><span class="n">spawn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="n">n</span><span class="p">);</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">spawn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">loop&#39;</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Thread</span><span class="p">.</span><span class="n">run</span><span class="w"> </span><span class="p">()</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="s">&quot;success</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
