Multithreading – is there a way to kill all forked threads in a ghci session without restarting it?

Based on my previous question, I would like to ask if there is any way to kill all user created threads in ghci sessions?

The reason is that when a function in ghci exits, its thread will not terminate automatically, even if it is reloaded by code Restarting ghci solves this problem, but since my application takes some time to load, it will be huge if there is a possible solution (even a hacker)

Solution

How can I build a ThreadID given that I know the actual number? Almost the same reason: the library doesn't give you anything at all to get all (still running) threads or threads of any other tool working on any thread that doesn't belong to you

In addition, you cannot reliably guess that the threads generated using forkio belong to your ghci session (all evaluations are usually sandboxed in forkio), the underlying yesod application or thread RTS (at least one call to forkio), which basically ensures that all event managers are closed) At present, this is not too bad because ghci runs in the main thread. If it is shut down, the IO manager will restart, but it may be changed in future versions

So why do threads collect even when they terminate? hs_ exit(). Essentially, it calls iomanagerdie() (kill all event managers) and exitscheduler (..) (see scheduler, basically kills all threads. None of these functions have an appropriate FFI wrapper

Calling HS_ When exit(), the main functions of Haskell world have been completed Because none of these functions has appropriate equivalents in the GHC * module, they can not be called directly in Haskell, so they can not be called in GHCi because there is no appropriate command:

So in a word, you can't It is easy to add a command to restart the scheduler in ghci But considering that the scheduler is_ Start in init() and set HS in RTs model_ Stop in exit (), which I suspect will be an easy extension

You can cheat according to what you want to do You can write your own forkiomem, which stores the ThreadID in the global Mvar and replaces all forkio in the source This can be very cumbersome, especially if you are using the library, because you need to ensure that forkio is replaced anywhere

Of course, you can use the base package, but this can be crazy (even so), change forkio and add killallforkio to control Concurrent. I've said it could be crazy

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>