What happens to child threads when their parent dies in GHC Haskell?

后端 未结 3 1944
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 02:55

The documentation for forkIO says

GHC note: the new thread inherits the masked state of the parent (see mask).

The newly created thread has an exce         


        
3条回答
  •  春和景丽
    2021-02-14 03:32

    This is an answer inspired by Zeta's. It uses a free monad transformer to avoid explicit nesting of computations, and the withAsync function from the async package instead of forkRunDie.

    module Main  where
    
    import Control.Monad
    import Control.Monad.Trans
    import Control.Monad.Trans.Free (FreeT,liftF,iterT)
    import Control.Concurrent
    import Control.Concurrent.Async (withAsync)
    import Control.Exception
    
    type DaemonIO = FreeT ((,) (IO ())) IO
    
    launch :: IO () -> DaemonIO ()
    launch a = liftF (a,()) 
    
    runDaemonIO :: DaemonIO a -> IO a
    runDaemonIO = iterT $ \(action,rest) -> withAsync action $ \_ -> rest
    
    main :: IO ()
    main = do
        let delaySeconds n = threadDelay $ n * (10^6)
        runDaemonIO $ do
            launch $ (forever $ delaySeconds 1 >> print "Pseudo child 1") 
                     `finally` putStrLn "killed 1!"
            launch $ (forever $ delaySeconds 1 >> print "Pseudo child 2") 
                     `finally` putStrLn "killed 2!"
            liftIO $ delaySeconds 10
            liftIO $ putStrLn "done!!!"
    

提交回复
热议问题