On 12/29/05, <b class="gmail_sendername">Brian Sniffen</b> &lt;<a href="mailto:brian.sniffen@gmail.com">brian.sniffen@gmail.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt; test_Cubby =<br>&gt;&nbsp;&nbsp; do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; tv &lt;- newTVar 0<br><br>You've almost got it!&nbsp;&nbsp;But &quot;newTVar 0&quot; has type STM Tvar, and you're<br>trying to use it in the IO monad.&nbsp;&nbsp;So just say &quot;tv &lt;- atomically
<br>(newTVar 0)&quot; and you're set.&nbsp;&nbsp;Do notice that you'll see output like<br>this:<br><br>co&quot;nisnusmeer t6 &quot;6<br>&quot;<br>&quot;c&quot;oinnssuemret&nbsp;&nbsp;61&quot;&quot;<br><br>&quot;&quot;cionnsseurmte&nbsp;&nbsp;95&quot;&quot;
<br><br>&quot;&quot;icnosnesrutm e2 &quot;9<br><br>since the two threads are interleaved.<br><br>--<br>Brian T. Sniffen<br><a href="mailto:bts@alum.mit.edu">bts@alum.mit.edu</a>&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;&nbsp;&nbsp;&nbsp;<a href="mailto:brian.sniffen@gmail.com">
brian.sniffen@gmail.com</a><br><a href="http://www.evenmere.org/~bts">http://www.evenmere.org/~bts</a><br></blockquote></div><br>
<br>

Thank you all for your help and comments ... <br>

<br>

<span style="font-family: courier new,monospace;">module Main where</span><span class="q"><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import System.Random</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import Control.Concurrent</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import Control.Concurrent.STM</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"></span><span class="q">
<span style="font-family: courier new,monospace;">forever act = act &gt;&gt; forever act</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"></span>

<span style="font-family: courier new,monospace;">test_Cubby =</span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">&nbsp; do</span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; tv &lt;- atomically (newTVar 0)</span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; forkIO (forever $ producer tv) &gt;&gt; (forever $ consumer tv)</span>
<span class="q"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; producer tv = do r &lt;- randomRIO (1,10)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
atomically (do { v &lt;- readTVar tv</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
; writeTVar tv (v+r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
})</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
print (&quot;insert &quot; ++ show r)</span><br style="font-family: courier new,monospace;"></span>
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
threadDelay r</span>
<span class="q"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; consumer tv = do r &lt;- randomRIO (1,10)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
atomically (do { v &lt;- readTVar tv</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
; if (v &lt; r) then retry</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else writeTVar tv (v-r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
})</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
print (&quot;consume &quot; ++ show r)</span><br style="font-family: courier new,monospace;"></span>
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
threadDelay r</span><br style="font-family: courier new,monospace;">

<br>

I also slow it down a bit to get readable output ... thanks again.<br>
- Quan<br>