On 12/29/05, <b class="gmail_sendername">Brian Sniffen</b> <<a href="mailto:brian.sniffen@gmail.com">brian.sniffen@gmail.com</a>> 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;">
> test_Cubby =<br>> do<br>> tv <- newTVar 0<br><br>You've almost got it! But "newTVar 0" has type STM Tvar, and you're<br>trying to use it in the IO monad. So just say "tv <- atomically
<br>(newTVar 0)" and you're set. Do notice that you'll see output like<br>this:<br><br>co"nisnusmeer t6 "6<br>"<br>"c"oinnssuemret 61""<br><br>""cionnsseurmte 95""
<br><br>""icnosnesrutm e2 "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> or <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 >> 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;"> do</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> tv <- atomically (newTVar 0)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> forkIO (forever $ producer tv) >> (forever $ consumer tv)</span>
<span class="q"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> producer tv = do r <- randomRIO (1,10)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
atomically (do { v <- readTVar tv</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
; writeTVar tv (v+r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
})</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
print ("insert " ++ show r)</span><br style="font-family: courier new,monospace;"></span>
<span style="font-family: courier new,monospace;">
threadDelay r</span>
<span class="q"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> consumer tv = do r <- randomRIO (1,10)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
atomically (do { v <- readTVar tv</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
; if (v < r) then retry</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
else writeTVar tv (v-r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
})</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
print ("consume " ++ show r)</span><br style="font-family: courier new,monospace;"></span>
<span style="font-family: courier new,monospace;">
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>