<span style="font-family: courier new,monospace;">Hello folks,<br>
<br>
Newbie question: how can I do something like the following? mixing IO and STM. </span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
module Test where</span><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;"><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 <- newTVar 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
forkIO (producer tv) >> (consumer tv)</span><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 style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">
producer tv</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
return ()</span><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 style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">
consumer tv</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
return ()</span><br style="font-family: courier new,monospace;"><br><br>
Thanks.<br>