Quantcast

how to "sleep" or "wait" in haskell?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to "sleep" or "wait" in haskell?

caminoix-2
hello :)

i've just ran across a what-i-think-should-be-trivial-but-still-have-no-idea-how-to-do-it problem. i was going to write a config file for the dzen taskbar in haskell. the main loop should be run in certain intervals (see here), and i can't find any information about how i could do it in haskell. i couldn't find any wait or sleep or anything that sounded reasonable.

could you please share some solution with me?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to "sleep" or "wait" in haskell?

Frank Hightower
Take a look into Control.Concurrent or GHC.Conc in the Haskell documentation. There's four functions under the subtitle "Waiting" in both of them. The functions you want are the ones named "delay"; "wait" is reserved for blocking the thread untill a condition occurs (such as a resource becomming available)

threadDelay :: Int -> IO ()
 
Suspends the current thread for a given number of microseconds

There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
 
 
registerDelay :: Int -> IO (TVar Bool)
 
Set the value of returned TVar to True after a given number of microseconds. The caveats associated with threadDelay also apply.  
 
threadWaitRead :: Fd -> IO ()
 
Block the current thread until data is available to read on the given file descriptor
 
threadWaitWrite :: Fd -> IO ()  
 
Block the current thread until data can be written to the given file descriptor
There is also a sleep function (see http://www.haskell.org/pipermail/haskell-cafe/2009-February/055473.html)
Loading...