gpt4 book ai didi

multithreading - GHC 如何识别一个线程是 BlockedIndefinitelyOnMVar?

转载 作者:行者123 更新时间:2023-12-04 04:30:22 25 4
gpt4 key购买 nike

如果线程等待 MVar没有可能的生产者,GHC 通常足够聪明,可以抛出 BlockedIndefinitelyOnMVar并杀死线程。这种检测是如何实现的?

它是否检查所有对 MVar 的引用都来自等待它的线程,或者类似的东西?

最佳答案

uses the garbage collector .

来自 Control.Concurrent:

GHC attempts to detect when threads are deadlocked using the garbage collector. A thread that is not reachable (cannot be found by following pointers from live objects) must be deadlocked, and in this case the thread is sent an exception. The exception is either 'BlockedIndefinitelyOnMVar', 'BlockedIndefinitelyOnSTM', 'NonTermination', or 'Deadlock', depending on the way in which the thread is deadlocked.

Note that this feature is intended for debugging, and should not be relied on for the correct operation of your program. There is no guarantee that the garbage collector will be accurate enough to detect your deadlock, and no guarantee that the garbage collector will run in a timely enough manner. Basically, the same caveats as for finalizers apply to deadlock detection.

There is a subtle interaction between deadlock detection and finalizers (as created by 'Foreign.Concurrent.newForeignPtr' or the functions in "System.Mem.Weak"): if a thread is blocked waiting for a finalizer to run, then the thread will be considered deadlocked and sent an exception. So preferably don't do this, but if you have no alternative then it is possible to prevent the thread from being considered deadlocked by making a 'StablePtr' pointing to it. Don't forget to release the 'StablePtr' later with 'freeStablePtr'.

关于multithreading - GHC 如何识别一个线程是 BlockedIndefinitelyOnMVar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com