On 11-02-02 04:42 PM, Warren Harris wrote:
It still seems to me that haskell is lacking when it comes to operations that 
can wait for multiple conditions.

STM opens the avenue to waiting for multiple conditions.

import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception(finally)

data DoOrDie = Do | Die

main = do
  die <- atomically newEmptyTMVar
  finale <- atomically newEmptyTMVar
  forkIO (timeloop die `finally` atomically (putTMVar finale ()))
  putStrLn "press enter to quit"
  getLine
  atomically (putTMVar die Die)
  atomically (takeTMVar finale)

timeloop die = run where
  run = do
    w <- atomically newEmptyTMVar
    i <- forkIO (threadDelay 15000000 >> atomically (putTMVar w Do))
    r <- atomically (takeTMVar w `orElse` takeTMVar die)
    case r of
      Do -> putStrLn "checkpoint" >> run
      Die -> killThread i >> putStrLn "checkmate"

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to