[Haskell-cafe] GHC.Conc.threadStatus - documentation of ThreadDied :: ThreadStatus
coot at coot.me
coot at coot.me
Mon Mar 7 19:59:44 UTC 2022
Hi Olaf,
`forkIO` is rather a low level. It's more common to use async package (https://hackage.haskell.org/package/async). Async has `waitCatch` which allows you to wait for a thread to finish and get access to either an exception which killed the thread or the result of the thread handler.
Best regards,
Marcin Szamotulski
Sent with ProtonMail secure email.
------- Original Message -------
On Monday, March 7th, 2022 at 10:56, Olaf Klinke <olf at aatal-apotheke.de> wrote:
> Dear Cafe,
>
> I had expected to see ThreadDied in the small example below.
>
> But when I compile with
>
> ghc --make -threaded -with-rtsopts=-N2
>
> The output is:
>
> threadStatus: user error (child thread is crashing!)
>
> The status of my child is:
>
> ThreadFinished
>
> The output is not really a lie. But how do I determine whether a child
>
> thread has exited normally or not? Wouldn't you say a call to fail (or
>
> any other throwIO) should count as ThreadDied?
>
> The documentation of GHC.Conc.forkIO says:
>
> "... passes all other exceptions to the uncaught exception handler."
>
> and the documentation for GHC.Conc.ThreadStatus says:
>
> ThreadDied -- the thread received an uncaught exception
>
> One can provoke ThreadDied by using throwTo from the parent thread. So
>
> the emphasis in the documentation of ThreadDied should be on the word
>
> "received".
>
> This is a case of misleading documentation, in my humble opinion.
>
> The constructor should not be named ThreadDied because that suggests
>
> inclusion of internal reasons.
>
> Olaf
>
> -- begin threadStatus.hs
>
> import Control.Concurrent
>
> import GHC.Conc
>
> main = mainThread
>
> childThread :: IO ()
>
> childThread = fail "child thread is crashing!"
>
> mainThread :: IO ()
>
> mainThread = do
>
> child <- forkIO childThread
>
> threadDelay 5000
>
> status <- threadStatus child
>
> putStr "The status of my child is: "
>
> print status
>
> -- end threadStatus.hs
>
> _______________________________________________
>
> Haskell-Cafe mailing list
>
> To (un)subscribe, modify options or view archives go to:
>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 509 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20220307/b7ceaf6e/attachment.sig>
More information about the Haskell-Cafe
mailing list