如何休眠或延迟Haskell中的线程? [英] How to sleep or delay the thread in Haskell?

查看:69
本文介绍了如何休眠或延迟Haskell中的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这确实不难找到答案,但是可惜我没有...我想在 do 块中延迟下一个执行步骤.我发现了功能 delay sleep nanosleep usleep .

This really shouldn't be so difficult to find an answer to, but alas I don't... I want to delay the next execution step in a do block. I have found the functions delay, sleep, nanosleep and usleep.

这个问题也没有涵盖如何使用其中任何一个,但是:在Haskell睡眠.

And also this question, that doesn't cover how to use any of these, however: Sleep in Haskell.

所有这些我都遇到了相同的错误,所以可能我从根本上做错了事:

I am getting the same error for all of these, so probably I am doing something wrong fundamentally:

变量不在范围内:delay :: Integer->IO a0

这是我的测试代码段:

main = do
  {
    putStrLn "line 1"
  ; delay 2
  ; putStrLn "line 2"
  }

Google搜索错误实际上并不会由于某些原因产生任何有用的信息.

Googling the error does not actually yield anything useful for some reason.

推荐答案

好,您必须先导入 Control.Concurrent :

import Control.Concurrent

threadDelay 1000000 --sleep for a million microseconds, or one second

这篇关于如何休眠或延迟Haskell中的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆