获取线程名称

时间:2018-07-23 21:29:39

标签: haskell

我正在与Haskell的异步运算符一起玩,我想打印线程名称,但是我用谷歌搜索它,但找不到解决方法。

这是我的代码

asyncResponse = do
                resAsync <- async operation
                response <- wait resAsync
                print response

operation = do
           threadDelay 5000000
           return "hello async world!!" --> Here Thread name

1 个答案:

答案 0 :(得分:4)

您可以使用myThreadId来获取ThreadId,它用作线程的标识符,如以下程序所示:

import Control.Concurrent
import Control.Concurrent.Async

main = asyncResponse

asyncResponse = do
  resAsync <- async operation
  response <- wait resAsync
  print response

operation = do
  threadDelay 5000000
  myid <- myThreadId
  return ("hello async world!!  my name is " ++ show myid)

运行此命令时,它会暂停5秒钟并打印:

"hello async world!!  i am thread ThreadId 5"

与线程“名称”的距离差不多。那是你想要的吗?

相关问题