如何终止 QThread [英] How can I terminate a QThread

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

问题描述

最近,我在这个标题中提到了这个问题.我曾尝试使用 QThread::terminate(),但我无法停止处于死循环中的线程(比方说,while(1)).

Recently ,I come across this problem as I memtioned in this Title. I have tried by using QThread::terminate(),but I just can NOT stop the thread ,which is in a dead loop (let's say,while(1)).

非常感谢.

推荐答案

终止线程是停止异步操作的简单解决方案,但这通常是一个坏主意:线程可能正在执行系统调用或可能处于终止时更新数据结构的中间过程,这可能会使程序甚至操作系统处于不稳定状态.

Terminating the thread is the easy solution to stopping an async operation, but it is usually a bad idea: the thread could be doing a system call or could be in the middle of updating a data structure when it is terminated, which could leave the program or even the OS in an unstable state.

尝试将您的 while(1) 转换为 while( isAlive() ) 并在您希望线程退出时使 isAlive() 返回 false.

Try to transform your while(1) into while( isAlive() ) and make isAlive() return false when you want the thread to exit.

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

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