Skip to content

Instantly share code, notes, and snippets.

@nh2
Created February 18, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nh2/bcf583721213d34e9f464558a91a682e to your computer and use it in GitHub Desktop.
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
-- 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