如何强制终止线程? [英] How to force terminate thread?

查看:90
本文介绍了如何强制终止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用跨平台#include< thread>

I use cross-platform #include <thread>

例如:在线程中我尝试neo4j查询并等待等待...

For example: In thread I try neo4j query and wait and wait...

主线程知道时间超过超时,如果我调用删除线程,它会终止整个程序!

main thread knows that time exceeds timeout, buy if I call delete thread, it terminate whole program!

推荐答案

我很高兴C ++运行时在这里做了一些如此明智的事情。

I am actually glad that the C++ runtime is doing something so sensible here.

你看,终止一个线程是一件非常危险的事情,因为你要终止的线程就是这样做的。虽然这似乎是一个好主意,但它很少。除非你确切地知道你终止的线程在
实际终止线程时正在做什么,否则各种各样的事情都可能出错。最明显的一点是你正在终止的线程持有某种锁,比如内存分配锁,或任何C运行时锁,甚至可能是加载器锁。

You see, terminating a thread is a very dangerous thing to do because the thread you are terminating literally does that. While it may seem like a good idea, it rarely is. Unless you know exactly what the thread you are terminating is doing at the time you actually terminate the thread, then all sorts of things can go wrong. The most obvious one is that the thread you are terminating is holding a lock of some kind, like the memory allocation lock, or any of the C Runtime locks or even maybe the loader lock.

因此,在终止过程中,您永远无法合理地继续执行流程中的任何线程,您唯一能做的就是终止整个过程。

So you can never reasonably continue the execution of any threads in a process after you terminate one, the only thing you can do is to terminate the entire process.

如果您遇到问题当你期望它没有退出时,你可以做的最好的事情是切换到该线程,看看它在做什么。很可能你在该线程中有一个无限循环,或者它被阻塞等待某事。

If you are having issues with a thread not exiting when you expect it to, the best thing you can do is switch to that thread and see what it is doing. It is likely that you have an infinite loop in that thread, or it is blocked waiting on something.


这篇关于如何强制终止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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