-
-
Save joom/8ccb151f45eab4bef25f to your computer and use it in GitHub Desktop.
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
-- ghc --make -O2 -threaded setInterval.hs | |
import Control.Concurrent (forkIO, putMVar, takeMVar, | |
newEmptyMVar, threadDelay) | |
import Control.Exception (finally) | |
setInterval :: IO a -> Int -> IO () | |
setInterval action microsecs = do | |
mvar <- newEmptyMVar | |
_ <- forkIO $ loop `finally` putMVar mvar () | |
takeMVar mvar | |
where loop = threadDelay microsecs >> action >> loop | |
main :: IO () | |
main = setInterval (putStrLn "yolo") 1000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment