ticker-1.0.0: A concurrent utility inspired by Ticker in golang

Safe HaskellSafe
LanguageHaskell2010

Control.Concurrent.Ticker

Synopsis

Documentation

newTicker Source #

Arguments

:: Int

ticker rate by micro sec

-> IO (Chan (), IO ())

ticker channel and ticker stopper

Create a new ticker.

>>> import Control.Concurrent.Chan (getChanContents)
>>> import Control.Monad (forM_)
>>> import System.Timeout (timeout)
>>> (chan, cancelTicker) <- newTicker $ 10^3 * 100
>>> chanStream <- getChanContents chan
>>> _ <- timeout (10^3 * 350) $ forM_ chanStream (\_ -> putStr "Tick!")
Tick!Tick!Tick!
>>> cancelTicker

withTicker Source #

Arguments

:: Int

ticker rate by micro sec

-> (Chan () -> IO a)

handler function

-> IO a

result of handler

Create a new ticker and pass a Chan () of the ticker to the supplied function. The ticker thread will be closed automatically.

>>> import Control.Concurrent.Chan (getChanContents)
>>> import Control.Monad (forM_)
>>> import System.Timeout (timeout)
>>> :{
withTicker (10^3 * 100) $ \chan -> do
  chanStream <- getChanContents chan
  _ <- timeout (10^3 * 350) $ forM_ chanStream (\_ -> putStr "Tick!")
  return ()
:}
Tick!Tick!Tick!

newCPUTimeTicker :: Int -> IO (Chan (), IO ()) Source #

Deprecated: It doesn't work well.

A variant of the newTicker that uses getCPUTime instead of threadDelay internally.