控制线程退出应用哈斯克尔 [英] Control thread to exit haskell application

查看:122
本文介绍了控制线程退出应用哈斯克尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的哈斯克尔,并与几样我有一个问题,我不能停止程序乱搞。我使用的是Windows 7和使用runhaskell从GHT。按Ctrl-C不工作,所以我不得不求助于这是一个痛苦位的任务管理器。

相反,这样做,我怎么可以创建一个单独的控制线程,它会等到我输入Q和然后退出我的Haskell的应用。

我已经得到了问题的应用程序的格式为:

  =主要做
 H< - connectTo服务器号(端口号(fromInteger口))
 hSetBuffering ^ h NoBuffering
 ...做一些东西与插座手柄...
 听^ h听::手柄 - > IO()
听H = $永远做
  T< - hGetLine ^ h
  让S =初始化Ť
  putStrLn小号
哪里
  一个永远的=做;一个永远

在伪code想什么,我拥有的是:

  =主要做
  waitForQuit
  ......原程序...waitForQuit :: IO()
   选项​​< - 的getchar
   如果选择=='Q',那么
     ...杀...应用
   其他
     waitForQuit


解决方案

您应该能够Haskell的线程的getchar和退出{有了,成功,失败}做到这一点。

 进口Control.Concurrent
进口System.Exit
进口Data.Char(TOLOWER)
进口System.IO主要做=
    forkIO realMain
    exitOnQexitOnQ = DO
    hSetBuffering标准输入NoBuffering
    C< - 的getchar
    当(TOLOWER C / ='Q')exitOnQ
    exitSuccess - 或exitWith,有的退出code值,使用hoogle。

打破下来:你可以通过 forkIO 获得兼任。请注意这是不是一个独立的操作系统线程,但一个Haskell线程是极其轻便。在 exitOnQ 线程需要得到击键刻不容缓 - 没有 NoBuffering 你不得不打Q- [ENTER] 。如果pressed关键不是(或问:),那么我们循环,否则就终止程序通过许多退出* 通话之一。

警告:这是一个非常罕见的极端例子,但GHC使用GC点作为线程调度点(有这个在过去两年改变了吗?),所以如果你的code花费的时间显著块中执行大量的纯具有零分配计算,那么你不能用这种方法来退出(除非你有通过螺纹RTS多个操作系统线程的的一个 + RTS -N#选项)。

I'm brand new to Haskell and in messing around with a few samples I've got a problem where I can't stop the program. I'm using Windows 7 and using runhaskell from ght. Ctrl-c doesn't work so I have to resort to the task manager which is a bit of a pain.

Instead of doing that how can I create a separate control thread that would wait until I typed q and then quit my Haskell application.

The application I've got the problem with is of the format:

main = do
 h <- connectTo server (PortNumber (fromInteger port))
 hSetBuffering h NoBuffering
 ... do some stuff with the socket handle ...
 listen h

listen :: Handle -> IO ()
listen h = forever $ do
  t <- hGetLine h
  let s = init t
  putStrLn s
where
  forever a = do a; forever a

In pseudo-code what I'd like to have is:

main = do
  waitForQuit
  ... original program ...

waitForQuit :: IO()
   option <- getChar
   if option == 'q' then
     ... kill the app ... 
   else 
     waitForQuit

解决方案

You should be able to do this with a Haskell thread, getChar and exit{With,Success,Failure}.

import Control.Concurrent
import System.Exit
import Data.Char (toLower)
import System.IO

main = do
    forkIO realMain
    exitOnQ

exitOnQ = do
    hSetBuffering stdin NoBuffering
    c <- getChar
    when (toLower c /= 'q') exitOnQ
    exitSuccess  -- or "exitWith" and some ExitCode value, use hoogle.

Breaking this down: You get concurrently via forkIO. Notice this isn't a separate OS thread, but a Haskell thread which is extremely lightweight. The exitOnQ thread needs to get keystrokes without delay - without the NoBuffering you'd have to hit q-[ENTER]. If the pressed key wasn't q (or Q) then we loop, otherwise we terminate the program via one of the many exit* calls.

WARNING: It is a very uncommon corner case, but GHC uses GC points as thread scheduling points (has this changed in the past two years?) so if your code is spending significant blocks of time performing lots of pure computations that have zero allocation then you can't use this method to quit (unless you have multiple OS threads via the threaded RTS and an +RTS -N# option).

这篇关于控制线程退出应用哈斯克尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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