Skip to content

Instantly share code, notes, and snippets.

@joom
Forked from saml/setInterval.hs
Created July 18, 2014 21:17
Show Gist options
  • Save joom/8ccb151f45eab4bef25f to your computer and use it in GitHub Desktop.
Save joom/8ccb151f45eab4bef25f to your computer and use it in GitHub Desktop.
-- 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