[Haskell-cafe] timely shutdown of timer threads
Albert Y. C. Lai
trebla at vex.net
Thu Feb 3 01:58:38 CET 2011
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.
data DoOrDie = Do | Die
main = do
die <- atomically newEmptyTMVar
finale <- atomically newEmptyTMVar
forkIO (timeloop die `finally` atomically (putTMVar finale ()))
putStrLn "press enter to quit"
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"
More information about the Haskell-Cafe