【发布时间】:2018-09-05 22:41:23
【问题描述】:
根据GHC user guide外呼可以标记interruptible,但是我不能让它工作。我在 GNU/Linux 上使用 ghc 8.4.3。
例如看这个 cbits.h:
/* cbits.h */
void loopForever();
cbits.c:
/* cbits.c */
#include <stdio.h>
#include <unistd.h>
#include "cbits.h"
void loopForever()
{
for (;;)
{
printf("Tick\n");
sleep(1);
}
}
最后是 Test.hs:
-- Test.hs
{-# LANGUAGE InterruptibleFFI #-}
module Main where
import Control.Concurrent
import Control.Concurrent.Async
main :: IO ()
main = race_ loopForever $ do
threadDelay 2000000
putStrLn "Finished"
foreign import ccall interruptible "cbits.h"
loopForever :: IO ()
我和ghc -threaded -o a.out cbits.c Test.hs一起编译的。
现在,我预计代码会在 2 秒后停止,但它会继续运行,即使在打印“完成”之后也是如此。它确实在用户指南中提到了This is **usually** enough to cause a blocking system call to return <...>,所以这是我的 c 函数特别糟糕的情况,还是我在 Haskell 方面做错了什么?
【问题讨论】: