<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>MLtonThread - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton 20061025</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      MLtonThread
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">

<pre class=code>
<B><FONT COLOR="#5F9EA0">signature</FONT></B> MLTON_THREAD <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> AtomicState:
         <B><FONT COLOR="#5F9EA0">sig</FONT></B>
            <B><FONT COLOR="#A020F0">datatype</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B> NonAtomic <B><FONT COLOR="#5F9EA0">|</FONT></B> Atomic <B><FONT COLOR="#A020F0">of</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>
         <B><FONT COLOR="#A020F0">end</FONT></B>

      <B><FONT COLOR="#A020F0">val</FONT></B> atomically: (<B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a
      <B><FONT COLOR="#A020F0">val</FONT></B> atomicBegin: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> atomicEnd: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> atomicState: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> AtomicState.t

      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Runnable:
         <B><FONT COLOR="#5F9EA0">sig</FONT></B>
            <B><FONT COLOR="#A020F0">type</FONT></B> t
         <B><FONT COLOR="#A020F0">end</FONT></B>

      <B><FONT COLOR="#A020F0">type</FONT></B> 'a t

      <B><FONT COLOR="#A020F0">val</FONT></B> atomicSwitch: ('a t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> Runnable.t) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a
      <B><FONT COLOR="#A020F0">val</FONT></B> new: ('a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a t
      <B><FONT COLOR="#A020F0">val</FONT></B> prepend: 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> ('b <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'b t
      <B><FONT COLOR="#A020F0">val</FONT></B> prepare: 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> Runnable.t
      <B><FONT COLOR="#A020F0">val</FONT></B> switch: ('a t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> Runnable.t) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a
   <B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
<tt>MLton.Thread</tt> provides access to MLton'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 <tt>switch</tt>ed to.  <tt>MLton.Thread</tt> does not include a default scheduling mechanism, but it can be used to implement both preemptive and non-preemptive threads. 
</p>

    <ul>

    <li>
<p>
 <tt>type&nbsp;AtomicState.t</tt> 
</p>
</li>

        <ul>

  the type of atomic states. 
        </ul>


    <li class="gap">
<p>
 <tt>atomically&nbsp;f</tt> 
</p>
</li>

        <ul>

  runs <tt>f</tt> in a critical section. 
        </ul>


    <li class="gap">
<p>
 <tt>atomicBegin&nbsp;()</tt> 
</p>
</li>

        <ul>

  begins a critical section. 
        </ul>


    <li class="gap">
<p>
 <tt>atomicEnd&nbsp;()</tt> 
</p>
</li>

        <ul>

  ends a critical section. 
        </ul>


    <li class="gap">
<p>
 <tt>atomicState&nbsp;()</tt> 
</p>
</li>

        <ul>

  returns the current atomic state. 
        </ul>


    <li class="gap">
<p>
 <tt>type&nbsp;Runnable.t</tt> 
</p>
</li>

        <ul>

  the type of threads that can be resumed. 
        </ul>


    <li class="gap">
<p>
 <tt>type&nbsp;'a&nbsp;t</tt> 
</p>
</li>

        <ul>

  the type of threads that expect a value of type <tt>'a</tt>. 
        </ul>


    <li class="gap">
<p>
 <tt>atomicSwitch&nbsp;f</tt> 
</p>
</li>

        <ul>

  like <tt>switch</tt>, but assumes an atomic calling context.  Upon   <tt>switch</tt>ing back to the current thread, an implicit   <tt>atomicEnd</tt> is performed. 
        </ul>


    <li class="gap">
<p>
 <tt>new&nbsp;f</tt> 
</p>
</li>

        <ul>

  creates a new thread that, when run, applies <tt>f</tt> to the value   given to the thread.  <tt>f</tt> must terminate by <tt>switch</tt>ing to   another thread or exiting the process. 
        </ul>


    <li class="gap">
<p>
 <tt>prepend&nbsp;(t,&nbsp;f)</tt> 
</p>
</li>

        <ul>

  creates a new thread (destroying <tt>t</tt> in the process) that first   applies <tt>f</tt> to the value given to the thread and then continues   with <tt>t</tt>.  This is a constant time operation. 
        </ul>


    <li class="gap">
<p>
 <tt>prepare&nbsp;(t,&nbsp;v)</tt> 
</p>
</li>

        <ul>

  prepares a new runnable thread (destroying <tt>t</tt> in the process)   that will evaluate <tt>t</tt> on <tt>v</tt>. 
        </ul>


    <li class="gap">
<p>
 <tt>switch&nbsp;f</tt> 
</p>
</li>

        <ul>

  applies <tt>f</tt> to the current thread to get <tt>rt</tt>, and then start   running thread <tt>rt</tt>.  It is an error for <tt>f</tt> to perform   another <tt>switch</tt>.  <tt>f</tt> is guaranteed to run atomically. 
        </ul>



    </ul>


<h2 id="head-b59f5a84d311ffa9c081e150b9d22e77bb539300">Example of non-preemptive threads</h2>

<pre class=code>
<B><FONT COLOR="#5F9EA0">structure</FONT></B> Queue:
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B> 'a t

      <B><FONT COLOR="#A020F0">val</FONT></B> new: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a t
      <B><FONT COLOR="#A020F0">val</FONT></B> enque: 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> deque: 'a t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a option
   <B><FONT COLOR="#A020F0">end</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#5F9EA0">struct</FONT></B>
      <B><FONT COLOR="#A020F0">datatype</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> T <B><FONT COLOR="#A020F0">of</FONT></B> {front: 'a <B><FONT COLOR="#228B22">list</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B>, back: 'a <B><FONT COLOR="#228B22">list</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B>}

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">new</FONT></I></B></FONT></B>() <B><FONT COLOR="#5F9EA0">=</FONT></B> T{front <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> [], back <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> []}

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">enque</FONT></I></B></FONT></B>(T{back, ...}, x) <B><FONT COLOR="#5F9EA0">=</FONT></B> back <B><FONT COLOR="#5F9EA0">:=</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>back

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">deque</FONT></I></B></FONT></B>(T{front, back}) <B><FONT COLOR="#5F9EA0">=</FONT></B>
         <B><FONT COLOR="#A020F0">case</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>front <B><FONT COLOR="#A020F0">of</FONT></B>
            [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (<B><FONT COLOR="#A020F0">case</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>back <B><FONT COLOR="#A020F0">of</FONT></B>
                      [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>
                    <B><FONT COLOR="#5F9EA0">|</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B> rev l
                           <B><FONT COLOR="#A020F0">in</FONT></B> <B><FONT COLOR="#A020F0">case</FONT></B> l <B><FONT COLOR="#A020F0">of</FONT></B>
                              [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">&quot;deque&quot;</FONT></B>
                            <B><FONT COLOR="#5F9EA0">|</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (back <B><FONT COLOR="#5F9EA0">:=</FONT></B> []; front <B><FONT COLOR="#5F9EA0">:=</FONT></B> l; <B><FONT COLOR="#5F9EA0">SOME</FONT></B> x)
                           <B><FONT COLOR="#A020F0">end</FONT></B>)
          <B><FONT COLOR="#5F9EA0">|</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (front <B><FONT COLOR="#5F9EA0">:=</FONT></B> l; <B><FONT COLOR="#5F9EA0">SOME</FONT></B> x) 
   <B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#5F9EA0">structure</FONT></B> Thread:
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> exit: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a
      <B><FONT COLOR="#A020F0">val</FONT></B> run: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> spawn: (<B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> yield: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
   <B><FONT COLOR="#A020F0">end</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#5F9EA0">struct</FONT></B>
      <B><FONT COLOR="#A020F0">open</FONT></B> MLton
      <B><FONT COLOR="#A020F0">open</FONT></B> Thread

      <B><FONT COLOR="#A020F0">val</FONT></B> topLevel: Thread.Runnable.t option <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>

      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">val</FONT></B> threads: Thread.Runnable.t Queue.t <B><FONT COLOR="#5F9EA0">=</FONT></B> Queue.new()
      <B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">         fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">ready</FONT></I></B></FONT></B> (t: Thread.Runnable.t) : <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> 
            Queue.enque(threads, t)
<B><FONT COLOR="#A020F0">         fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">next</FONT></I></B></FONT></B> () : Thread.Runnable.t <B><FONT COLOR="#5F9EA0">=</FONT></B>
            <B><FONT COLOR="#A020F0">case</FONT></B> Queue.deque threads <B><FONT COLOR="#A020F0">of</FONT></B>
               <B><FONT COLOR="#5F9EA0">NONE</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> valOf(<B><FONT COLOR="#5F9EA0">!</FONT></B>topLevel)
             <B><FONT COLOR="#5F9EA0">|</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> t
      <B><FONT COLOR="#A020F0">end</FONT></B>
   
      <B><FONT COLOR="#A020F0">fun</FONT></B> 'a exit(): 'a <B><FONT COLOR="#5F9EA0">=</FONT></B> switch(<B><FONT COLOR="#A020F0">fn</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> next())
      
<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">new</FONT></I></B></FONT></B>(f: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>): Thread.Runnable.t <B><FONT COLOR="#5F9EA0">=</FONT></B>
         Thread.prepare
         (Thread.new (<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ((f() <B><FONT COLOR="#A020F0">handle</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> exit())
                                ; exit())),
          ())
         
<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">schedule</FONT></I></B></FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B> (ready t; next())

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">yield</FONT></I></B></FONT></B>(): <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> switch(<B><FONT COLOR="#A020F0">fn</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> schedule (Thread.prepare (t, ())))

      <B><FONT COLOR="#A020F0">val</FONT></B> spawn <B><FONT COLOR="#5F9EA0">=</FONT></B> ready <B><FONT COLOR="#A020F0">o</FONT></B> new

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">run</FONT></I></B></FONT></B>(): <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
         (switch(<B><FONT COLOR="#A020F0">fn</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B>
                 (topLevel <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> (Thread.prepare (t, ()))
                  ; next()))
          ; topLevel <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>)
   <B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">fn</FONT></B> 0 <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (<B><FONT COLOR="#A020F0">print</FONT></B>(<B><FONT COLOR="#A020F0">concat</FONT></B>[Int.toString n, <B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>])
            ; Thread.yield()
            ; loop(n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1))

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop' <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">fn</FONT></B> 0 <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (Thread.spawn(<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> loop n); loop'(n <B><FONT COLOR="#5F9EA0">-</FONT></B> 2))

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> Thread.spawn(<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> loop' 10)

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> Thread.run()

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;success\n&quot;</FONT></B>
</PRE>
<p>
 
</p>
<h2 id="head-5312b6df875ff55a1aac1703500af9a957b9a09a">Example of preemptive threads</h2>

<pre class=code>
<B><FONT COLOR="#5F9EA0">structure</FONT></B> Queue:
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B> 'a t

      <B><FONT COLOR="#A020F0">val</FONT></B> new: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a t
      <B><FONT COLOR="#A020F0">val</FONT></B> enque: 'a t <B><FONT COLOR="#5F9EA0">*</FONT></B> 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> deque: 'a t <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a option
   <B><FONT COLOR="#A020F0">end</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#5F9EA0">struct</FONT></B>
      <B><FONT COLOR="#A020F0">datatype</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> T <B><FONT COLOR="#A020F0">of</FONT></B> {front: 'a <B><FONT COLOR="#228B22">list</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B>, back: 'a <B><FONT COLOR="#228B22">list</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B>}

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">new</FONT></I></B></FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B> T {front <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> [], back <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> []}

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">enque</FONT></I></B></FONT></B> (T {back, ...}, x) <B><FONT COLOR="#5F9EA0">=</FONT></B> back <B><FONT COLOR="#5F9EA0">:=</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>back

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">deque</FONT></I></B></FONT></B> (T {front, back}) <B><FONT COLOR="#5F9EA0">=</FONT></B>
         <B><FONT COLOR="#A020F0">case</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>front <B><FONT COLOR="#A020F0">of</FONT></B>
            [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (<B><FONT COLOR="#A020F0">case</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>back <B><FONT COLOR="#A020F0">of</FONT></B>
                      [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>
                    <B><FONT COLOR="#5F9EA0">|</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B> rev l
                           <B><FONT COLOR="#A020F0">in</FONT></B> <B><FONT COLOR="#A020F0">case</FONT></B> l <B><FONT COLOR="#A020F0">of</FONT></B>
                              [] <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">&quot;deque&quot;</FONT></B>
                            <B><FONT COLOR="#5F9EA0">|</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (back <B><FONT COLOR="#5F9EA0">:=</FONT></B> []; front <B><FONT COLOR="#5F9EA0">:=</FONT></B> l; <B><FONT COLOR="#5F9EA0">SOME</FONT></B> x)
                           <B><FONT COLOR="#A020F0">end</FONT></B>)
          <B><FONT COLOR="#5F9EA0">|</FONT></B> x <B><FONT COLOR="#5F9EA0">::</FONT></B> l <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (front <B><FONT COLOR="#5F9EA0">:=</FONT></B> l; <B><FONT COLOR="#5F9EA0">SOME</FONT></B> x) 
   <B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#5F9EA0">structure</FONT></B> Thread:
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> exit: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 'a
      <B><FONT COLOR="#A020F0">val</FONT></B> run: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> spawn: (<B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>) <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> yield: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
   <B><FONT COLOR="#A020F0">end</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#5F9EA0">struct</FONT></B>
      <B><FONT COLOR="#A020F0">open</FONT></B> Posix.Signal
      <B><FONT COLOR="#A020F0">open</FONT></B> MLton
      <B><FONT COLOR="#A020F0">open</FONT></B> Itimer Signal Thread

      <B><FONT COLOR="#A020F0">val</FONT></B> topLevel: Thread.Runnable.t option <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>

      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">val</FONT></B> threads: Thread.Runnable.t Queue.t <B><FONT COLOR="#5F9EA0">=</FONT></B> Queue.new ()
      <B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">         fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">ready</FONT></I></B></FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B> Queue.enque (threads, t)
<B><FONT COLOR="#A020F0">         fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">next</FONT></I></B></FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B>
            <B><FONT COLOR="#A020F0">case</FONT></B> Queue.deque threads <B><FONT COLOR="#A020F0">of</FONT></B>
               <B><FONT COLOR="#5F9EA0">NONE</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> valOf (<B><FONT COLOR="#5F9EA0">!</FONT></B>topLevel)
             <B><FONT COLOR="#5F9EA0">|</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> t
      <B><FONT COLOR="#A020F0">end</FONT></B>
   
      <B><FONT COLOR="#A020F0">fun</FONT></B> 'a exit (): 'a <B><FONT COLOR="#5F9EA0">=</FONT></B> switch (<B><FONT COLOR="#A020F0">fn</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> next ())
      
<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">new</FONT></I></B></FONT></B> (f: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>): Thread.Runnable.t <B><FONT COLOR="#5F9EA0">=</FONT></B>
         Thread.prepare
         (Thread.new (<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ((f () <B><FONT COLOR="#A020F0">handle</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> exit ())
                                ; exit ())),
          ())
         
<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">schedule</FONT></I></B></FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B> (ready t; next ())

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">yield</FONT></I></B></FONT></B> (): <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> switch (<B><FONT COLOR="#A020F0">fn</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> schedule (Thread.prepare (t, ())))

      <B><FONT COLOR="#A020F0">val</FONT></B> spawn <B><FONT COLOR="#5F9EA0">=</FONT></B> ready <B><FONT COLOR="#A020F0">o</FONT></B> new

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">setItimer</FONT></I></B></FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B>
         Itimer.set (Itimer.Real,
                     {value <B><FONT COLOR="#5F9EA0">=</FONT></B> t,
                      interval <B><FONT COLOR="#5F9EA0">=</FONT></B> t})

<B><FONT COLOR="#A020F0">      fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">run</FONT></I></B></FONT></B> (): <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B>
         (switch (<B><FONT COLOR="#A020F0">fn</FONT></B> t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B>
                  (topLevel <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> (Thread.prepare (t, ()))
                   ; new (<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (setHandler (alrm, Handler.handler schedule)
                                    ; setItimer (Time.fromMilliseconds 20)))))
          ; setItimer Time.zeroTime
          ; ignore alrm
          ; topLevel <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>)
   <B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> delay <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">fn</FONT></B> 0 <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> delay (n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1)
         
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">fn</FONT></B> 0 <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (delay 500000; loop (n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1))

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> loop' <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">fn</FONT></B> 0 <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (Thread.spawn (<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> loop n); loop' (n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1))

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> Thread.spawn (<B><FONT COLOR="#A020F0">fn</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> loop' 10)

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> Thread.run ()

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;success\n&quot;</FONT></B>
</PRE>
<p>
 
</p>
</div>



<p>
<hr>
Last edited on 2005-12-02 03:52:11 by <span title="209-6-203-188.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>
