Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a Timeout in Haskell

I'm brand new to Haskell and I'm trying to figure out how to schedule a callback to be fired X seconds from now (and no sooner).

In searching Hoogle and Google, I've discovered:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/GHC-Event.html

as well as:

http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/System-Event.html

Both of these feature a "registerTimeout" function that seems to do what I want. However, System.Event appears to no longer be in the latest builds of Haskell and GHC.Event is marked as being internal.

Is there some user-space substitute I could use? I'd prefer not to use the timing functions intended for GLUT (unless that's the prescribed way of achieving my goal), and System.Timeout doesn't appear to quite what I want; it is intended to put a maximum on the amount of time something can take rather than a minimum.

Thank you for your input!

like image 571
zslayton Avatar asked Mar 27 '13 23:03

zslayton


1 Answers

I would simply execute the callback on its own thread after a delay. This is a GHC-ism.

import Control.Concurrent (forkIO, threadDelay)

-- | Run the callback after the given number of milliseconds.
runAfterDelay :: Int -> IO () -> IO ()
runAfterDelay t f = forkIO (threadDelay t >> f)

Threads in GHC are exceptionally light-weight, so this won't take tie up system resources.

like image 155
Dietrich Epp Avatar answered Sep 30 '22 04:09

Dietrich Epp