从一个线程池调用Thread.Abort的上线 [英] Calling Thread.Abort on a thread from a ThreadPool

查看:175
本文介绍了从一个线程池调用Thread.Abort的上线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事正在使用第三方.NET库,我们没有源$ C ​​$ C。我们使用一个线程池来有很多的线程调用此库,偶尔一个线程只会永远挂起,而剩下的人欢快地突突沿。

My co-worker is using a third-party .NET library for which we don't have the source code. We're using a ThreadPool to have a lot of threads call into this library, and occasionally one of the threads will just hang forever while the rest of them merrily chug along.

所以我们要使用的可怕 Thread.Abort的来杀死这样的线程。我旋转了我自己的线程时这样做过,但我从来没有使用一个线程池。如果我们跟踪每个任务的启动时间是这样的:

So we want to use the dreaded Thread.Abort to kill such threads. I've done this before when spinning up my own threads, but I've never used a ThreadPool. If we track the start times of each task like this:

static Dictionary<Thread, DateTime> started = new Dictionary<Thread, DateTime>();

static void DoSomeWork(object foo)
{
    lock(started)
        started[Thread.CurrentThread] = DateTime.Now;

    SomeBuggyLibraryThatMightInfiniteLoopOrSomething.callSomeFunction(doo);

    lock(started)
        started.Remove(Thread.CurrentThread);
}

这样,我们才能锁定和遍历正在运行的线程,并调用 Thread.Abort的来杀了他们?如果我们这样做,这样,我们需要一个新的线程添加到线程池来替代,我们只杀了一个,或将线程池处理,对于我们呢?

then can we lock and iterate over the running threads and call Thread.Abort to kill them? And if we do, then will we need to add a new thread to the ThreadPool to replace the one that we just killed, or will the ThreadPool handle that for us?

编辑:我很知道所有的潜在问题与 Thread.Abort的。我知道它应该尽可能不将生产code使用,并且它不一定甚至停止线程,如果你放弃一个线程,而该线程已经获得了锁,那么你就可以挂断其他线程等等。但是现在我们是在紧的最后期限,我们有体面的理由相信,在这一特定情况下,我们可以调用 Thread.Abort的没有把整个过程处于危险之中,我们想避免重写这个程序,以消除线程池,除非绝对需要。

I'm very aware of all of the potential problems with Thread.Abort. I know that it should ideally never be used in production code, and that it doesn't necessarily even stop the thread, and that if you abort a thread while the thread has acquired a lock, then you can hang up other threads, etc. But right now we're on a tight deadline and we have decent reason to believe that in this one particular case, we can call Thread.Abort without putting the entire process in jeopardy, and we'd like to avoid rewriting this program to eliminate the ThreadPool unless we absolutely have to.

所以,我想知道的是这样的:因为我们会调用 Thread.Abort的上属于一个线程池线程,有没有引起任何特殊问题这些是线程池线程,做我们必须手动旋转了一个新的线程来替换被杀害的一个或将线程池做的我们呢?

So what I want to know is this: given that we WILL be calling Thread.Abort on a thread that belongs to a ThreadPool, are there any special problems caused by these being ThreadPool threads, and do we have to manually spin up a new thread to replace the one that got killed or will the ThreadPool do that for us?

推荐答案

没有,你不应该调用中止在线程池中的线程。从我的本地测试,似乎线程池并重新创建线程,如果你放弃他们 - 我放弃1000线程池中的线程,它仍然是工作。我不知道是否应该依赖于这种行为,但也许你可以逃脱它在这种情况下。虽然在一般使用Thread.Abort的是不正确的方式来做到这一点。

No, you shouldn't call Abort on threads in the thread pool. From my local testing, it seems that the ThreadPool does recreate threads if you abort them - I aborted 1000 thread pool threads and it was still working. I don't know if you should rely on this behaviour, but maybe you can get away with it in this case. In general though using Thread.Abort is not the right way to do this.

正确的方法来调用一个函数,你不信任表现良好是开始在一个新的进程,并在必要时杀死进程。

The correct way to call a function you don't trust to behave well is to start it in a new process and kill the process if necessary.

这篇关于从一个线程池调用Thread.Abort的上线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆