从套接字(Windows)杀死等待hGetLine的线程
如果我在Windows(10)机器上运行以下Java程序(为了完整起见,我在这里发布该程序): 然后我从Haskell连接到Java服务器:从套接字(Windows)杀死等待hGetLine的线程,windows,multithreading,sockets,haskell,io,Windows,Multithreading,Sockets,Haskell,Io,如果我在Windows(10)机器上运行以下Java程序(为了完整起见,我在这里发布该程序): 然后我从Haskell连接到Java服务器: readWithinNSecs :: IO () readWithinNSecs = withSocketsDo $ do h <- connectTo "localhost" (PortNumber 9090) hSetBuffering h NoBuffering readerTid <- forkIO $ reader h
readWithinNSecs :: IO ()
readWithinNSecs = withSocketsDo $ do
h <- connectTo "localhost" (PortNumber 9090)
hSetBuffering h NoBuffering
readerTid <- forkIO $ reader h
threadDelay $ 2 * 10^6
putStrLn "Killing the reader"
killThread readerTid
putStrLn "Reader thread killed"
where
reader h = do
line <- strip <$> hGetLine h
putStrLn $ "Got " ++ line
如果Haskell程序是线程化/非线程化编译的,会有什么区别吗。通过编译不带
线程的程序
主线程可以杀死读取器
线程。但是,不使用线程化不是一个选项(我们确实希望使用操作系统线程)。顺便说一句,在linux上,这个示例似乎对线程化和非线程化RTS都有效。因此,这看起来是windows特有的问题。这就是为什么我在问题中多次提到Windows;)看起来bug报告已在8.2.1中修复。确认了吗?
readWithinNSecs :: IO ()
readWithinNSecs = withSocketsDo $ do
h <- connectTo "localhost" (PortNumber 9090)
hSetBuffering h NoBuffering
readerTid <- forkIO $ reader h
threadDelay $ 2 * 10^6
putStrLn "Killing the reader"
killThread readerTid
putStrLn "Reader thread killed"
where
reader h = do
line <- strip <$> hGetLine h
putStrLn $ "Got " ++ line
<socket: 380>: hGetLine: failed (No error)
import Control.Concurrent.Async
-- ...
readerAsync h = do
a <- async $ strip <$> hGetLine h
line <- wait a
putStrLn $ "Got " ++ line