Created
February 18, 2017 18:52
-
-
Save nh2/bcf583721213d34e9f464558a91a682e to your computer and use it in GitHub Desktop.
Haskell snippet that demonstates that the GHC RTS spawns an unlimited number of FFI threads for safe FFI calls; see https://ghc.haskell.org/trac/ghc/ticket/13296
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- From: | |
-- slyfox: yeah, ghc's RTS does not limit amount of outstanding safe FFIs. example: http://dpaste.com/2YT058D | |
-- slyfox: 'ghc --make safe-blocking.hs -threaded -debug && time ./safe-blocking +RTS -Ds' this runs in 6 seconds and nicely prints full list of blocked threads on FFI | |
import Control.Concurrent | |
import Control.Concurrent.MVar | |
import Control.Monad | |
import Foreign.C.Types | |
foreign import ccall safe "unistd.h sleep" sleep :: CInt -> IO CInt | |
main :: IO () | |
main = do | |
ts <- forM [1..100] $ \n -> do | |
lock <- newEmptyMVar | |
forkIO $ do | |
sleep 5 | |
putStrLn $ show n ++ " in done!" | |
putMVar lock () | |
return lock | |
mapM_ takeMVar ts | |
putStrLn "all done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment